Completed
Push — master ( d0dda4...003cf0 )
by Aimeos
09:43
created

PayPalExpress::getOrderDetails()   F

Complexity

Conditions 11
Paths 836

Size

Total Lines 98
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 60
nc 836
nop 1
dl 0
loc 98
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2016
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
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
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'=> 'integer',
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'=> 'integer',
123
			'internaltype'=> 'integer',
124
			'default'=> 1,
125
			'required'=> false,
126
		),
127
		'paypalexpress.url-validate' => array(
128
			'code' => 'paypalexpress.url-validate',
129
			'internalcode'=> 'paypalexpress.url-validate',
130
			'label'=> 'Validation URL',
131
			'type'=> 'string',
132
			'internaltype'=> 'string',
133
			'default'=> 'https://www.paypal.com/webscr&cmd=_notify-validate',
134
			'required'=> false,
135
		),
136
	);
137
138
139
	/**
140
	 * Initializes the provider object.
141
	 *
142
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
143
	 * @param \Aimeos\MShop\Service\Item\Iface $serviceItem Service item with configuration
144
	 * @throws \Aimeos\MShop\Service\Exception If one of the required configuration values isn't available
145
	 */
146
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Service\Item\Iface $serviceItem )
147
	{
148
		parent::__construct( $context, $serviceItem );
149
150
		$configParameters = array(
151
			'paypalexpress.AccountEmail',
152
			'paypalexpress.ApiUsername',
153
			'paypalexpress.ApiPassword',
154
			'paypalexpress.ApiSignature',
155
		);
156
157
		$config = $serviceItem->getConfig();
158
159
		foreach( $configParameters as $param )
160
		{
161
			if( !isset( $config[$param] ) ) {
162
				throw new \Aimeos\MShop\Service\Exception( sprintf( 'Configuration for "%1$s" is missing', $param ) );
163
			}
164
		}
165
166
		$default = 'https://api-3t.paypal.com/nvp';
167
		$this->apiendpoint = $this->getConfigValue( array( 'paypalexpress.ApiEndpoint' ), $default );
168
	}
169
170
171
	/**
172
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
173
	 * rules for the value of each field in the administration interface.
174
	 *
175
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
176
	 */
177
	public function getConfigBE()
178
	{
179
		return $this->getConfigItems( $this->beConfig );
180
	}
181
182
183
	/**
184
	 * Checks the backend configuration attributes for validity.
185
	 *
186
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
187
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
188
	 * 	known by the provider but aren't valid
189
	 */
190
	public function checkConfigBE( array $attributes )
191
	{
192
		$errors = parent::checkConfigBE( $attributes );
193
194
		return array_merge( $errors, $this->checkConfig( $this->beConfig, $attributes ) );
195
	}
196
197
198
	/**
199
	 * Tries to get an authorization or captures the money immediately for the given order if capturing the money
200
	 * separately isn't supported or not configured by the shop owner.
201
	 *
202
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object
203
	 * @param array $params Request parameter if available
204
	 * @return \Aimeos\MShop\Common\Item\Helper\Form\Standard Form object with URL, action and parameters to redirect to
205
	 * 	(e.g. to an external server of the payment provider or to a local success page)
206
	 */
207
	public function process( \Aimeos\MShop\Order\Item\Iface $order, array $params = [] )
