1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Metaways Infosystems GmbH, 2012 |
||
6 | * @copyright Aimeos (aimeos.org), 2015-2018 |
||
7 | * @package MShop |
||
8 | * @subpackage Service |
||
9 | */ |
||
10 | |||
11 | |||
12 | namespace Aimeos\MShop\Service\Provider\Payment; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Payment provider for paypal express orders. |
||
17 | * |
||
18 | * @package MShop |
||
19 | * @subpackage Service |
||
20 | */ |
||
21 | class PayPalExpress |
||
22 | extends \Aimeos\MShop\Service\Provider\Payment\Base |
||
23 | implements \Aimeos\MShop\Service\Provider\Payment\Iface |
||
24 | { |
||
25 | private $apiendpoint; |
||
26 | |||
27 | private $beConfig = array( |
||
28 | 'paypalexpress.ApiUsername' => array( |
||
29 | 'code' => 'paypalexpress.ApiUsername', |
||
30 | 'internalcode' => 'paypalexpress.ApiUsername', |
||
31 | 'label' => 'Username', |
||
32 | 'type' => 'string', |
||
33 | 'internaltype' => 'string', |
||
34 | 'default' => '', |
||
35 | 'required' => true, |
||
36 | ), |
||
37 | 'paypalexpress.AccountEmail' => array( |
||
38 | 'code' => 'paypalexpress.AccountEmail', |
||
39 | 'internalcode' => 'paypalexpress.AccountEmail', |
||
40 | 'label' => 'Registered e-mail address of the shop owner in PayPal', |
||
41 | 'type' => 'string', |
||
42 | 'internaltype' => 'string', |
||
43 | 'default' => '', |
||
44 | 'required' => true, |
||
45 | ), |
||
46 | 'paypalexpress.ApiPassword' => array( |
||
47 | 'code' => 'paypalexpress.ApiPassword', |
||
48 | 'internalcode' => 'paypalexpress.ApiPassword', |
||
49 | 'label' => 'Password', |
||
50 | 'type' => 'string', |
||
51 | 'internaltype' => 'string', |
||
52 | 'default' => '', |
||
53 | 'required' => true, |
||
54 | ), |
||
55 | 'paypalexpress.ApiSignature' => array( |
||
56 | 'code' => 'paypalexpress.ApiSignature', |
||
57 | 'internalcode' => 'paypalexpress.ApiSignature', |
||
58 | 'label' => 'Signature', |
||
59 | 'type' => 'string', |
||
60 | 'internaltype' => 'string', |
||
61 | 'default' => '', |
||
62 | 'required' => true, |
||
63 | ), |
||
64 | 'paypalexpress.ApiEndpoint' => array( |
||
65 | 'code' => 'paypalexpress.ApiEndpoint', |
||
66 | 'internalcode' => 'paypalexpress.ApiEndpoint', |
||
67 | 'label' => 'APIEndpoint', |
||
68 | 'type' => 'string', |
||
69 | 'internaltype' => 'string', |
||
70 | 'default' => 'https://api-3t.paypal.com/nvp', |
||
71 | 'required' => false, |
||
72 | ), |
||
73 | 'paypalexpress.PaypalUrl' => array( |
||
74 | 'code' => 'paypalexpress.PaypalUrl', |
||
75 | 'internalcode' => 'paypalexpress.PaypalUrl', |
||
76 | 'label' => 'PaypalUrl', |
||
77 | 'type' => 'string', |
||
78 | 'internaltype' => 'string', |
||
79 | 'default' => 'https://www.paypal.com/webscr&cmd=_express-checkout&useraction=commit&token=%1$s', |
||
80 | 'required' => false, |
||
81 | ), |
||
82 | 'paypalexpress.PaymentAction' => array( |
||
83 | 'code' => 'paypalexpress.PaymentAction', |
||
84 | 'internalcode' => 'paypalexpress.PaymentAction', |
||
85 | 'label' => 'PaymentAction', |
||
86 | 'type' => 'string', |
||
87 | 'internaltype' => 'string', |
||
88 | 'default' => 'Sale', |
||
89 | 'required' => false, |
||
90 | ), |
||
91 | 'paypalexpress.LandingPage' => array( |
||
92 | 'code' => 'paypalexpress.LandingPage', |
||
93 | 'internalcode' => 'paypalexpress.LandingPage', |
||
94 | 'label' => 'Landing page', |
||
95 | 'type' => 'string', |
||
96 | 'internaltype' => 'string', |
||
97 | 'default' => 'Login', |
||
98 | 'required' => false, |
||
99 | ), |
||
100 | 'paypalexpress.FundingSource' => array( |
||
101 | 'code' => 'paypalexpress.FundingSource', |
||
102 | 'internalcode' => 'paypalexpress.FundingSource', |
||
103 | 'label' => 'Funding source', |
||
104 | 'type' => 'string', |
||
105 | 'internaltype' => 'string', |
||
106 | 'default' => 'CreditCard', |
||
107 | 'required' => false, |
||
108 | ), |
||
109 | 'paypalexpress.AddrOverride' => array( |
||
110 | 'code' => 'paypalexpress.AddrOverride', |
||
111 | 'internalcode' => 'paypalexpress.AddrOverride', |
||
112 | 'label' => 'Customer can change address', |
||
113 | 'type' => 'boolean', |
||
114 | 'internaltype' => 'integer', |
||
115 | 'default' => 0, |
||
116 | 'required' => false, |
||
117 | ), |
||
118 | 'paypalexpress.NoShipping' => array( |
||
119 | 'code' => 'paypalexpress.NoShipping', |
||
120 | 'internalcode' => 'paypalexpress.NoShipping', |
||
121 | 'label' => 'Don\'t display shipping address', |
||
122 | 'type' => 'boolean', |
||
123 | 'internaltype' => 'integer', |
||
124 | 'default' => 1, |
||
125 | 'required' => false, |
||
126 | ), |
||
127 | 'paypalexpress.address' => array( |
||
128 | 'code' => 'paypalexpress.address', |
||
129 | 'internalcode' => 'paypalexpress.address', |
||
130 | 'label' => 'Pass customer address to PayPal', |
||
131 | 'type' => 'boolean', |
||
132 | 'internaltype' => 'integer', |
||
133 | 'default' => 1, |
||
134 | 'required' => false, |
||
135 | ), |
||
136 | 'paypalexpress.product' => array( |
||
137 | 'code' => 'paypalexpress.product', |
||
138 | 'internalcode' => 'paypalexpress.product', |
||
139 | 'label' => 'Pass product details to PayPal', |
||
140 | 'type' => 'boolean', |
||
141 | 'internaltype' => 'integer', |
||
142 | 'default' => 1, |
||
143 | 'required' => false, |
||
144 | ), |
||
145 | 'paypalexpress.service' => array( |
||
146 | 'code' => 'paypalexpress.service', |
||
147 | 'internalcode' => 'paypalexpress.service', |
||
148 | 'label' => 'Pass delivery/payment details to PayPal', |
||
149 | 'type' => 'boolean', |
||
150 | 'internaltype' => 'integer', |
||
151 | 'default' => 1, |
||
152 | 'required' => false, |
||
153 | ), |
||
154 | 'paypalexpress.url-validate' => array( |
||
155 | 'code' => 'paypalexpress.url-validate', |
||
156 | 'internalcode' => 'paypalexpress.url-validate', |
||
157 | 'label' => 'Validation URL', |
||
158 | 'type' => 'string', |
||
159 | 'internaltype' => 'string', |
||
160 | 'default' => 'https://www.paypal.com/webscr&cmd=_notify-validate', |
||
161 | 'required' => false, |
||
162 | ), |
||
163 | ); |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Initializes the provider object. |
||
168 | * |
||
169 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
170 | * @param \Aimeos\MShop\Service\Item\Iface $serviceItem Service item with configuration |
||
171 | * @throws \Aimeos\MShop\Service\Exception If one of the required configuration values isn't available |
||
172 | */ |
||
173 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Service\Item\Iface $serviceItem ) |
||
174 | { |
||
175 | parent::__construct( $context, $serviceItem ); |
||
176 | |||
177 | $default = 'https://api-3t.paypal.com/nvp'; |
||
178 | $this->apiendpoint = $this->getConfigValue( array( 'paypalexpress.ApiEndpoint' ), $default ); |
||
179 | } |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
184 | * rules for the value of each field in the administration interface. |
||
185 | * |
||
186 | * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface |
||
187 | */ |
||
188 | public function getConfigBE() |
||
189 | { |
||
190 | return $this->getConfigItems( $this->beConfig ); |
||
191 | } |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Checks the backend configuration attributes for validity. |
||
196 | * |
||
197 | * @param array $attributes Attributes added by the shop owner in the administraton interface |
||
198 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
199 | * known by the provider but aren't valid |
||
200 | */ |
||
201 | public function checkConfigBE( array $attributes ) |
||
202 | { |
||
203 | $errors = parent::checkConfigBE( $attributes ); |
||
204 | |||
205 | return array_merge( $errors, $this->checkConfig( $this->beConfig, $attributes ) ); |
||
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * Tries to get an authorization or captures the money immediately for the given order if capturing the money |
||
211 | * separately isn't supported or not configured by the shop owner. |
||
212 | * |
||
213 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
214 | * @param array $params Request parameter if available |
||
215 | * @return \Aimeos\MShop\Common\Item\Helper\Form\Standard Form object with URL, action and parameters to redirect to |
||
216 | * (e.g. to an external server of the payment provider or to a local success page) |
||
217 | */ |
||
218 | public function process( \Aimeos\MShop\Order\Item\Iface $order, array $params = [] ) |
||
219 | { |
||
220 | $orderBaseItem = $this->getOrderBase( $order->getBaseId(), \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL ); |
||
221 | |||
222 | $values = $this->getOrderDetails( $orderBaseItem ); |
||
223 | $values['METHOD'] = 'SetExpressCheckout'; |
||
224 | $values['PAYMENTREQUEST_0_INVNUM'] = $order->getId(); |
||
225 | $values['RETURNURL'] = $this->getConfigValue( array( 'payment.url-success' ) ); |
||
226 | $values['CANCELURL'] = $this->getConfigValue( array( 'payment.url-cancel', 'payment.url-success' ) ); |
||
227 | $values['USERSELECTEDFUNDINGSOURCE'] = $this->getConfigValue( array( 'paypalexpress.FundingSource' ), 'CreditCard' ); |
||
228 | $values['LANDINGPAGE'] = $this->getConfigValue( array( 'paypalexpress.LandingPage' ), 'Login' ); |
||
229 | |||
230 | $urlQuery = http_build_query( $values, '', '&' ); |
||
231 | $response = $this->send( $this->apiendpoint, 'POST', $urlQuery ); |
||
232 | $rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ ); |
||
233 | |||
234 | $default = 'https://www.paypal.com/webscr&cmd=_express-checkout&useraction=commit&token=%1$s'; |
||
235 | $paypalUrl = sprintf( $this->getConfigValue( array( 'paypalexpress.PaypalUrl' ), $default ), $rvals['TOKEN'] ); |
||
236 | |||
237 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
238 | $serviceItem = $orderBaseItem->getService( $type, $this->getServiceItem()->getCode() ); |
||
239 | $this->setAttributes( $serviceItem, ['TOKEN' => $rvals['TOKEN']], 'payment/paypal' ); |
||
240 | $this->saveOrderBase( $orderBaseItem ); |
||
241 | |||
242 | return new \Aimeos\MShop\Common\Item\Helper\Form\Standard( $paypalUrl, 'POST', [] ); |
||
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * Queries for status updates for the given order if supported. |
||
248 | * |
||
249 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
250 | */ |
||
251 | public function query( \Aimeos\MShop\Order\Item\Iface $order ) |
||
252 | { |
||
253 | if( ( $tid = $this->getOrderServiceItem( $order->getBaseId() )->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null ) |
||
254 | { |
||
255 | $msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() ); |
||
256 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
257 | } |
||
258 | |||
259 | $values = $this->getAuthParameter(); |
||
260 | $values['METHOD'] = 'GetTransactionDetails'; |
||
261 | $values['TRANSACTIONID'] = $tid; |
||
262 | |||
263 | $urlQuery = http_build_query( $values, '', '&' ); |
||
264 | $response = $this->send( $this->apiendpoint, 'POST', $urlQuery ); |
||
265 | $rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ ); |
||
266 | |||
267 | $this->setPaymentStatus( $order, $rvals ); |
||
268 | $this->saveOrder( $order ); |
||
269 | } |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Captures the money later on request for the given order if supported. |
||
274 | * |
||
275 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
276 | */ |
||
277 | public function capture( \Aimeos\MShop\Order\Item\Iface $order ) |
||
278 | { |
||
279 | $baseItem = $this->getOrderBase( $order->getBaseId() ); |
||
280 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
281 | $serviceItem = $baseItem->getService( $type, $this->getServiceItem()->getCode() ); |
||
282 | |||
283 | if( ( $tid = $serviceItem->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null ) |
||
284 | { |
||
285 | $msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() ); |
||
286 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
287 | } |
||
288 | |||
289 | $price = $baseItem->getPrice(); |
||
290 | |||
291 | $values = $this->getAuthParameter(); |
||
292 | $values['METHOD'] = 'DoCapture'; |
||
293 | $values['COMPLETETYPE'] = 'Complete'; |
||
294 | $values['AUTHORIZATIONID'] = $tid; |
||
295 | $values['INVNUM'] = $order->getId(); |
||
296 | $values['CURRENCYCODE'] = $price->getCurrencyId(); |
||
297 | $values['AMT'] = $this->getAmount( $price ); |
||
298 | |||
299 | $urlQuery = http_build_query( $values, '', '&' ); |
||
300 | $response = $this->send( $this->apiendpoint, 'POST', $urlQuery ); |
||
301 | $rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ ); |
||
302 | |||
303 | $this->setPaymentStatus( $order, $rvals ); |
||
304 | |||
305 | $attributes = []; |
||
306 | if( isset( $rvals['PARENTTRANSACTIONID'] ) ) { |
||
307 | $attributes['PARENTTRANSACTIONID'] = $rvals['PARENTTRANSACTIONID']; |
||
308 | } |
||
309 | |||
310 | //updates the transaction id |
||
311 | $attributes['TRANSACTIONID'] = $rvals['TRANSACTIONID']; |
||
312 | $this->setAttributes( $serviceItem, $attributes, 'payment/paypal' ); |
||
313 | |||
314 | $this->saveOrderBase( $baseItem ); |
||
315 | $this->saveOrder( $order ); |
||
316 | } |
||
317 | |||
318 | |||
319 | /** |
||
320 | * Refunds the money for the given order if supported. |
||
321 | * |
||
322 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
323 | */ |
||
324 | public function refund( \Aimeos\MShop\Order\Item\Iface $order ) |
||
325 | { |
||
326 | $baseItem = $this->getOrderBase( $order->getBaseId() ); |
||
327 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
328 | $serviceItem = $baseItem->getService( $type, $this->getServiceItem()->getCode() ); |
||
329 | |||
330 | if( ( $tid = $serviceItem->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null ) |
||
331 | { |
||
332 | $msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() ); |
||
333 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
334 | } |
||
335 | |||
336 | $values = $this->getAuthParameter(); |
||
337 | $values['METHOD'] = 'RefundTransaction'; |
||
338 | $values['REFUNDSOURCE'] = 'instant'; |
||
339 | $values['REFUNDTYPE'] = 'Full'; |
||
340 | $values['TRANSACTIONID'] = $tid; |
||
341 | $values['INVOICEID'] = $order->getId(); |
||
342 | |||
343 | $urlQuery = http_build_query( $values, '', '&' ); |
||
344 | $response = $this->send( $this->apiendpoint, 'POST', $urlQuery ); |
||
345 | $rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ ); |
||
346 | |||
347 | $attributes = array( 'REFUNDTRANSACTIONID' => $rvals['REFUNDTRANSACTIONID'] ); |
||
348 | $this->setAttributes( $serviceItem, $attributes, 'payment/paypal' ); |
||
349 | $this->saveOrderBase( $baseItem ); |
||
350 | |||
351 | $order->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUND ); |
||
352 | $this->saveOrder( $order ); |
||
353 | } |
||
354 | |||
355 | |||
356 | /** |
||
357 | * Cancels the authorization for the given order if supported. |
||
358 | * |
||
359 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
360 | */ |
||
361 | public function cancel( \Aimeos\MShop\Order\Item\Iface $order ) |
||
362 | { |
||
363 | if( ( $tid = $this->getOrderServiceItem( $order->getBaseId() )->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null ) |
||
364 | { |
||
365 | $msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() ); |
||
366 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
367 | } |
||
368 | |||
369 | $values = $this->getAuthParameter(); |
||
370 | $values['METHOD'] = 'DoVoid'; |
||
371 | $values['AUTHORIZATIONID'] = $tid; |
||
372 | |||
373 | $urlQuery = http_build_query( $values, '', '&' ); |
||
374 | $response = $this->send( $this->apiendpoint, 'POST', $urlQuery ); |
||
375 | $this->checkResponse( $order->getId(), $response, __METHOD__ ); |
||
376 | |||
377 | $order->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED ); |
||
378 | $this->saveOrder( $order ); |
||
379 | } |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Updates the order status sent by payment gateway notifications |
||
384 | * |
||
385 | * @param \Psr\Http\Message\ServerRequestInterface Request object |
||
386 | * @return \Psr\Http\Message\ResponseInterface Response object |
||
387 | */ |
||
388 | public function updatePush( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response ) |
||
389 | { |
||
390 | $params = $request->getQueryParams(); |
||
391 | |||
392 | if( !isset( $params['txn_id'] ) ) { //tid from ipn |
||
393 | return $response->withStatus( 400, 'PayPal Express: Parameter "txn_id" is missing' ); |
||
394 | } |
||
395 | |||
396 | $urlQuery = http_build_query( $params, '', '&' ); |
||
397 | |||
398 | //validation |
||
399 | $result = $this->send( $this->getConfigValue( array( 'paypalexpress.url-validate' ) ), 'POST', $urlQuery ); |
||
400 | |||
401 | if( $result !== 'VERIFIED' ) { |
||
402 | return $response->withStatus( 400, sprintf( 'PayPal Express: Invalid request "%1$s"', $urlQuery ) ); |
||
403 | } |
||
404 | |||
405 | |||
406 | $order = $this->getOrder( $params['invoice'] ); |
||
407 | $baseItem = $this->getOrderBase( $order->getBaseId() ); |
||
408 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
409 | $serviceItem = $baseItem->getService( $type, $this->getServiceItem()->getCode() ); |
||
410 | |||
411 | $this->checkIPN( $baseItem, $params ); |
||
412 | |||
413 | $status = array( 'PAYMENTSTATUS' => $params['payment_status'] ); |
||
414 | |||
415 | if( isset( $params['pending_reason'] ) ) { |
||
416 | $status['PENDINGREASON'] = $params['pending_reason']; |
||
417 | } |
||
418 | |||
419 | $this->setAttributes( $serviceItem, array( $params['txn_id'] => $params['payment_status'] ), 'payment/paypal/txn' ); |
||
420 | $this->setAttributes( $serviceItem, array( 'TRANSACTIONID' => $params['txn_id'] ), 'payment/paypal' ); |
||
421 | $this->saveOrderBase( $baseItem ); |
||
422 | |||
423 | $this->setPaymentStatus( $order, $status ); |
||
424 | $this->saveOrder( $order ); |
||
425 | |||
426 | return $response->withStatus( 200 ); |
||
427 | } |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Updates the orders for whose status updates have been received by the confirmation page |
||
432 | * |
||
433 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object with parameters and request body |
||
434 | * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item that should be updated |
||
435 | * @return \Aimeos\MShop\Order\Item\Iface Updated order item |
||
436 | * @throws \Aimeos\MShop\Service\Exception If updating the orders failed |
||
437 | */ |
||
438 | public function updateSync( \Psr\Http\Message\ServerRequestInterface $request, \Aimeos\MShop\Order\Item\Iface $orderItem ) |
||
439 | { |
||
440 | $params = (array) $request->getAttributes() + (array) $request->getParsedBody() + (array) $request->getQueryParams(); |
||
441 | |||
442 | if( !isset( $params['token'] ) || !isset( $params['PayerID'] ) ) { |
||
443 | throw new \Aimeos\MShop\Service\Exception( 'Parameter "token" or "PayerID" missing' ); |
||
444 | } |
||
445 | |||
446 | $baseItem = $this->getOrderBase( $orderItem->getBaseId() ); |
||
447 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
448 | $serviceItem = $baseItem->getService( $type, $this->getServiceItem()->getCode() ); |
||
449 | |||
450 | $price = $baseItem->getPrice(); |
||
451 | |||
452 | $values = $this->getAuthParameter(); |
||
453 | $values['METHOD'] = 'DoExpressCheckoutPayment'; |
||
454 | $values['TOKEN'] = $params['token']; |
||
455 | $values['PAYERID'] = $params['PayerID']; |
||
456 | $values['PAYMENTACTION'] = $this->getConfigValue( array( 'paypalexpress.PaymentAction' ), 'Sale' ); |
||
457 | $values['CURRENCYCODE'] = $price->getCurrencyId(); |
||
458 | $values['AMT'] = $this->getAmount( $price ); |
||
459 | |||
460 | $urlQuery = http_build_query( $values, '', '&' ); |
||
461 | $response = $this->send( $this->apiendpoint, 'POST', $urlQuery ); |
||
462 | $rvals = $this->checkResponse( $orderItem->getId(), $response, __METHOD__ ); |
||
463 | |||
464 | $attributes = array( 'PAYERID' => $params['PayerID'] ); |
||
465 | |||
466 | if( isset( $rvals['TRANSACTIONID'] ) ) |
||
467 | { |
||
468 | $attributes['TRANSACTIONID'] = $rvals['TRANSACTIONID']; |
||
469 | $this->setAttributes( $serviceItem, array( $rvals['TRANSACTIONID'] => $rvals['PAYMENTSTATUS'] ), 'payment/paypal/txn' ); |
||
470 | } |
||
471 | |||
472 | $this->setAttributes( $serviceItem, $attributes, 'payment/paypal' ); |
||
473 | $this->saveOrderBase( $baseItem ); |
||
474 | |||
475 | $this->setPaymentStatus( $orderItem, $rvals ); |
||
476 | $this->saveOrder( $orderItem ); |
||
477 | |||
478 | return $orderItem; |
||
479 | } |
||
480 | |||
481 | |||
482 | /** |
||
483 | * Checks what features the payment provider implements. |
||
484 | * |
||
485 | * @param integer $what Constant from abstract class |
||
486 | * @return boolean True if feature is available in the payment provider, false if not |
||
487 | */ |
||
488 | public function isImplemented( $what ) |
||
489 | { |
||
490 | switch( $what ) |
||
491 | { |
||
492 | case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CAPTURE: |
||
493 | case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY: |
||
494 | case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CANCEL: |
||
495 | case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_REFUND: |
||
496 | return true; |
||
497 | } |
||
498 | |||
499 | return false; |
||
500 | } |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Checks the response from the payment server. |
||
505 | * |
||
506 | * @param string $orderid Order item ID |
||
507 | * @param string $response Response from the payment provider |
||
508 | * @param string $method Name of the calling method |
||
509 | * @return array Associative list of key/value pairs containing the response parameters |
||
510 | * @throws \Aimeos\MShop\Service\Exception If request was not successful and an error was returned |
||
511 | */ |
||
512 | protected function checkResponse( $orderid, $response, $method ) |
||
513 | { |
||
514 | $rvals = []; |
||
515 | parse_str( $response, $rvals ); |
||
516 | |||
517 | if( $rvals['ACK'] !== 'Success' ) |
||
518 | { |
||
519 | $msg = 'PayPal Express: method = ' . $method . ', order ID = ' . $orderid . ', response = ' . print_r( $rvals, true ); |
||
520 | $this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::WARN, 'core/service/payment' ); |
||
521 | |||
522 | if( $rvals['ACK'] !== 'SuccessWithWarning' ) |
||
523 | { |
||
524 | $short = ( isset( $rvals['L_SHORTMESSAGE0'] ) ? $rvals['L_SHORTMESSAGE0'] : '<none>' ); |
||
525 | $msg = sprintf( 'PayPal Express: Request for order ID "%1$s" failed with "%2$s"', $orderid, $short ); |
||
526 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
527 | } |
||
528 | } |
||
529 | |||
530 | return $rvals; |
||
531 | } |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Checks if IPN message from paypal is valid. |
||
536 | * |
||
537 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket |
||
538 | * @param array $params |
||
539 | */ |
||
540 | protected function checkIPN( $basket, $params ) |
||
541 | { |
||
542 | $attrManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order/base/service/attribute' ); |
||
543 | |||
544 | if( $this->getConfigValue( array( 'paypalexpress.AccountEmail' ) ) !== $params['receiver_email'] ) |
||
545 | { |
||
546 | $msg = sprintf( 'PayPal Express: Wrong receiver email "%1$s"', $params['receiver_email'] ); |
||
547 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
548 | } |
||
549 | |||
550 | $price = $basket->getPrice(); |
||
551 | |||
552 | if( $this->getAmount( $price ) != $params['payment_amount'] ) |
||
553 | { |
||
554 | $msg = sprintf( 'PayPal Express: Wrong payment amount "%1$s" for order ID "%2$s"', $params['payment_amount'], $params['invoice'] ); |
||
555 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
556 | } |
||
557 | |||
558 | $search = $attrManager->createSearch(); |
||
559 | $expr = array( |
||
560 | $search->compare( '==', 'order.base.service.attribute.code', $params['txn_id'] ), |
||
561 | $search->compare( '==', 'order.base.service.attribute.value', $params['payment_status'] ), |
||
562 | ); |
||
563 | |||
564 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
565 | $results = $attrManager->searchItems( $search ); |
||
566 | |||
567 | if( ( $attr = reset( $results ) ) !== false ) |
||
568 | { |
||
569 | $msg = sprintf( 'PayPal Express: Duplicate transaction with ID "%1$s" and status "%2$s" ', $params['txn_id'], $params['txn_status'] ); |
||
570 | throw new \Aimeos\MShop\Service\Exception( $msg ); |
||
571 | } |
||
572 | } |
||
573 | |||
574 | |||
575 | /** |
||
576 | * Maps the PayPal status to the appropriate payment status and sets it in the order object. |
||
577 | * |
||
578 | * @param \Aimeos\MShop\Order\Item\Iface $invoice Order invoice object |
||
579 | * @param array $response Associative list of key/value pairs containing the PayPal response |
||
580 | */ |
||
581 | protected function setPaymentStatus( \Aimeos\MShop\Order\Item\Iface $invoice, array $response ) |
||
582 | { |
||
583 | if( !isset( $response['PAYMENTSTATUS'] ) ) { |
||
584 | return; |
||
585 | } |
||
586 | |||
587 | switch( $response['PAYMENTSTATUS'] ) |
||
588 | { |
||
589 | case 'Pending': |
||
590 | if( isset( $response['PENDINGREASON'] ) ) |
||
591 | { |
||
592 | if( $response['PENDINGREASON'] === 'authorization' ) |
||
593 | { |
||
594 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED ); |
||
595 | break; |
||
596 | } |
||
597 | |||
598 | $str = 'PayPal Express: order ID = ' . $invoice->getId() . ', PENDINGREASON = ' . $response['PENDINGREASON']; |
||
599 | $this->getContext()->getLogger()->log( $str, \Aimeos\MW\Logger\Base::INFO, 'core/service/payment' ); |
||
600 | } |
||
601 | |||
602 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_PENDING ); |
||
603 | break; |
||
604 | |||
605 | case 'In-Progress': |
||
606 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_PENDING ); |
||
607 | break; |
||
608 | |||
609 | case 'Completed': |
||
610 | case 'Processed': |
||
611 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED ); |
||
612 | break; |
||
613 | |||
614 | case 'Failed': |
||
615 | case 'Denied': |
||
616 | case 'Expired': |
||
617 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUSED ); |
||
618 | break; |
||
619 | |||
620 | case 'Refunded': |
||
621 | case 'Partially-Refunded': |
||
622 | case 'Reversed': |
||
623 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUND ); |
||
624 | break; |
||
625 | |||
626 | case 'Canceled-Reversal': |
||
627 | case 'Voided': |
||
628 | $invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED ); |
||
629 | break; |
||
630 | |||
631 | default: |
||
632 | $str = 'PayPal Express: order ID = ' . $invoice->getId() . ', response = ' . print_r( $response, true ); |
||
633 | $this->getContext()->getLogger()->log( $str, \Aimeos\MW\Logger\Base::INFO, 'core/service/payment' ); |
||
634 | } |
||
635 | } |
||
636 | |||
637 | |||
638 | /** |
||
639 | * Returns an list of order data required by PayPal. |
||
640 | * |
||
641 | * @param \Aimeos\MShop\Order\Item\Base\Iface $orderBase Order base item |
||
642 | * @return array Associative list of key/value pairs with order data required by PayPal |
||
643 | */ |
||
644 | protected function getOrderDetails( \Aimeos\MShop\Order\Item\Base\Iface $orderBase ) |
||
645 | { |
||
646 | $lastPos = 0; |
||
647 | $deliveryCosts = 0; |
||
648 | $deliveryPrices = []; |
||
649 | $values = $this->getAuthParameter(); |
||
650 | |||
651 | |||
652 | if( $this->getConfigValue( 'paypalexpress.address', true ) ) |
||
653 | { |
||
654 | try |
||
655 | { |
||
656 | $orderAddressDelivery = $orderBase->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT ); |
||
657 | |||
658 | /* setting up the address details */ |
||
659 | $values['NOSHIPPING'] = $this->getConfigValue( array( 'paypalexpress.NoShipping' ), 1 ); |
||
660 | $values['ADDROVERRIDE'] = $this->getConfigValue( array( 'paypalexpress.AddrOverride' ), 0 ); |
||
661 | $values['PAYMENTREQUEST_0_SHIPTONAME'] = $orderAddressDelivery->getFirstName() . ' ' . $orderAddressDelivery->getLastName(); |
||
662 | $values['PAYMENTREQUEST_0_SHIPTOSTREET'] = $orderAddressDelivery->getAddress1() . ' ' . $orderAddressDelivery->getAddress2() . ' ' . $orderAddressDelivery->getAddress3(); |
||
663 | $values['PAYMENTREQUEST_0_SHIPTOCITY'] = $orderAddressDelivery->getCity(); |
||
664 | $values['PAYMENTREQUEST_0_SHIPTOSTATE'] = $orderAddressDelivery->getState(); |
||
665 | $values['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $orderAddressDelivery->getCountryId(); |
||
666 | $values['PAYMENTREQUEST_0_SHIPTOZIP'] = $orderAddressDelivery->getPostal(); |
||
667 | } |
||
668 | catch( \Exception $e ) { ; } // If no address is available |
||
669 | } |
||
670 | |||
671 | |||
672 | if( $this->getConfigValue( 'paypalexpress.product', true ) ) |
||
673 | { |
||
674 | foreach( $orderBase->getProducts() as $product ) |
||
675 | { |
||
676 | $price = $product->getPrice(); |
||
677 | $lastPos = $product->getPosition() - 1; |
||
678 | |||
679 | $deliveryPrice = clone $price; |
||
680 | $deliveryPrices = $this->addPrice( $deliveryPrices, $deliveryPrice->setValue( '0.00' ), $product->getQuantity() ); |
||
681 | |||
682 | $values['L_PAYMENTREQUEST_0_NUMBER' . $lastPos] = $product->getId(); |
||
683 | $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $product->getName(); |
||
684 | $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = $product->getQuantity(); |
||
685 | $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $this->getAmount( $price, false ); |
||
686 | } |
||
687 | } |
||
688 | |||
689 | |||
690 | if( $this->getConfigValue( 'paypalexpress.service', true ) ) |
||
691 | { |
||
692 | foreach( $orderBase->getService( 'payment' ) as $service ) |
||
693 | { |
||
694 | $price = $service->getPrice(); |
||
695 | |||
696 | if( ( $paymentCosts = $this->getAmount( $price ) ) > '0.00' ) |
||
697 | { |
||
698 | $lastPos++; |
||
699 | $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $this->getContext()->getI18n()->dt( 'mshop', 'Payment costs' ); |
||
700 | $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1'; |
||
701 | $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $paymentCosts; |
||
702 | } |
||
703 | } |
||
704 | |||
705 | try |
||
706 | { |
||
707 | foreach( $orderBase->getService( 'delivery' ) as $service ) |
||
708 | { |
||
709 | $deliveryPrices = $this->addPrice( $deliveryPrices, $service->getPrice() ); |
||
710 | |||
711 | foreach( $deliveryPrices as $priceItem ) { |
||
712 | $deliveryCosts += $this->getAmount( $priceItem ); |
||
713 | } |
||
714 | |||
715 | $values['L_SHIPPINGOPTIONAMOUNT0'] = number_format( $deliveryCosts, 2, '.', '' ); |
||
716 | $values['L_SHIPPINGOPTIONLABEL0'] = $service->getCode(); |
||
717 | $values['L_SHIPPINGOPTIONNAME0'] = $service->getName(); |
||
718 | $values['L_SHIPPINGOPTIONISDEFAULT0'] = 'true'; |
||
719 | } |
||
720 | } |
||
721 | catch( \Exception $e ) { ; } // If no delivery service is available |
||
722 | } |
||
723 | |||
724 | |||
725 | $price = $orderBase->getPrice(); |
||
726 | $amount = $this->getAmount( $price ); |
||
727 | |||
728 | if( $deliveryCosts === 0 ) |
||
729 | { |
||
730 | foreach( $deliveryPrices as $priceItem ) { |
||
731 | $deliveryCosts += $this->getAmount( $priceItem ); |
||
732 | } |
||
733 | } |
||
734 | |||
735 | $values['MAXAMT'] = $amount + 0.01; // possible rounding error |
||
736 | $values['PAYMENTREQUEST_0_AMT'] = $amount; |
||
737 | $values['PAYMENTREQUEST_0_ITEMAMT'] = number_format( $amount - $deliveryCosts, 2, '.', '' ); |
||
738 | $values['PAYMENTREQUEST_0_SHIPPINGAMT'] = number_format( $deliveryCosts, 2, '.', '' ); |
||
739 | $values['PAYMENTREQUEST_0_INSURANCEAMT'] = '0.00'; |
||
740 | $values['PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED'] = 'false'; |
||
741 | $values['PAYMENTREQUEST_0_SHIPDISCAMT'] = '0.00'; |
||
742 | $values['PAYMENTREQUEST_0_CURRENCYCODE'] = $orderBase->getPrice()->getCurrencyId(); |
||
743 | $values['PAYMENTREQUEST_0_PAYMENTACTION'] = $this->getConfigValue( array( 'paypalexpress.PaymentAction' ), 'sale' ); |
||
744 | |||
745 | return $values; |
||
746 | } |
||
747 | |||
748 | |||
749 | /** |
||
750 | * Returns the data required for authorization against the PayPal server. |
||
751 | * |
||
752 | * @return array Associative list of key/value pairs containing the autorization parameters |
||
753 | */ |
||
754 | protected function getAuthParameter() |
||
755 | { |
||
756 | return array( |
||
757 | 'VERSION' => '204.0', |
||
758 | 'SIGNATURE' => $this->getConfigValue( array( 'paypalexpress.ApiSignature' ) ), |
||
759 | 'USER' => $this->getConfigValue( array( 'paypalexpress.ApiUsername' ) ), |
||
760 | 'PWD' => $this->getConfigValue( array( 'paypalexpress.ApiPassword' ) ), |
||
761 | ); |
||
762 | } |
||
763 | |||
764 | |||
765 | /** |
||
766 | * Returns order service item for specified base ID. |
||
767 | * |
||
768 | * @param integer $baseid Base ID of the order |
||
769 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order service item |
||
770 | */ |
||
771 | protected function getOrderServiceItem( $baseid ) |
||
772 | { |
||
773 | $type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT; |
||
774 | $basket = $this->getOrderBase( $baseid, \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE ); |
||
775 | |||
776 | return $basket->getService( $type, $this->getServiceItem()->getCode() ); |
||
777 | } |
||
778 | |||
779 | |||
780 | /** |
||
781 | * Adds the costs to the price item with the corresponding tax rate |
||
782 | * |
||
783 | * @param \Aimeos\MShop\Price\Item\Iface[] $prices Associative list of tax rates as key and price items as value |
||
784 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item that should be added |
||
785 | * @param integer $quantity Product quantity |
||
786 | * @return \Aimeos\MShop\Price\Item\Iface[] Updated list of price items |
||
787 | */ |
||
788 | protected function addPrice( array $prices, $price, $quantity = 1 ) |
||
789 | { |
||
790 | $taxrate = $price->getTaxRate(); |
||
791 | |||
792 | if( !isset( $prices[$taxrate] ) ) |
||
793 | { |
||
794 | $prices[$taxrate] = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'price' )->createItem(); |
||
795 | $prices[$taxrate]->setTaxRate( $taxrate ); |
||
796 | } |
||
797 | |||
798 | $prices[$taxrate]->addItem( $price, $quantity ); |
||
799 | |||
800 | return $prices; |
||
801 | } |
||
802 | |||
803 | |||
804 | /** |
||
805 | * Sends request parameters to the providers interface. |
||
806 | * |
||
807 | * @param string $target Receivers address e.g. url. |
||
808 | * @param string $method Initial method (e.g. post or get) |
||
809 | * @param mixed $payload Update information whose format depends on the payment provider |
||
810 | * @return string response body of a http request |
||
811 | */ |
||
812 | public function send( $target, $method, $payload ) |
||
813 | { |
||
814 | $response = ''; |
||
815 | |||
816 | if( ( $curl = curl_init() )=== false ) { |
||
817 | throw new \Aimeos\MW\Communication\Exception( 'Could not initialize curl' ); |
||
0 ignored issues
–
show
|
|||
818 | } |
||
819 | |||
820 | try |
||
821 | { |
||
822 | curl_setopt( $curl, CURLOPT_URL, $target ); |
||
823 | |||
824 | curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, strtoupper( $method ) ); |
||
825 | curl_setopt( $curl, CURLOPT_POSTFIELDS, $payload ); |
||
826 | curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 25 ); |
||
827 | curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); // return data as string |
||
828 | |||
829 | curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true ); |
||
830 | |||
831 | if ( ( $response = curl_exec( $curl ) ) === false ) { |
||
832 | throw new \Aimeos\MW\Communication\Exception( sprintf( 'Sending order failed: "%1$s"', curl_error( $curl ) ) ); |
||
833 | } |
||
834 | |||
835 | if ( curl_errno($curl) ) { |
||
836 | throw new \Aimeos\MW\Communication\Exception( sprintf( 'Error with nr."%1$s" - "%2$s"', curl_errno($curl), curl_error($curl) ) ); |
||
837 | } |
||
838 | |||
839 | curl_close( $curl ); |
||
840 | } |
||
841 | catch( \Exception $e ) |
||
842 | { |
||
843 | curl_close( $curl ); |
||
844 | throw $e; |
||
845 | } |
||
846 | |||
847 | return $response; |
||
848 | } |
||
849 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths