1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMercadoPagoPlugin\Payum\Action; |
6
|
|
|
|
7
|
|
|
use MercadoPago\Payment; |
8
|
|
|
use MercadoPago\SDK; |
9
|
|
|
use Odiseo\SyliusMercadoPagoPlugin\Payum\MercadoPagoApi; |
10
|
|
|
use Payum\Core\Action\ActionInterface; |
11
|
|
|
use Payum\Core\ApiAwareInterface; |
12
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
13
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
14
|
|
|
use Payum\Core\Exception\UnsupportedApiException; |
15
|
|
|
use Payum\Core\GatewayAwareInterface; |
16
|
|
|
use Payum\Core\GatewayAwareTrait; |
17
|
|
|
use Payum\Core\Reply\HttpResponse; |
18
|
|
|
use Payum\Core\Request\Notify; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
final class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface |
22
|
|
|
{ |
23
|
|
|
use GatewayAwareTrait; |
24
|
|
|
|
25
|
|
|
/** @var MercadoPagoApi */ |
26
|
|
|
private $api; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function execute($request): void |
32
|
|
|
{ |
33
|
|
|
/** @var $request Notify */ |
34
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
35
|
|
|
|
36
|
|
|
$details = ArrayObject::ensureArrayObject($request->getModel()); |
37
|
|
|
|
38
|
|
|
$data = json_decode(file_get_contents("php://input"), true); |
39
|
|
|
$this->log($data); |
40
|
|
|
|
41
|
|
|
SDK::setAccessToken($this->api->getAccessToken()); |
42
|
|
|
SDK::setIntegratorId('dev_11586dc9e7f311eab4a00242ac130004'); |
43
|
|
|
|
44
|
|
|
if ('payment' == $data['type']) { |
45
|
|
|
/** @var Payment $payment */ |
46
|
|
|
$payment = Payment::find_by_id($data['data']['id']); |
47
|
|
|
|
48
|
|
|
$paymentArray = $payment->toArray(); |
49
|
|
|
$details['payment'] = $paymentArray; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw new HttpResponse('OK', Response::HTTP_OK); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $data |
57
|
|
|
*/ |
58
|
|
|
private function log(array $data): void |
59
|
|
|
{ |
60
|
|
|
// Todo use a better way to log the information |
61
|
|
|
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL. |
62
|
|
|
"Attempt: ".(json_encode($data)).PHP_EOL. |
63
|
|
|
"-------------------------".PHP_EOL; |
64
|
|
|
|
65
|
|
|
file_put_contents(__DIR__.'/../../../../../../var/log/mercado_pago_log_'.date("j.n.Y").'.txt', $log, FILE_APPEND); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function supports($request) |
72
|
|
|
{ |
73
|
|
|
return |
74
|
|
|
$request instanceof Notify && |
75
|
|
|
$request->getModel() instanceof \ArrayAccess |
76
|
|
|
; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function setApi($api): void |
83
|
|
|
{ |
84
|
|
|
if (!$api instanceof MercadoPagoApi) { |
85
|
|
|
throw new UnsupportedApiException('Not supported. Expected an instance of ' . MercadoPagoApi::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->api = $api; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|