208
	{
209
		$orderid = $order->getId();
210
		$orderBaseItem = $this->getOrderBase( $order->getBaseId(), \Aimeos\MShop\Order\Manager\Base\Base::PARTS_ALL );
211
212
		$values = $this->getOrderDetails( $orderBaseItem );
213
		$values['METHOD'] = 'SetExpressCheckout';
214
		$values['PAYMENTREQUEST_0_INVNUM'] = $orderid;
215
		$values['RETURNURL'] = $this->getConfigValue( array( 'payment.url-success' ) );
216
		$values['CANCELURL'] = $this->getConfigValue( array( 'payment.url-cancel', 'payment.url-success' ) );
217
		$values['USERSELECTEDFUNDINGSOURCE'] = $this->getConfigValue( array( 'paypalexpress.FundingSource' ), 'CreditCard' );
218
		$values['LANDINGPAGE'] = $this->getConfigValue( array( 'paypalexpress.LandingPage' ), 'Login' );
219
220
		$urlQuery = http_build_query( $values, '', '&' );
221
		$response = $this->getCommunication()->transmit( $this->apiendpoint, 'POST', $urlQuery );
222
		$rvals = $this->checkResponse( $orderid, $response, __METHOD__ );
223
224
		$default = 'https://www.paypal.com/webscr&cmd=_express-checkout&useraction=commit&token=%1$s';
225
		$paypalUrl = sprintf( $this->getConfigValue( array( 'paypalexpress.PaypalUrl' ), $default ), $rvals['TOKEN'] );
226
227
		$type = \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT;
228
		$this->setAttributes( $orderBaseItem->getService( $type ), array( 'TOKEN' => $rvals['TOKEN'] ), 'payment/paypal' );
229
		$this->saveOrderBase( $orderBaseItem );
230
231
		return new \Aimeos\MShop\Common\Item\Helper\Form\Standard( $paypalUrl, 'POST', [] );
232
	}
233
234
235
	/**
236
	 * Queries for status updates for the given order if supported.
237
	 *
238
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object
239
	 */
240
	public function query( \Aimeos\MShop\Order\Item\Iface $order )
241
	{
242
		if( ( $tid = $this->getOrderServiceItem( $order->getBaseId() )->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null )
243
		{
244
			$msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() );
245
			throw new \Aimeos\MShop\Service\Exception( $msg );
246
		}
247
248
		$values = $this->getAuthParameter();
249
		$values['METHOD'] = 'GetTransactionDetails';
250
		$values['TRANSACTIONID'] = $tid;
251
252
		$urlQuery = http_build_query( $values, '', '&' );
253
		$response = $this->getCommunication()->transmit( $this->apiendpoint, 'POST', $urlQuery );
254
		$rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ );
255
256
		$this->setPaymentStatus( $order, $rvals );
257
		$this->saveOrder( $order );
258
	}
259
260
261
	/**
262
	 * Captures the money later on request for the given order if supported.
263
	 *
264
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object
265
	 */
266
	public function capture( \Aimeos\MShop\Order\Item\Iface $order )
267
	{
268
		$baseid = $order->getBaseId();
269
		$baseItem = $this->getOrderBase( $baseid );
270
		$serviceItem = $baseItem->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
271
272
		if( ( $tid = $serviceItem->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null )
273
		{
274
			$msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() );
275
			throw new \Aimeos\MShop\Service\Exception( $msg );
276
		}
277
278
		$price = $baseItem->getPrice();
279
280
		$values = $this->getAuthParameter();
281
		$values['METHOD'] = 'DoCapture';
282
		$values['COMPLETETYPE'] = 'Complete';
283
		$values['AUTHORIZATIONID'] = $tid;
284
		$values['INVNUM'] = $order->getId();
285
		$values['CURRENCYCODE'] = $price->getCurrencyId();
286
		$values['AMT'] = $this->getAmount( $price );
287
288
		$urlQuery = http_build_query( $values, '', '&' );
289
		$response = $this->getCommunication()->transmit( $this->apiendpoint, 'POST', $urlQuery );
290
		$rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ );
291
292
		$this->setPaymentStatus( $order, $rvals );
293
294
		$attributes = [];
295
		if( isset( $rvals['PARENTTRANSACTIONID'] ) ) {
296
			$attributes['PARENTTRANSACTIONID'] = $rvals['PARENTTRANSACTIONID'];
297
		}
298
299
		//updates the transaction id
300
		$attributes['TRANSACTIONID'] = $rvals['TRANSACTIONID'];
301
		$this->setAttributes( $serviceItem, $attributes, 'payment/paypal' );
302
303
		$this->saveOrderBase( $baseItem );
304
		$this->saveOrder( $order );
305
	}
306
307
308
	/**
309
	 * Refunds the money for the given order if supported.
310
	 *
311
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object
312
	 */
313
	public function refund( \Aimeos\MShop\Order\Item\Iface $order )
