/
paypalplatform.php
574 lines (501 loc) · 18.1 KB
/
paypalplatform.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
<?php
/********************************************
PayPal Adaptive Payments API Module
Defines all the global variables and the wrapper functions
********************************************/
$PROXY_HOST = '127.0.0.1';
$PROXY_PORT = '808';
$Env = "sandbox";
//------------------------------------
// PayPal API Credentials
// Replace <API_USERNAME> with your API Username
// Replace <API_PASSWORD> with your API Password
// Replace <API_SIGNATURE> with your Signature
//------------------------------------
$API_UserName = "ezell_api1.ezellburke.com";
$API_Password = "GEWF7KFHB3MP793Z";
$API_Signature = "AFcWxV21C7fd0v3bYYYRCpSSRl31AfhHzZM7M-IXQ2wOLZa-ESptdzfR ";
// AppID is preset for sandbox use
// If your application goes live, you will be assigned a value for the live environment by PayPal as part of the live onboarding process
$API_AppID = "APP-80W284485P519543T";
$API_Endpoint = "";
if ($Env == "sandbox")
{
$API_Endpoint = "https://svcs.sandbox.paypal.com/AdaptivePayments";
}
else
{
$API_Endpoint = "https://svcs.paypal.com/AdaptivePayments";
}
$USE_PROXY = false;
if (session_id() == "")
session_start();
function generateCharacter () {
$possible = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
return $char;
}
function generateTrackingID () {
$GUID = generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter();
$GUID .= generateCharacter().generateCharacter().generateCharacter().generateCharacter();
return $GUID;
}
/*
'-------------------------------------------------------------------------------------------------------------------------------------------
' Purpose: Prepares the parameters for the Refund API Call.
' The API credentials used in a Pay call can make the Refund call
' against a payKey, or a tracking id, or to specific receivers of a payKey or a tracking id
' that resulted from the Pay call
'
' A receiver itself with its own API credentials can make a Refund call against the transactionId corresponding to their transaction.
' The API credentials used in a Pay call cannot use transactionId to issue a refund
' for a transaction for which they themselves were not the receiver
'
' If you do specify specific receivers, keep in mind that you must provide the amounts as well
' If you specify a transactionId, then only the receiver of that transactionId is affected therefore
' the receiverEmailArray and receiverAmountArray should have 1 entry each if you do want to give a partial refund
' Inputs:
'
' Conditionally Required:
' One of the following: payKey or trackingId or trasactionId or
' (payKey and receiverEmailArray and receiverAmountArray) or
' (trackingId and receiverEmailArray and receiverAmountArray) or
' (transactionId and receiverEmailArray and receiverAmountArray)
' Returns:
' The NVP Collection object of the Refund call response.
'--------------------------------------------------------------------------------------------------------------------------------------------
*/
function CallRefund( $payKey, $transactionId, $trackingId, $receiverEmailArray, $receiverAmountArray )
{
/* Gather the information to make the Refund call.
The variable nvpstr holds the name value pairs
*/
$nvpstr = "";
// conditionally required fields
if ("" != $payKey)
{
$nvpstr = "payKey=" . urlencode($payKey);
if (0 != count($receiverEmailArray))
{
reset($receiverEmailArray);
while (list($key, $value) = each($receiverEmailArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
}
}
}
if (0 != count($receiverAmountArray))
{
reset($receiverAmountArray);
while (list($key, $value) = each($receiverAmountArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
}
}
}
}
elseif ("" != $trackingId)
{
$nvpstr = "trackingId=" . urlencode($trackingId);
if (0 != count($receiverEmailArray))
{
reset($receiverEmailArray);
while (list($key, $value) = each($receiverEmailArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
}
}
}
if (0 != count($receiverAmountArray))
{
reset($receiverAmountArray);
while (list($key, $value) = each($receiverAmountArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
}
}
}
}
elseif ("" != $transactionId)
{
$nvpstr = "transactionId=" . urlencode($transactionId);
// the caller should only have 1 entry in the email and amount arrays
if (0 != count($receiverEmailArray))
{
reset($receiverEmailArray);
while (list($key, $value) = each($receiverEmailArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
}
}
}
if (0 != count($receiverAmountArray))
{
reset($receiverAmountArray);
while (list($key, $value) = each($receiverAmountArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
}
}
}
}
/* Make the Refund call to PayPal */
$resArray = hash_call("Refund", $nvpstr);
/* Return the response array */
return $resArray;
}
/*
'-------------------------------------------------------------------------------------------------------------------------------------------
' Purpose: Prepares the parameters for the PaymentDetails API Call.
' The PaymentDetails call can be made with either
' a payKey, a trackingId, or a transactionId of a previously successful Pay call.
' Inputs:
'
' Conditionally Required:
' One of the following: payKey or transactionId or trackingId
' Returns:
' The NVP Collection object of the PaymentDetails call response.
'--------------------------------------------------------------------------------------------------------------------------------------------
*/
function CallPaymentDetails( $payKey, $transactionId, $trackingId )
{
/* Gather the information to make the PaymentDetails call.
The variable nvpstr holds the name value pairs
*/
$nvpstr = "";
// conditionally required fields
if ("" != $payKey)
{
$nvpstr = "payKey=" . urlencode($payKey);
}
elseif ("" != $transactionId)
{
$nvpstr = "transactionId=" . urlencode($transactionId);
}
elseif ("" != $trackingId)
{
$nvpstr = "trackingId=" . urlencode($trackingId);
}
/* Make the PaymentDetails call to PayPal */
$resArray = hash_call("PaymentDetails", $nvpstr);
/* Return the response array */
return $resArray;
}
/*
'-------------------------------------------------------------------------------------------------------------------------------------------
' Purpose: Prepares the parameters for the Pay API Call.
' Inputs:
'
' Required:
'
' Optional:
'
'
' Returns:
' The NVP Collection object of the Pay call response.
'--------------------------------------------------------------------------------------------------------------------------------------------
*/
function CallPay( $actionType, $cancelUrl, $returnUrl, $currencyCode, $receiverEmailArray, $receiverAmountArray,
$receiverPrimaryArray, $receiverInvoiceIdArray, $feesPayer, $ipnNotificationUrl,
$memo, $pin, $preapprovalKey, $reverseAllParallelPaymentsOnError, $senderEmail, $trackingId )
{
/* Gather the information to make the Pay call.
The variable nvpstr holds the name value pairs
*/
// required fields
$nvpstr = "actionType=" . urlencode($actionType) . "¤cyCode=" . urlencode($currencyCode);
$nvpstr .= "&returnUrl=" . urlencode($returnUrl) . "&cancelUrl=" . urlencode($cancelUrl);
if (0 != count($receiverAmountArray))
{
reset($receiverAmountArray);
while (list($key, $value) = each($receiverAmountArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
}
}
}
if (0 != count($receiverEmailArray))
{
reset($receiverEmailArray);
while (list($key, $value) = each($receiverEmailArray))
{
if ("" != $value)
{
$nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
}
}
}
if (0 != count($receiverPrimaryArray))
{
reset($receiverPrimaryArray);
while (list($key, $value) = each($receiverPrimaryArray))
{
if ("" != $value)
{
$nvpstr = $nvpstr . "&receiverList.receiver(" . $key . ").primary=" . urlencode($value);
}
}
}
if (0 != count($receiverInvoiceIdArray))
{
reset($receiverInvoiceIdArray);
while (list($key, $value) = each($receiverInvoiceIdArray))
{
if ("" != $value)
{
$nvpstr = $nvpstr . "&receiverList.receiver(" . $key . ").invoiceId=" . urlencode($value);
}
}
}
// optional fields
if ("" != $feesPayer)
{
$nvpstr .= "&feesPayer=" . urlencode($feesPayer);
}
if ("" != $ipnNotificationUrl)
{
$nvpstr .= "&ipnNotificationUrl=" . urlencode($ipnNotificationUrl);
}
if ("" != $memo)
{
$nvpstr .= "&memo=" . urlencode($memo);
}
if ("" != $pin)
{
$nvpstr .= "&pin=" . urlencode($pin);
}
if ("" != $preapprovalKey)
{
$nvpstr .= "&preapprovalKey=" . urlencode($preapprovalKey);
}
if ("" != $reverseAllParallelPaymentsOnError)
{
$nvpstr .= "&reverseAllParallelPaymentsOnError=" . urlencode($reverseAllParallelPaymentsOnError);
}
if ("" != $senderEmail)
{
$nvpstr .= "&senderEmail=" . urlencode($senderEmail);
}
if ("" != $trackingId)
{
$nvpstr .= "&trackingId=" . urlencode($trackingId);
}
/* Make the Pay call to PayPal */
$resArray = hash_call("Pay", $nvpstr);
/* Return the response array */
return $resArray;
}
/*
'-------------------------------------------------------------------------------------------------------------------------------------------
' Purpose: Prepares the parameters for the PreapprovalDetails API Call.
' Inputs:
'
' Required:
' preapprovalKey: A preapproval key that identifies the agreement resulting from a previously successful Preapproval call.
' Returns:
' The NVP Collection object of the PreapprovalDetails call response.
'--------------------------------------------------------------------------------------------------------------------------------------------
*/
function CallPreapprovalDetails( $preapprovalKey )
{
/* Gather the information to make the PreapprovalDetails call.
The variable nvpstr holds the name value pairs
*/
// required fields
$nvpstr = "preapprovalKey=" . urlencode($preapprovalKey);
/* Make the PreapprovalDetails call to PayPal */
$resArray = hash_call("PreapprovalDetails", $nvpstr);
/* Return the response array */
return $resArray;
}
/*
'-------------------------------------------------------------------------------------------------------------------------------------------
' Purpose: Prepares the parameters for the Preapproval API Call.
' Inputs:
'
' Required:
'
' Optional:
'
'
' Returns:
' The NVP Collection object of the Preapproval call response.
'--------------------------------------------------------------------------------------------------------------------------------------------
*/
function CallPreapproval( $returnUrl, $cancelUrl, $currencyCode, $startingDate, $endingDate, $maxTotalAmountOfAllPayments,
$senderEmail, $maxNumberOfPayments, $paymentPeriod, $dateOfMonth, $dayOfWeek,
$maxAmountPerPayment, $maxNumberOfPaymentsPerPeriod, $pinType )
{
/* Gather the information to make the Preapproval call.
The variable nvpstr holds the name value pairs
*/
// required fields
$nvpstr = "returnUrl=" . urlencode($returnUrl) . "&cancelUrl=" . urlencode($cancelUrl);
$nvpstr .= "¤cyCode=" . urlencode($currencyCode) . "&startingDate=" . urlencode($startingDate);
$nvpstr .= "&endingDate=" . urlencode($endingDate);
$nvpstr .= "&maxTotalAmountOfAllPayments=" . urlencode($maxTotalAmountOfAllPayments);
// optional fields
if ("" != $senderEmail)
{
$nvpstr .= "&senderEmail=" . urlencode($senderEmail);
}
if ("" != $maxNumberOfPayments)
{
$nvpstr .= "&maxNumberOfPayments=" . urlencode($maxNumberOfPayments);
}
if ("" != $paymentPeriod)
{
$nvpstr .= "&paymentPeriod=" . urlencode($paymentPeriod);
}
if ("" != $dateOfMonth)
{
$nvpstr .= "&dateOfMonth=" . urlencode($dateOfMonth);
}
if ("" != $dayOfWeek)
{
$nvpstr .= "&dayOfWeek=" . urlencode($dayOfWeek);
}
if ("" != $maxAmountPerPayment)
{
$nvpstr .= "&maxAmountPerPayment=" . urlencode($maxAmountPerPayment);
}
if ("" != $maxNumberOfPaymentsPerPeriod)
{
$nvpstr .= "&maxNumberOfPaymentsPerPeriod=" . urlencode($maxNumberOfPaymentsPerPeriod);
}
if ("" != $pinType)
{
$nvpstr .= "&pinType=" . urlencode($pinType);
}
/* Make the Preapproval call to PayPal */
$resArray = hash_call("Preapproval", $nvpstr);
/* Return the response array */
return $resArray;
}
/**
'-------------------------------------------------------------------------------------------------------------------------------------------
* hash_call: Function to perform the API call to PayPal using API signature
* @methodName is name of API method.
* @nvpStr is nvp string.
* returns an associative array containing the response from the server.
'-------------------------------------------------------------------------------------------------------------------------------------------
*/
function hash_call($methodName, $nvpStr)
{
//declaring of global variables
global $API_Endpoint, $API_UserName, $API_Password, $API_Signature, $API_AppID;
global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$API_Endpoint/$methodName");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//turning off the server and peer verification(TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
// Set the HTTP Headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-PAYPAL-REQUEST-DATA-FORMAT: NV',
'X-PAYPAL-RESPONSE-DATA-FORMAT: NV',
'X-PAYPAL-SECURITY-USERID: ' . $API_UserName,
'X-PAYPAL-SECURITY-PASSWORD: ' .$API_Password,
'X-PAYPAL-SECURITY-SIGNATURE: ' . $API_Signature,
'X-PAYPAL-SERVICE-VERSION: 1.3.0',
'X-PAYPAL-APPLICATION-ID: ' . $API_AppID
));
//if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
//Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
if($USE_PROXY)
curl_setopt ($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
// RequestEnvelope fields
$detailLevel = urlencode("ReturnAll"); // See DetailLevelCode in the WSDL for valid enumerations
$errorLanguage = urlencode("en_US"); // This should be the standard RFC 3066 language identification tag, e.g., en_US
// NVPRequest for submitting to server
$nvpreq = "requestEnvelope.errorLanguage=$errorLanguage&requestEnvelope.detailLevel=$detailLevel";
$nvpreq .= "&$nvpStr";
//setting the nvpreq as POST FIELD to curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
//getting response from server
$response = curl_exec($ch);
//converting NVPResponse to an Associative Array
$nvpResArray=deformatNVP($response);
$nvpReqArray=deformatNVP($nvpreq);
$_SESSION['nvpReqArray']=$nvpReqArray;
if (curl_errno($ch))
{
// moving to display page to display curl errors
$_SESSION['curl_error_no']=curl_errno($ch) ;
$_SESSION['curl_error_msg']=curl_error($ch);
//Execute the Error handling module to display errors.
}
else
{
//closing the curl
curl_close($ch);
}
return $nvpResArray;
}
/*'----------------------------------------------------------------------------------
Purpose: Redirects to PayPal.com site.
Inputs: $cmd is the querystring
Returns:
----------------------------------------------------------------------------------
*/
function RedirectToPayPal ( $cmd )
{
// Redirect to paypal.com here
global $Env;
$payPalURL = "";
if ($Env == "sandbox")
{
$payPalURL = "https://www.sandbox.paypal.com/webscr?" . $cmd;
}
else
{
$payPalURL = "https://www.paypal.com/webscr?" . $cmd;
}
header("Location: ".$payPalURL);
exit;
}
/*'----------------------------------------------------------------------------------
* This function will take NVPString and convert it to an Associative Array and it will decode the response.
* It is usefull to search for a particular key and displaying arrays.
* @nvpstr is NVPString.
* @nvpArray is Associative Array.
----------------------------------------------------------------------------------
*/
function deformatNVP($nvpstr)
{
$intial=0;
$nvpArray = array();
while(strlen($nvpstr))
{
//postion of Key
$keypos= strpos($nvpstr,'=');
//position of value
$valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);
/*getting the Key and Value values and storing in a Associative Array*/
$keyval=substr($nvpstr,$intial,$keypos);
$valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
//decoding the respose
$nvpArray[urldecode($keyval)] =urldecode( $valval);
$nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
}
return $nvpArray;
}
?>