-
Notifications
You must be signed in to change notification settings - Fork 31
/
Api.php
631 lines (495 loc) · 21 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
<?php
namespace Payum\Paypal\ExpressCheckout\Nvp;
use Http\Message\MessageFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Exception\RuntimeException;
use Payum\Core\HttpClientInterface;
/**
* @link https://www.x.com/developers/paypal/documentation-tools/api/getexpresscheckoutdetails-api-operation-nvp
* @link https://www.x.com/developers/paypal/documentation-tools/api/doexpresscheckoutpayment-api-operation-nvp
* @link https://www.x.com/developers/paypal/documentation-tools/api/setexpresscheckout-api-operation-nvp
* @link https://www.x.com/developers/paypal/documentation-tools/api/gettransactiondetails-api-operation-nvp
* @link https://www.x.com/developers/paypal/documentation-tools/api/createrecurringpaymentsprofile-api-operation-nvp
* @link https://www.x.com/developers/paypal/documentation-tools/api/getrecurringpaymentsprofiledetails-api-operation-nvp
* @link https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/UpdateRecurringPaymentsProfile_API_Operation_NVP/
*
* L_ERRORCODE: @link https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_errorcodes
* ACK: @link https://www.x.com/content/paypal-nvp-api-overview
*/
class Api
{
public const ACK_SUCCESS = 'Success';
public const ACK_SUCCESS_WITH_WARNING = 'SuccessWithWarning';
public const ACK_FAILURE = 'Failure';
public const ACK_FAILUREWITHWARNING = 'FailureWithWarning';
public const ACK_WARNING = 'Warning';
public const CHECKOUTSTATUS_PAYMENT_ACTION_NOT_INITIATED = 'PaymentActionNotInitiated';
public const CHECKOUTSTATUS_PAYMENT_ACTION_FAILED = 'PaymentActionFailed';
public const CHECKOUTSTATUS_PAYMENT_ACTION_IN_PROGRESS = 'PaymentActionInProgress';
public const CHECKOUTSTATUS_PAYMENT_COMPLETED = 'PaymentCompleted';
public const CHECKOUTSTATUS_PAYMENT_ACTION_COMPLETED = 'PaymentActionCompleted';
/**
* No status
*/
public const PAYMENTSTATUS_NONE = 'None';
/**
* A reversal has been canceled; for example, when you win a dispute and the funds for the reversal have been returned to you.
*/
public const PAYMENTSTATUS_CANCELED_REVERSAL = 'Canceled-Reversal';
/**
* The payment has been completed, and the funds have been added successfully to your account balance.
*/
public const PAYMENTSTATUS_COMPLETED = 'Completed';
/**
* You denied the payment. This happens only if the payment was previously pending because of possible reasons described for the PendingReason element.
*/
public const PAYMENTSTATUS_DENIED = 'Denied';
/**
* The authorization period for this payment has been reached.
*/
public const PAYMENTSTATUS_EXPIRED = 'Expired';
/**
* The payment has failed. This happens only if the payment was made from your buyer's bank account.
*/
public const PAYMENTSTATUS_FAILED = 'Failed';
/**
* The transaction has not terminated, e.g. an authorization may be awaiting completion.
*/
public const PAYMENTSTATUS_IN_PROGRESS = 'In-Progress';
/**
* The payment has been partially refunded.
*/
public const PAYMENTSTATUS_PARTIALLY_REFUNDED = 'Partially-Refunded';
/**
* The payment is pending. See the PendingReason field for more information.
*/
public const PAYMENTSTATUS_PENDING = 'Pending';
/**
* You refunded the payment.
*/
public const PAYMENTSTATUS_REFUNDED = 'Refunded';
/**
* A payment was reversed due to a chargeback or other type of reversal. The funds have been removed from your account balance and returned to the buyer. The reason for the reversal is specified in the ReasonCode element.
*/
public const PAYMENTSTATUS_REVERSED = 'Reversed';
/**
* A payment has been accepted.
*/
public const PAYMENTSTATUS_PROCESSED = 'Processed';
/**
* An authorization for this transaction has been voided.
*/
public const PAYMENTSTATUS_VOIDED = 'Voided';
/**
* The payment has been completed, and the funds have been added successfully to your pending balance.
*/
public const PAYMENTSTATUS_COMPLETED_FUNDS_HELD = 'Completed-Funds-Held';
public const PENDINGREASON_AUTHORIZATION = 'authorization';
/**
* How you want to obtain payment. When implementing parallel payments, this field is required and must be set to Order. When implementing digital goods, this field is required and must be set to Sale. You can specify up to 10 payments, where n is a digit between 0 and 9, inclusive; except for digital goods, which supports single payments only. If the transaction does not include a one-time purchase, this field is ignored. It is one of the following values:
*
* Sale – This is a final sale for which you are requesting payment (default).
*/
public const PAYMENTACTION_SALE = 'Sale';
/**
* How you want to obtain payment. When implementing parallel payments, this field is required and must be set to Order. When implementing digital goods, this field is required and must be set to Sale. You can specify up to 10 payments, where n is a digit between 0 and 9, inclusive; except for digital goods, which supports single payments only. If the transaction does not include a one-time purchase, this field is ignored. It is one of the following values:
*
* Authorization – This payment is a basic authorization subject to settlement with PayPal Authorization and Capture.
*/
public const PAYMENTACTION_AUTHORIZATION = 'Authorization';
/**
* How you want to obtain payment. When implementing parallel payments, this field is required and must be set to Order. When implementing digital goods, this field is required and must be set to Sale. You can specify up to 10 payments, where n is a digit between 0 and 9, inclusive; except for digital goods, which supports single payments only. If the transaction does not include a one-time purchase, this field is ignored. It is one of the following values:
*
* Order – This payment is an order authorization subject to settlement with PayPal Authorization and Capture.
*/
public const PAYMENTACTION_ORDER = 'Order';
/**
* Payment has not been authorized by the user.
*/
public const L_ERRORCODE_PAYMENT_NOT_AUTHORIZED = 10485;
/**
* This Express Checkout session has expired.
*/
public const L_ERRORCODE_SESSION_HAS_EXPIRED = 10411;
/**
* PayPal displays the shipping address on the PayPal pages.
*/
public const NOSHIPPING_DISPLAY_ADDRESS = 0;
/**
* PayPal does not display shipping address fields whatsoever.
*/
public const NOSHIPPING_NOT_DISPLAY_ADDRESS = 1;
/**
* If you do not pass the shipping address, PayPal obtains it from the buyer’s account profile.
*/
public const NOSHIPPING_DISPLAY_BUYER_ADDRESS = 2;
/**
* You do not require the buyer’s shipping address be a confirmed address.
* For digital goods, this field is required, and you must set it to 0.
* Setting this field overrides the setting you specified in your Merchant Account Profile.
*/
public const REQCONFIRMSHIPPING_NOT_REQUIRED = 0;
/**
* You require the buyer’s shipping address be a confirmed address.
* Setting this field overrides the setting you specified in your Merchant Account Profile.
*/
public const REQCONFIRMSHIPPING_REQUIRED = 1;
/**
* Indicates whether an item is digital or physical. For digital goods, this field is required and must be set to Digital. You can specify up to 10 payments, where n is a digit between 0 and 9, inclusive, and m specifies the list item within the payment; except for digital goods, which only supports single payments.
*/
public const PAYMENTREQUEST_ITERMCATEGORY_DIGITAL = 'Digital';
/**
* Indicates whether an item is digital or physical. For digital goods, this field is required and must be set to Digital. You can specify up to 10 payments, where n is a digit between 0 and 9, inclusive, and m specifies the list item within the payment; except for digital goods, which only supports single payments.
*/
public const PAYMENTREQUEST_ITERMCATEGORY_PHYSICAL = 'Physical';
/**
* Indicates whether you would like PayPal to automatically bill the outstanding balance amount in the next billing cycle.
*
* PayPal does not automatically bill the outstanding balance.
*/
public const AUTOBILLOUTAMT_NOAUTOBILL = 'NoAutoBill';
/**
* Indicates whether you would like PayPal to automatically bill the outstanding balance amount in the next billing cycle.
*
* PayPal automatically bills the outstanding balance.
*/
public const AUTOBILLOUTAMT_ADDTONEXTBILLING = 'AddToNextBilling';
public const BILLINGPERIOD_DAY = 'Day';
public const BILLINGPERIOD_WEEK = 'Week';
/**
* For SemiMonth, billing is done on the 1st and 15th of each month.
*/
public const BILLINGPERIOD_SEMIMONTH = 'SemiMonth';
public const BILLINGPERIOD_MONTH = 'Month';
public const BILLINGPERIOD_YEAR = 'Year';
/**
* By default, PayPal suspends the pending profile in the event that the initial payment amount fails. You can override this default behavior by setting this field to ContinueOnFailure. Then, if the initial payment amount fails, PayPal adds the failed payment amount to the outstanding balance for this recurring payment profile.
*/
public const FAILEDINITAMTACTION_CONTINUEONFAILURE = 'ContinueOnFailure';
/**
* If this field is not set or you set it to CancelOnFailure, PayPal creates the recurring payment profile, but places it into a pending status until the initial payment completes. If the initial payment clears, PayPal notifies you by IPN that the pending profile has been activated. If the payment fails, PayPal notifies you by IPN that the pending profile has been canceled.
*/
public const FAILEDINITAMTACTION_CANCELONFAILURE = 'CancelOnFailure';
public const CREDITCARDTYPE_VISA = 'Visa';
public const CREDITCARDTYPE_MASTERCARD = 'MasterCard';
public const CREDITCARDTYPE_DISCOVER = 'Discover';
public const CREDITCARDTYPE_AMEX = 'Amex';
/**
* If the credit card type is Maestro, you must set CURRENCYCODE to GBP. In addition, you must specify either STARTDATE or ISSUENUMBER.
*/
public const CREDITCARDTYPE_MAESTRO = 'Maestro';
public const PAYERSTATUS_VERIFIED = 'verified';
public const PAYERSTATUS_UNVERIFIED = 'unverified';
/**
* The recurring payment profile has been successfully created and activated for scheduled payments according the billing instructions from the recurring payments profile.
*/
public const PROFILESTATUS_ACTIVEPROFILE = 'ActiveProfile';
/**
* The system is in the process of creating the recurring payment profile. Please check your IPN messages for an update.
*/
public const PROFILESTATUS_PENDINGPROFILE = 'PendingProfile';
/**
* Type of billing agreement. For recurring payments, this field must be set to RecurringPayments. In this case, you can specify up to ten billing agreements. Other defined values are not valid.
*/
public const BILLINGTYPE_RECURRING_PAYMENTS = 'RecurringPayments';
/**
* Type of billing agreement for reference transactions. You must have permission from PayPal to use this field. This field must be set to one of the following values:
*
* PayPal creates a billing agreement for each transaction associated with buyer. You must specify version 54.0 or higher to use this option.
*/
public const BILLINGTYPE_MERCHANTINITIATEDBILLING = 'MerchantInitiatedBilling';
/**
* Type of billing agreement for reference transactions. You must have permission from PayPal to use this field. This field must be set to one of the following values:
*
* PayPal creates a single billing agreement for all transactions associated with buyer. Use this value unless you need per-transaction billing agreements. You must specify version 58.0 or higher to use this option.
*/
public const BILLINGTYPE_MERCHANTINITIATEDBILLINGSINGLEAGREEMENT = 'MerchantInitiatedBilling';
public const RECURRINGPAYMENTSTATUS_ACTIVE = 'Active';
public const RECURRINGPAYMENTSTATUS_PENDING = 'Pending';
public const RECURRINGPAYMENTSTATUS_CANCELLED = 'Cancelled';
public const RECURRINGPAYMENTSTATUS_SUSPENDED = 'Suspended';
public const RECURRINGPAYMENTSTATUS_EXPIRED = 'Expired';
public const RECURRINGPAYMENTSTATUS_REACTIVATE = 'Reactivate';
public const RECURRINGPAYMENTACTION_CANCEL = 'Cancel';
public const USERACTION_COMMIT = 'commit';
public const CMD_EXPRESS_CHECKOUT = '_express-checkout';
public const CMD_EXPRESS_CHECKOUT_MOBILE = '_express-checkout-mobile';
public const VERSION = '65.1';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
protected $options = [
'username' => null,
'password' => null,
'signature' => null,
'subject' => null,
'return_url' => null,
'cancel_url' => null,
'sandbox' => null,
'useraction' => null,
'cmd' => self::CMD_EXPRESS_CHECKOUT,
];
/**
* @param HttpClientInterface|null $client
* @param MessageFactory|null $messageFactory
*/
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validateNotEmpty([
'username',
'password',
'signature',
]);
if (! is_bool($options['sandbox'])) {
throw new InvalidArgumentException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* Require: PAYMENTREQUEST_0_AMT
*
* @return array
*/
public function setExpressCheckout(array $fields)
{
if (! isset($fields['RETURNURL'])) {
if (! $this->options['return_url']) {
throw new RuntimeException('The return_url must be set either to FormRequest or to options.');
}
$fields['RETURNURL'] = $this->options['return_url'];
}
if (! isset($fields['CANCELURL'])) {
if (! $this->options['cancel_url']) {
throw new RuntimeException('The cancel_url must be set either to FormRequest or to options.');
}
$fields['CANCELURL'] = $this->options['cancel_url'];
}
$fields['METHOD'] = 'SetExpressCheckout';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: TOKEN
*
* @return array
*/
public function getExpressCheckoutDetails(array $fields)
{
$fields['METHOD'] = 'GetExpressCheckoutDetails';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: TRANSACTIONID
*
* @return array
*/
public function getTransactionDetails(array $fields)
{
$fields['METHOD'] = 'GetTransactionDetails';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: STARTDATE
*
* @return array
*/
public function transactionSearch(array $fields)
{
$fields['METHOD'] = 'TransactionSearch';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: PAYMENTREQUEST_0_AMT, PAYMENTREQUEST_0_PAYMENTACTION, PAYERID, TOKEN
*
* @return array
*/
public function doExpressCheckoutPayment(array $fields)
{
$fields['METHOD'] = 'DoExpressCheckoutPayment';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* @return array
*/
public function createRecurringPaymentsProfile(array $fields)
{
$fields['METHOD'] = 'CreateRecurringPaymentsProfile';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* @return array
*/
public function updateRecurringPaymentsProfile(array $fields)
{
$fields['METHOD'] = 'UpdateRecurringPaymentsProfile';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* @return array
*/
public function getRecurringPaymentsProfileDetails(array $fields)
{
$fields['METHOD'] = 'GetRecurringPaymentsProfileDetails';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* @return array
*/
public function manageRecurringPaymentsProfileStatus(array $fields)
{
$fields['METHOD'] = 'ManageRecurringPaymentsProfileStatus';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: PAYERID, TOKEN
*
* @return array
*/
public function createBillingAgreement(array $fields)
{
$fields['METHOD'] = 'CreateBillingAgreement';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: AMT, PAYMENTACTION, REFERENCEID
*
* @return array
*/
public function doReferenceTransaction(array $fields)
{
$fields['METHOD'] = 'DoReferenceTransaction';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: AUTHORIZATIONID, AMT, COMPLETETYPE
*
* @return array
*/
public function doCapture(array $fields)
{
$fields['METHOD'] = 'DoCapture';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: TRANSACTIONID
*
* @return array
*/
public function refundTransaction(array $fields)
{
$fields['METHOD'] = 'RefundTransaction';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* Require: AUTHORIZATIONID
*
* @return array
*/
public function doVoid(array $fields)
{
$fields['METHOD'] = 'DoVoid';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* @param string $token
*
* @return string
*/
public function getAuthorizeTokenUrl($token, array $query = [])
{
$defaultQuery = array_filter([
'useraction' => $this->options['useraction'],
'cmd' => $this->options['cmd'],
'token' => $token,
]);
$query = array_filter($query);
return sprintf(
'https://%s/cgi-bin/webscr?%s',
$this->options['sandbox'] ? 'www.sandbox.paypal.com' : 'www.paypal.com',
http_build_query(array_replace($defaultQuery, $query))
);
}
/**
* @throws HttpException
*
* @return array
*/
protected function doRequest(array $fields)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$request = $this->messageFactory->createRequest('POST', $this->getApiEndpoint(), $headers, http_build_query($fields));
$response = $this->client->send($request);
if (! ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
$result = [];
parse_str($response->getBody()->getContents(), $result);
foreach ($result as &$value) {
$value = urldecode($value);
}
return $result;
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return $this->options['sandbox'] ?
'https://api-3t.sandbox.paypal.com/nvp' :
'https://api-3t.paypal.com/nvp'
;
}
protected function addAuthorizeFields(array &$fields): void
{
$fields['PWD'] = $this->options['password'];
$fields['USER'] = $this->options['username'];
$fields['SIGNATURE'] = $this->options['signature'];
if ($this->options['subject']) {
$fields['SUBJECT'] = $this->options['subject'];
}
}
protected function addVersionField(array &$fields): void
{
$fields['VERSION'] = self::VERSION;
}
}