314
	{
315
		$baseItem = $this->getOrderBase( $order->getBaseId() );
316
		$serviceItem = $baseItem->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
317
318
		if( ( $tid = $serviceItem->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null )
319
		{
320
			$msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() );
321
			throw new \Aimeos\MShop\Service\Exception( $msg );
322
		}
323
324
		$values = $this->getAuthParameter();
325
		$values['METHOD'] = 'RefundTransaction';
326
		$values['REFUNDSOURCE'] = 'instant';
327
		$values['REFUNDTYPE'] = 'Full';
328
		$values['TRANSACTIONID'] = $tid;
329
		$values['INVOICEID'] = $order->getId();
330
331
		$urlQuery = http_build_query( $values, '', '&' );
332
		$response = $this->getCommunication()->transmit( $this->apiendpoint, 'POST', $urlQuery );
333
		$rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ );
334
335
		$attributes = array( 'REFUNDTRANSACTIONID' => $rvals['REFUNDTRANSACTIONID'] );
336
		$this->setAttributes( $serviceItem, $attributes, 'payment/paypal' );
337
		$this->saveOrderBase( $baseItem );
338
339
		$order->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUND );
340
		$this->saveOrder( $order );
341
	}
342
343
344
	/**
345
	 * Cancels the authorization for the given order if supported.
346
	 *
347
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object
348
	 */
349
	public function cancel( \Aimeos\MShop\Order\Item\Iface $order )
350
	{
351
		if( ( $tid = $this->getOrderServiceItem( $order->getBaseId() )->getAttribute( 'TRANSACTIONID', 'payment/paypal' ) ) === null )
352
		{
353
			$msg = sprintf( 'PayPal Express: Payment transaction ID for order ID "%1$s" not available', $order->getId() );
354
			throw new \Aimeos\MShop\Service\Exception( $msg );
355
		}
356
357
		$values = $this->getAuthParameter();
358
		$values['METHOD'] = 'DoVoid';
359
		$values['AUTHORIZATIONID'] = $tid;
360
361
		$urlQuery = http_build_query( $values, '', '&' );
362
		$response = $this->getCommunication()->transmit( $this->apiendpoint, 'POST', $urlQuery );
363
		$this->checkResponse( $order->getId(), $response, __METHOD__ );
364
365
		$order->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED );
366
		$this->saveOrder( $order );
367
	}
368
369
370
	/**
371
	 * Updates the orders for which status updates were received via direct requests (like HTTP).
372
	 *
373
	 * @param array $params Associative list of request parameters
374
	 * @param string|null $body Information sent within the body of the request
375
	 * @param string|null &$response Response body for notification requests
376
	 * @param array &$header Response headers for notification requests
377
	 * @return \Aimeos\MShop\Order\Item\Iface|null Order item if update was successful, null if the given parameters are not valid for this provider
378
	 * @throws \Aimeos\MShop\Service\Exception If updating one of the orders failed
379
	 */
380
	public function updateSync( array $params = [], $body = null, &$response = null, array &$header = [] )
381
	{
382
		if( isset( $params['token'] ) && isset( $params['PayerID'] ) && isset( $params['orderid'] ) ) {
383
			return $this->doExpressCheckoutPayment( $params );
384
		}
385
386
		//tid from ipn
387
		if( !isset( $params['txn_id'] ) ) {
388
			return null;
389
		}
390
391
		$urlQuery = http_build_query( $params, '', '&' );
392
393
		//validation
394
		$response = $this->getCommunication()->transmit( $this->getConfigValue( array( 'paypalexpress.url-validate' ) ), 'POST', $urlQuery );
395
396
397
		if( $response !== 'VERIFIED' )
398
		{
399
			$msg = sprintf( 'PayPal Express: Invalid request "%1$s"', $urlQuery );
400
			throw new \Aimeos\MShop\Service\Exception( $msg );
401
		}
402
403
404
		$order = $this->getOrder( $params['invoice'] );
405
		$baseItem = $this->getOrderBase( $order->getBaseId() );
406
		$serviceItem = $baseItem->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
407
408
		$this->checkIPN( $baseItem, $params );
409
410
		$status = array( 'PAYMENTSTATUS' => $params['payment_status'] );
411
412
		if( isset( $params['pending_reason'] ) ) {
413
			$status['PENDINGREASON'] = $params['pending_reason'];
414
		}
415
416
		$this->setAttributes( $serviceItem, array( $params['txn_id'] => $params['payment_status'] ), 'payment/paypal/txn' );
417
		$this->setAttributes( $serviceItem, array( 'TRANSACTIONID' => $params['txn_id'] ), 'payment/paypal' );
418
		$this->saveOrderBase( $baseItem );
419
420
		$this->setPaymentStatus( $order, $status );
421
		$this->saveOrder( $order );
422
423
		return $order;
424
	}
