1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMercadoPagoPlugin\Payum\Action; |
6
|
|
|
|
7
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
8
|
|
|
use MercadoPago\Item; |
9
|
|
|
use MercadoPago\Payer; |
10
|
|
|
use MercadoPago\Preference; |
11
|
|
|
use MercadoPago\SDK; |
12
|
|
|
use Odiseo\SyliusMercadoPagoPlugin\Payum\MercadoPagoApi; |
13
|
|
|
use Payum\Core\Action\ActionInterface; |
14
|
|
|
use Payum\Core\ApiAwareInterface; |
15
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
16
|
|
|
use Payum\Core\Exception\UnsupportedApiException; |
17
|
|
|
use Payum\Core\GatewayAwareInterface; |
18
|
|
|
use Payum\Core\GatewayAwareTrait; |
19
|
|
|
use Payum\Core\Reply\HttpRedirect; |
20
|
|
|
use Payum\Core\Request\Capture; |
21
|
|
|
use Payum\Core\Security\GenericTokenFactoryAwareInterface; |
22
|
|
|
use Payum\Core\Security\GenericTokenFactoryAwareTrait; |
23
|
|
|
use Sylius\Bundle\PayumBundle\Request\GetStatus; |
24
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
25
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
26
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
27
|
|
|
use Sylius\Component\Core\Model\PaymentInterface as SyliusPaymentInterface; |
28
|
|
|
|
29
|
|
|
final class CaptureAction implements |
30
|
|
|
ActionInterface, |
31
|
|
|
ApiAwareInterface, |
32
|
|
|
GatewayAwareInterface, |
33
|
|
|
GenericTokenFactoryAwareInterface |
34
|
|
|
{ |
35
|
|
|
use GatewayAwareTrait; |
36
|
|
|
use GenericTokenFactoryAwareTrait; |
37
|
|
|
|
38
|
|
|
/** @var MercadoPagoApi */ |
39
|
|
|
private $api; |
40
|
|
|
|
41
|
|
|
/** @var CacheManager */ |
42
|
|
|
private $imagineCacheManager; |
43
|
|
|
|
44
|
|
|
public function __construct(CacheManager $imagineCacheManager) |
45
|
|
|
{ |
46
|
|
|
$this->imagineCacheManager = $imagineCacheManager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function execute($request): void |
53
|
|
|
{ |
54
|
|
|
/** @var Capture $request */ |
55
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
56
|
|
|
|
57
|
|
|
/** @var SyliusPaymentInterface $payment */ |
58
|
|
|
$payment = $request->getModel(); |
59
|
|
|
|
60
|
|
|
$this->gateway->execute($status = new GetStatus($payment)); |
61
|
|
|
|
62
|
|
|
if ($status->isNew()) { |
63
|
|
|
/** @var OrderInterface $order */ |
64
|
|
|
$order = $payment->getOrder(); |
65
|
|
|
|
66
|
|
|
/** @var CustomerInterface $customer */ |
67
|
|
|
$customer = $order->getCustomer(); |
68
|
|
|
|
69
|
|
|
/** @var AddressInterface $billingAddress */ |
70
|
|
|
$billingAddress = $order->getBillingAddress(); |
71
|
|
|
|
72
|
|
|
// $items = $order->getItems(); |
73
|
|
|
|
74
|
|
|
SDK::setAccessToken($this->api->getAccessToken()); |
75
|
|
|
|
76
|
|
|
SDK::setIntegratorId('dev_11586dc9e7f311eab4a00242ac130004'); |
77
|
|
|
|
78
|
|
|
$preference = new Preference(); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$preferenceItems = []; |
82
|
|
|
|
83
|
|
|
// TODO create items for products when MercadoPago fix the API |
84
|
|
|
|
85
|
|
|
// foreach ($items as $item) { |
86
|
|
|
// $preferenceItem = new Item(); |
87
|
|
|
// |
88
|
|
|
// $preferenceItem->__set('id', $item->getId()); |
89
|
|
|
// $preferenceItem->__set('title', $item->getProductName()); |
90
|
|
|
// $preferenceItem->__set('quantity', $item->getQuantity()); |
91
|
|
|
// $preferenceItem->__set('currency_id', $order->getCurrencyCode()); |
92
|
|
|
// $preferenceItem->__set('unit_price', $item->getUnitPrice() / 100); |
93
|
|
|
// |
94
|
|
|
// /** @var ProductInterface $product */ |
95
|
|
|
// $product = $item->getProduct(); |
96
|
|
|
// |
97
|
|
|
// if (!$product->getImagesByType('thumbnail')->isEmpty()) { |
98
|
|
|
// /** @var ImageInterface $image */ |
99
|
|
|
// $image = $product->getImagesByType('thumbnail')->first(); |
100
|
|
|
// |
101
|
|
|
// $path = $this->imagineCacheManager->getBrowserPath( |
102
|
|
|
// (string)parse_url($image->getPath() ?: '', PHP_URL_PATH), |
103
|
|
|
// 'sylius_shop_product_tiny_thumbnail' |
104
|
|
|
// ); |
105
|
|
|
// } elseif ($product->getImages()->first()) { |
106
|
|
|
// /** @var ImageInterface $image */ |
107
|
|
|
// $image = $product->getImages()->first(); |
108
|
|
|
// |
109
|
|
|
// $path = $this->imagineCacheManager->getBrowserPath( |
110
|
|
|
// (string)parse_url($image->getPath() ?: '', PHP_URL_PATH), |
111
|
|
|
// 'sylius_shop_product_tiny_thumbnail' |
112
|
|
|
// ); |
113
|
|
|
// } else { |
114
|
|
|
// $path = '//placehold.it/64x64'; |
115
|
|
|
// } |
116
|
|
|
// |
117
|
|
|
// $preferenceItem->__set('picture_url', $path); |
118
|
|
|
// |
119
|
|
|
// $preferenceItems[] = $preferenceItem; |
120
|
|
|
// } |
121
|
|
|
// |
122
|
|
|
// if ($order->getShippingTotal() > 0) { |
123
|
|
|
// $shipment = new Item(); |
124
|
|
|
// |
125
|
|
|
// $shipment->__set('id', 'shipping'); |
126
|
|
|
// $shipment->__set('title', 'Shipping'); |
127
|
|
|
// $shipment->__set('quantity', 1); |
128
|
|
|
// $shipment->__set('currency_id', $order->getCurrencyCode()); |
129
|
|
|
// $shipment->__set('unit_price', $order->getShippingTotal() / 100); |
130
|
|
|
// |
131
|
|
|
// $preferenceItems[] = $shipment; |
132
|
|
|
// } |
133
|
|
|
// |
134
|
|
|
// if ($order->getAdjustmentsTotalRecursively('tax') > 0) { |
135
|
|
|
// $tax = new Item(); |
136
|
|
|
// |
137
|
|
|
// $tax->__set('id', 'tax'); |
138
|
|
|
// $tax->__set('title', 'Tax'); |
139
|
|
|
// $tax->__set('quantity', 1); |
140
|
|
|
// $tax->__set('currency_id', $order->getCurrencyCode()); |
141
|
|
|
// $tax->__set('unit_price', $order->getAdjustmentsTotalRecursively('tax') / 100); |
142
|
|
|
// |
143
|
|
|
// $preferenceItems[] = $tax; |
144
|
|
|
// } |
145
|
|
|
|
146
|
|
|
$preferenceItem = new Item(); |
147
|
|
|
|
148
|
|
|
$orderTotal = $order->getTotal() / 100; |
149
|
|
|
|
150
|
|
|
$preferenceItem->__set('id', $order->getId()); |
151
|
|
|
$preferenceItem->__set('title', 'TOTAL'); |
152
|
|
|
$preferenceItem->__set('quantity', 1); |
153
|
|
|
$preferenceItem->__set('currency_id', $order->getCurrencyCode()); |
154
|
|
|
$preferenceItem->__set('unit_price', $orderTotal); |
155
|
|
|
|
156
|
|
|
$preferenceItems[] = $preferenceItem; |
157
|
|
|
|
158
|
|
|
$preference->__set('items', $preferenceItems); |
159
|
|
|
|
160
|
|
|
/** Todo try to implement installments |
161
|
|
|
$preferencePayment = new Payment(); |
162
|
|
|
|
163
|
|
|
$channel = $order->getChannel(); |
164
|
|
|
$minimumForInstalments = $channel->getMinimumOrderTotalForInstalments(); |
165
|
|
|
if ($minimumForInstalments) { |
166
|
|
|
if ($orderTotal < $minimumForInstalments) { |
167
|
|
|
$preferencePayment->__set('installments', 1); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$preference->__set('payment_methods', $preferencePayment); |
172
|
|
|
**/ |
173
|
|
|
|
174
|
|
|
$payer = new Payer(); |
175
|
|
|
|
176
|
|
|
$payerFirstName = $customer->getFirstName() !== null ? $customer->getFirstName() : $billingAddress->getFirstName(); |
177
|
|
|
$payerLastName = $customer->getLastName() !== null ? $customer->getLastName() : $billingAddress->getLastName(); |
178
|
|
|
$payerEmail = $customer->getEmail() !== null ? $customer->getEmail() : null; |
179
|
|
|
$payerPhoneNumber = $customer->getPhoneNumber() !== null ? $customer->getPhoneNumber() : $billingAddress->getPhoneNumber(); |
180
|
|
|
|
181
|
|
|
if ($payerFirstName !== null) { |
182
|
|
|
$payer->__set('name', $payerFirstName); |
183
|
|
|
} |
184
|
|
|
if ($payerLastName !== null) { |
185
|
|
|
$payer->__set('surname', $payerLastName); |
186
|
|
|
} |
187
|
|
|
if ($payerEmail !== null) { |
188
|
|
|
$payer->__set('email', $payerEmail); |
189
|
|
|
} |
190
|
|
|
if ($payerPhoneNumber !== null) { |
191
|
|
|
$payer->__set('phone', [ |
192
|
|
|
'number' => $payerPhoneNumber |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$payer->__set('address', [ |
197
|
|
|
'street_name' => $billingAddress->getStreet(), |
198
|
|
|
'zip_code' => $billingAddress->getPostcode(), |
199
|
|
|
]); |
200
|
|
|
|
201
|
|
|
$preference->__set('payer', $payer); |
202
|
|
|
|
203
|
|
|
$preference->__set('external_reference', $order->getNumber()); |
204
|
|
|
|
205
|
|
|
$preference->__set('back_urls', [ |
206
|
|
|
'success' => $request->getToken()->getAfterUrl(), |
207
|
|
|
'failure' => $request->getToken()->getAfterUrl(), |
208
|
|
|
'pending' => $request->getToken()->getAfterUrl() |
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
// Create notification url with Notify Token |
212
|
|
|
$notifyToken = $this->tokenFactory->createNotifyToken( |
213
|
|
|
$request->getToken()->getGatewayName(), |
214
|
|
|
$request->getToken()->getDetails() |
215
|
|
|
); |
216
|
|
|
$preference->__set('notification_url', $notifyToken->getTargetUrl().'?source_news=webhooks'); |
217
|
|
|
|
218
|
|
|
$preference->__set('auto_return', 'all'); |
219
|
|
|
|
220
|
|
|
$status = 400; |
221
|
|
|
$message = 'KO'; |
222
|
|
|
$preferenceData = null; |
223
|
|
|
|
224
|
|
|
if ($preference->save() === true) { |
225
|
|
|
$status = 200; |
226
|
|
|
$message = 'Preference created!'; |
227
|
|
|
$preferenceData = $preference->toArray(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$response = [ |
231
|
|
|
'status' => $status, |
232
|
|
|
'message' => $message, |
233
|
|
|
'preference' => $preferenceData |
234
|
|
|
]; |
235
|
|
|
} catch (\Exception $exception) { |
236
|
|
|
$response = [ |
237
|
|
|
'status' => $exception->getCode(), |
238
|
|
|
'message' => $exception->getMessage(), |
239
|
|
|
'preference' => null |
240
|
|
|
]; |
241
|
|
|
} finally { |
242
|
|
|
$payment->setDetails($response); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if ($response['status'] === 200) { |
246
|
|
|
$initPoint = $this->api->isSandbox() |
247
|
|
|
? $preference->__get('sandbox_init_point') |
248
|
|
|
: $preference->__get('init_point') |
249
|
|
|
; |
250
|
|
|
|
251
|
|
|
throw new HttpRedirect($initPoint); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function supports($request): bool |
260
|
|
|
{ |
261
|
|
|
return |
262
|
|
|
$request instanceof Capture && |
263
|
|
|
$request->getModel() instanceof SyliusPaymentInterface |
264
|
|
|
; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
|
|
public function setApi($api): void |
271
|
|
|
{ |
272
|
|
|
if (!$api instanceof MercadoPagoApi) { |
273
|
|
|
throw new UnsupportedApiException('Not supported. Expected an instance of ' . MercadoPagoApi::class); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$this->api = $api; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|