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
|
<?php /**************************************************** CallerService.php
This file uses the constants.php to get parameters needed to make an API call and calls the server.if you want use your own credentials, you have to change the constants.php
Called by TransactionDetails.php, ReviewOrder.php, DoDirectPaymentReceipt.php and DoExpressCheckoutPayment.php.
****************************************************/ require_once 'constants_sbox.php'; if(defined('API_USERNAME')) $API_UserName=API_USERNAME;
if(defined('API_PASSWORD')) $API_Password=API_PASSWORD;
if(defined('API_SIGNATURE')) $API_Signature=API_SIGNATURE;
if(defined('API_ENDPOINT')) $API_Endpoint =API_ENDPOINT;
$version=VERSION;
if(defined('SUBJECT')) $subject = SUBJECT; // below three are needed if used permissioning if(defined('AUTH_TOKEN')) $AUTH_token= AUTH_TOKEN;
if(defined('AUTH_SIGNATURE')) $AUTH_signature=AUTH_SIGNATURE;
if(defined('AUTH_TIMESTAMP')) $AUTH_timestamp=AUTH_TIMESTAMP;
function nvpHeader() { global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header, $subject, $AUTH_token,$AUTH_signature,$AUTH_timestamp; $nvpHeaderStr = "";
if(defined('AUTH_MODE')) { //$AuthMode = "3TOKEN"; //Merchant's API 3-TOKEN Credential is required to make API Call. //$AuthMode = "FIRSTPARTY"; //Only merchant Email is required to make EC Calls. //$AuthMode = "THIRDPARTY";Partner's API Credential and Merchant Email as Subject are required. $AuthMode = "AUTH_MODE"; } else { if((!empty($API_UserName)) && (!empty($API_Password)) && (!empty($API_Signature)) && (!empty($subject))) { $AuthMode = "THIRDPARTY"; } else if((!empty($API_UserName)) && (!empty($API_Password)) && (!empty($API_Signature))) { $AuthMode = "3TOKEN"; } elseif (!empty($AUTH_token) && !empty($AUTH_signature) && !empty($AUTH_timestamp)) { $AuthMode = "PERMISSION"; } elseif(!empty($subject)) { $AuthMode = "FIRSTPARTY"; } } switch($AuthMode) { case "3TOKEN" : $nvpHeaderStr = "&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature); break; case "FIRSTPARTY" : $nvpHeaderStr = "&SUBJECT=".urlencode($subject); break; case "THIRDPARTY" : $nvpHeaderStr = "&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature)."&SUBJECT=".urlencode($subject); break; case "PERMISSION" : $nvpHeaderStr = formAutorization($AUTH_token,$AUTH_signature,$AUTH_timestamp); break; } return $nvpHeaderStr; }
/** * 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 associtive array containing the response from the server. */
function hash_call($methodName,$nvpStr) { //declaring of global variables global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header, $subject, $AUTH_token,$AUTH_signature,$AUTH_timestamp; // form header string $nvpheader=nvpHeader(); //setting the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$API_Endpoint); 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_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST, 1); //in case of permission APIs send headers as HTTPheders if(!empty($AUTH_token) && !empty($AUTH_signature) && !empty($AUTH_timestamp)) { $headers_array[] = "X-PP-AUTHORIZATION: ".$nvpheader; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array); curl_setopt($ch, CURLOPT_HEADER, false); } else { $nvpStr=$nvpheader.$nvpStr; } //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);
//check if version is included in $nvpStr else include the version. if(strlen(str_replace('VERSION=', '', strtoupper($nvpStr))) == strlen($nvpStr)) { $nvpStr = "&VERSION=" . urlencode($version) . $nvpStr; } $nvpreq="METHOD=".urlencode($methodName).$nvpStr; //setting the nvpreq as POST FIELD to curl curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);
//getting response from server $response = curl_exec($ch);
//convrting 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); $location = "APIError.php"; header("Location: $location"); } else { //closing the curl curl_close($ch); }
return $nvpResArray; }
/** 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; } function formAutorization($auth_token,$auth_signature,$auth_timestamp) { $authString="token=".$auth_token.",signature=".$auth_signature.",timestamp=".$auth_timestamp ; return $authString; } ?>
|