425
426
427
	/**
428
	 * Checks what features the payment provider implements.
429
	 *
430
	 * @param integer $what Constant from abstract class
431
	 * @return boolean True if feature is available in the payment provider, false if not
432
	 */
433
	public function isImplemented( $what )
434
	{
435
		switch( $what )
436
		{
437
			case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CAPTURE:
438
			case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY:
439
			case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_CANCEL:
440
			case \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_REFUND:
441
				return true;
442
		}
443
444
		return false;
445
	}
446
447
	/**
448
	 * Begins paypalexpress transaction and saves transaction id.
449
	 *
450
	 * @param mixed $params Update information whose format depends on the payment provider
451
	 * @return \Aimeos\MShop\Order\Item\Iface|null Order item if update was successful, null if the given parameters are not valid for this provider
452
	 */
453
	protected function doExpressCheckoutPayment( $params )
454
	{
455
		$order = $this->getOrder( $params['orderid'] );
456
		$baseid = $order->getBaseId();
457
		$baseItem = $this->getOrderBase( $baseid );
458
		$serviceItem = $baseItem->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
459
460
		$price = $baseItem->getPrice();
461
462
		$values = $this->getAuthParameter();
463
		$values['METHOD'] = 'DoExpressCheckoutPayment';
464
		$values['TOKEN'] = $params['token'];
465
		$values['PAYERID'] = $params['PayerID'];
466
		$values['PAYMENTACTION'] = $this->getConfigValue( array( 'paypalexpress.PaymentAction' ), 'Sale' );
467
		$values['CURRENCYCODE'] = $price->getCurrencyId();
468
		$values['NOTIFYURL'] = $this->getConfigValue( array( 'payment.url-update', 'payment.url-success' ) );
469
		$values['AMT'] = $this->getAmount( $price );
470
471
		$urlQuery = http_build_query( $values, '', '&' );
472
		$response = $this->getCommunication()->transmit( $this->apiendpoint, 'POST', $urlQuery );
473
		$rvals = $this->checkResponse( $order->getId(), $response, __METHOD__ );
474
475
		$attributes = array( 'PAYERID' => $params['PayerID'] );
476
477
		if( isset( $rvals['TRANSACTIONID'] ) )
478
		{
479
			$attributes['TRANSACTIONID'] = $rvals['TRANSACTIONID'];
480
			$this->setAttributes( $serviceItem, array( $rvals['TRANSACTIONID'] => $rvals['PAYMENTSTATUS'] ), 'payment/paypal/txn' );
481
		}
482
483
		$this->setAttributes( $serviceItem, $attributes, 'payment/paypal' );
484
		$this->saveOrderBase( $baseItem );
485
486
		$this->setPaymentStatus( $order, $rvals );
487
		$this->saveOrder( $order );
488
489
		return $order;
490
	}
491
492
493
	/**
494
	 * Checks the response from the payment server.
495
	 *
496
	 * @param string $orderid Order item ID
497
	 * @param string $response Response from the payment provider
498
	 * @param string $method Name of the calling method
499
	 * @return array Associative list of key/value pairs containing the response parameters
500
	 * @throws \Aimeos\MShop\Service\Exception If request was not successful and an error was returned
501
	 */
502
	protected function checkResponse( $orderid, $response, $method )
503
	{
504
		$rvals = [];
505
		parse_str( $response, $rvals );
506
507
		if( $rvals['ACK'] !== 'Success' )
508
		{
509
			$msg = 'PayPal Express: method = ' . $method . ', order ID = ' . $orderid . ', response = ' . print_r( $rvals, true );
510
			$this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::WARN, 'core/service/payment' );
511
512
			if( $rvals['ACK'] !== 'SuccessWithWarning' )
513
			{
514
				$short = ( isset( $rvals['L_SHORTMESSAGE0'] ) ? $rvals['L_SHORTMESSAGE0'] : '<none>' );
515
				$msg = sprintf( 'PayPal Express: Request for order ID "%1$s" failed with "%2$s"', $orderid, $short );
516
				throw new \Aimeos\MShop\Service\Exception( $msg );
517
			}
518
		}
519
520
		return $rvals;
521
	}
522
523
524
	/**
525
	 * Checks if IPN message from paypal is valid.
526
	 *
527
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket
528
	 * @param array $params
529
	 */
530
	protected function checkIPN( $basket, $params )
531
	{
532
		$attrManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order/base/service/attribute' );
533
534
		if( $this->getConfigValue( array( 'paypalexpress.AccountEmail' ) ) !== $params['receiver_email'] )
535
		{
536
			$msg = sprintf( 'PayPal Express: Wrong receiver email "%1$s"', $params['receiver_email'] );
537
			throw new \Aimeos\MShop\Service\Exception( $msg );
538
		}
539
540
		$price = $basket->getPrice();
541
542
		if( $this->getAmount( $price ) != $params['payment_amount'] )
543
		{
544
			$msg = sprintf( 'PayPal Express: Wrong payment amount "%1$s" for order ID "%2$s"', $params['payment_amount'], $params['invoice'] );
545
			throw new \Aimeos\MShop\Service\Exception( $msg );
546
		}
547
548
		$search = $attrManager->createSearch();
549
		$expr = array(
550
			$search->compare( '==', 'order.base.service.attribute.code', $params['txn_id'] ),
551
			$search->compare( '==', 'order.base.service.attribute.value', $params['payment_status'] ),
552
		);
553
554
		$search->setConditions( $search->combine( '&&', $expr ) );
555
		$results = $attrManager->searchItems( $search );
556
557
		if( ( $attr = reset( $results ) ) !== false )
558
		{
559
			$msg = sprintf( 'PayPal Express: Duplicate transaction with ID "%1$s" and status "%2$s" ', $params['txn_id'], $params['txn_status'] );
560
			throw new \Aimeos\MShop\Service\Exception( $msg );
561
		}
562
	}
563
564
565
	/**
566
	 * Maps the PayPal status to the appropriate payment status and sets it in the order object.
567
	 *
568
	 * @param \Aimeos\MShop\Order\Item\Iface $invoice Order invoice object
569
	 * @param array $response Associative list of key/value pairs containing the PayPal response
570
	 */
571
	protected function setPaymentStatus( \Aimeos\MShop\Order\Item\Iface $invoice, array $response )
572
	{
573
		if( !isset( $response['PAYMENTSTATUS'] ) ) {
574
			return;
575
		}
576
577
		switch( $response['PAYMENTSTATUS'] )
578
		{
579
			case 'Pending':
580
				if( isset( $response['PENDINGREASON'] ) )
581
				{
582
					if( $response['PENDINGREASON'] === 'authorization' )
583
					{
584
						$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED );
585
						break;
586
					}
587
588
					$str = 'PayPal Express: order ID = ' . $invoice->getId() . ', PENDINGREASON = ' . $response['PENDINGREASON'];
589
					$this->getContext()->getLogger()->log( $str, \Aimeos\MW\Logger\Base::INFO, 'core/service/payment' );
590
				}
591
592
				$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_PENDING );
593
				break;
594
595
			case 'In-Progress':
596
				$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_PENDING );
597
				break;
598
599
			case 'Completed':
600
			case 'Processed':
601
				$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_RECEIVED );
602
				break;
603
604
			case 'Failed':
605
			case 'Denied':
606
			case 'Expired':
607
				$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUSED );
608
				break;
609
610
			case 'Refunded':
611
			case 'Partially-Refunded':
612
			case 'Reversed':
613
				$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_REFUND );
614
				break;
615
616
			case 'Canceled-Reversal':
617
			case 'Voided':
618
				$invoice->setPaymentStatus( \Aimeos\MShop\Order\Item\Base::PAY_CANCELED );
619
				break;
620
621
			default:
622
				$str = 'PayPal Express: order ID = ' . $invoice->getId() . ', response = ' . print_r( $response, true );
623
				$this->getContext()->getLogger()->log( $str, \Aimeos\MW\Logger\Base::INFO, 'core/service/payment' );
624
		}
625
	}
626
627
628
	/**
629
	 * Returns an list of order data required by PayPal.
630
	 *
631
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $orderBase Order base item
632
	 * @return array Associative list of key/value pairs with order data required by PayPal
633
	 */
634
	protected function getOrderDetails( \Aimeos\MShop\Order\Item\Base\Iface $orderBase )
635
	{
636
		$deliveryCosts = 0;
637
		$deliveryPrices = [];
638
		$values = $this->getAuthParameter();
639
640
641
		if( $this->getConfigValue( 'paypalexpress.address', true ) )
642
		{
643
			try
644
			{
645
				$orderAddressDelivery = $orderBase->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
646
647
				/* setting up the address details */
648
				$values['NOSHIPPING'] = $this->getConfigValue( array( 'paypalexpress.NoShipping' ), 1 );
649
				$values['ADDROVERRIDE'] = $this->getConfigValue( array( 'paypalexpress.AddrOverride' ), 0 );
650
				$values['PAYMENTREQUEST_0_SHIPTONAME'] = $orderAddressDelivery->getFirstName() . ' ' . $orderAddressDelivery->getLastName();
651
				$values['PAYMENTREQUEST_0_SHIPTOSTREET'] = $orderAddressDelivery->getAddress1() . ' ' . $orderAddressDelivery->getAddress2() . ' ' . $orderAddressDelivery->getAddress3();
652
				$values['PAYMENTREQUEST_0_SHIPTOCITY'] = $orderAddressDelivery->getCity();
653
				$values['PAYMENTREQUEST_0_SHIPTOSTATE'] = $orderAddressDelivery->getState();
654
				$values['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $orderAddressDelivery->getCountryId();
655
				$values['PAYMENTREQUEST_0_SHIPTOZIP'] = $orderAddressDelivery->getPostal();
656
			}
657
			catch( \Exception $e ) { ; } // If no address is available
658
		}
659
660
661
		if( $this->getConfigValue( 'paypalexpress.product', true ) )
662
		{
663
			$lastPos = 0;
664
			foreach( $orderBase->getProducts() as $product )
665
			{
666
				$price = $product->getPrice();
667
				$lastPos = $product->getPosition() - 1;
668
669
				$deliveryPrice = clone $price;
670
				$deliveryPrices = $this->addPrice( $deliveryPrices, $deliveryPrice->setValue( '0.00' ), $product->getQuantity() );
671
672
				$values['L_PAYMENTREQUEST_0_NUMBER' . $lastPos] = $product->getId();
673
				$values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $product->getName();
674
				$values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = $product->getQuantity();
675
				$values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $this->getAmount( $price, false );
676
			}
677
		}
678
679
680
		if( $this->getConfigValue( 'paypalexpress.service', true ) )
681
		{
682
			$price = $orderBase->getService( 'payment' )->getPrice();
683
			if( ( $paymentCosts = $this->getAmount( $price ) ) > '0.00' )
684
			{
685
				$lastPos++;
0 ignored issues
show
Bug introduced by
The variable $lastPos does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
686
				$values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $this->getContext()->getI18n()->dt( 'mshop', 'Payment costs' );
687
				$values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1';
688
				$values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $paymentCosts;
689
			}
690
691
			try
692
			{
693
				$orderServiceDeliveryItem = $orderBase->getService( 'delivery' );
694
				$price = $orderServiceDeliveryItem->getPrice();
695
				$deliveryPrices = $this->addPrice( $deliveryPrices, $price );
696
697
				foreach( $deliveryPrices as $priceItem ) {
698
					$deliveryCosts += $this->getAmount( $priceItem );
699
				}
700
701
				$values['L_SHIPPINGOPTIONAMOUNT0'] = number_format( $deliveryCosts, 2, '.', '' );
702
				$values['L_SHIPPINGOPTIONLABEL0'] = $orderServiceDeliveryItem->getCode();
703
				$values['L_SHIPPINGOPTIONNAME0'] = $orderServiceDeliveryItem->getName();
704
				$values['L_SHIPPINGOPTIONISDEFAULT0'] = 'true';
705
			}
706
			catch( \Exception $e ) { ; } // If no delivery service is available
707
		}
708
709
710
		$price = $orderBase->getPrice();
711
		$amount = $this->getAmount( $price );
712
713
		if( $deliveryCosts === 0 )
714
		{
715
			foreach( $deliveryPrices as $priceItem ) {
716
				$deliveryCosts += $this->getAmount( $priceItem );
717
			}
718
		}
719
720
		$values['MAXAMT'] = $amount + 0.01; // possible rounding error
721
		$values['PAYMENTREQUEST_0_AMT'] = $amount;
722
		$values['PAYMENTREQUEST_0_ITEMAMT'] = number_format( $amount - $deliveryCosts, 2, '.', '' );
723
		$values['PAYMENTREQUEST_0_SHIPPINGAMT'] = number_format( $deliveryCosts, 2, '.', '' );
724
		$values['PAYMENTREQUEST_0_INSURANCEAMT'] = '0.00';
725
		$values['PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED'] = 'false';
726
		$values['PAYMENTREQUEST_0_SHIPDISCAMT'] = '0.00';
727
		$values['PAYMENTREQUEST_0_CURRENCYCODE'] = $orderBase->getPrice()->getCurrencyId();
728
		$values['PAYMENTREQUEST_0_PAYMENTACTION'] = $this->getConfigValue( array( 'paypalexpress.PaymentAction' ), 'sale' );
729
730
		return $values;
731
	}
732
733
734
	/**
735
	 * Returns the data required for authorization against the PayPal server.
736
	 *
737
	 * @return array Associative list of key/value pairs containing the autorization parameters
738
	 */
739
	protected function getAuthParameter()
740
	{
741
		return array(
742
			'VERSION' => '204.0',
743
			'SIGNATURE' => $this->getConfigValue( array( 'paypalexpress.ApiSignature' ) ),
744
			'USER' => $this->getConfigValue( array( 'paypalexpress.ApiUsername' ) ),
745
			'PWD' => $this->getConfigValue( array( 'paypalexpress.ApiPassword' ) ),
746
		);
747
	}
748
749
750
	/**
751
	 * Returns order service item for specified base ID.
752
	 *
753
	 * @param integer $baseid Base ID of the order
754
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order service item
755
	 */
756
	protected function getOrderServiceItem( $baseid )
757
	{
758
		$basket = $this->getOrderBase( $baseid, \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE );
759
		return $basket->getService( \Aimeos\MShop\Order\Item\Base\Service\Base::TYPE_PAYMENT );
760
	}
761
762
763
	/**
764
	 * Adds the costs to the price item with the corresponding tax rate
765
	 *
766
	 * @param \Aimeos\MShop\Price\Item\Iface[] $prices Associative list of tax rates as key and price items as value
767
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price item that should be added
768
	 * @param integer $quantity Product quantity
769
	 * @return \Aimeos\MShop\Price\Item\Iface[] Updated list of price items
0 ignored issues
show
Documentation introduced by
Should the return type not be array<*,\Aimeos\MShop\Common\Item\Iface>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
770
	 */
771
	protected function addPrice( array $prices, $price, $quantity = 1 )
772
	{
773
		$taxrate = $price->getTaxRate();
774
775
		if( !isset( $prices[$taxrate] ) )
776
		{
777
			$prices[$taxrate] = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'price' )->createItem();
778
			$prices[$taxrate]->setTaxRate( $taxrate );
779
		}
780
781
		$prices[$taxrate]->addItem( $price, $quantity );
782
783
		return $prices;
784
	}
785
}