Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
2,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.77 KB | None | 0 0
  1. <?php
  2. /********************************************
  3. PayPal API Module
  4.  
  5. Defines all the global variables and the wrapper functions
  6. ********************************************/
  7. $PROXY_HOST = '127.0.0.1';
  8. $PROXY_PORT = '808';
  9.  
  10. $SandboxFlag = false;
  11.  
  12. //'------------------------------------
  13. //' PayPal API Credentials
  14. //' Replace <API_USERNAME> with your API Username
  15. //' Replace <API_PASSWORD> with your API Password
  16. //' Replace <API_SIGNATURE> with your Signature
  17. //'------------------------------------
  18. $API_UserName = "eloboostingfactory_api1.gmail.com";
  19. $API_Password = "TGWQVUA2KPSNHK2R";
  20. $API_Signature = "Ahe6Kv87KZ10u0hylXbqAZcJT8jxAtwUZfsBDSBAzEu1e8wmQCCYX3bc";
  21.  
  22. // BN Code is only applicable for partners
  23. $sBNCode = "PP-ECWizard";
  24.  
  25.  
  26. /*
  27. ' Define the PayPal Redirect URLs.
  28. ' This is the URL that the buyer is first sent to do authorize payment with their paypal account
  29. ' change the URL depending if you are testing on the sandbox or the live PayPal site
  30. '
  31. ' For the sandbox, the URL is https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=
  32. ' For the live site, the URL is https://www.paypal.com/webscr&cmd=_express-checkout&token=
  33. */
  34.  
  35. if ($SandboxFlag == true)
  36. {
  37. $API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
  38. $PAYPAL_URL = "https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=";
  39. }
  40. else
  41. {
  42. $API_Endpoint = "https://api-3t.paypal.com/nvp";
  43. $PAYPAL_URL = "https://www.paypal.com/webscr&cmd=_express-checkout&token=";
  44. }
  45.  
  46. $USE_PROXY = false;
  47. $version="89";
  48.  
  49. if (session_id() == "")
  50. session_start();
  51.  
  52. /* An express checkout transaction starts with a token, that
  53. identifies to PayPal your transaction
  54. In this example, when the script sees a token, the script
  55. knows that the buyer has already authorized payment through
  56. paypal. If no token was found, the action is to send the buyer
  57. to PayPal to first authorize payment
  58. */
  59.  
  60. /*
  61. '-------------------------------------------------------------------------------------------------------------------------------------------
  62. ' Purpose: Prepares the parameters for the SetExpressCheckout API Call.
  63. ' Inputs:
  64. ' paymentAmount: Total value of the shopping cart
  65. ' currencyCodeType: Currency code value the PayPal API
  66. ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  67. ' returnURL: the page where buyers return to after they are done with the payment review on PayPal
  68. ' cancelURL: the page where buyers return to when they cancel the payment review on PayPal
  69. '--------------------------------------------------------------------------------------------------------------------------------------------
  70. */
  71. function CallShortcutExpressCheckout( $paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL)
  72. {
  73. //------------------------------------------------------------------------------------------------------------------------------------
  74. // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
  75.  
  76. $nvpstr="&PAYMENTREQUEST_0_AMT=". $paymentAmount;
  77. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
  78. $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
  79. $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
  80. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
  81. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_ITEMAMT=" . $paymentAmount;
  82. $nvpstr = $nvpstr . "&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital";
  83. $nvpstr = $nvpstr . "&L_PAYMENTREQUEST_0_NAME0=BoostingfactoryService";
  84. $nvpstr = $nvpstr . "&L_PAYMENTREQUEST_0_QTY0=1";
  85. $nvpstr = $nvpstr . "&L_PAYMENTREQUEST_0_AMT0=" . $paymentAmount;
  86.  
  87. $_SESSION["currencyCodeType"] = $currencyCodeType;
  88. $_SESSION["PaymentType"] = $paymentType;
  89.  
  90. //'---------------------------------------------------------------------------------------------------------------
  91. //' Make the API call to PayPal
  92. //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
  93. //' If an error occured, show the resulting errors
  94. //'---------------------------------------------------------------------------------------------------------------
  95. $resArray=hash_call("SetExpressCheckout", $nvpstr);
  96. $ack = strtoupper($resArray["ACK"]);
  97. if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
  98. {
  99. $token = urldecode($resArray["TOKEN"]);
  100. $_SESSION['TOKEN']=$token;
  101. }
  102.  
  103. return $resArray;
  104. }
  105.  
  106. /*
  107. '-------------------------------------------------------------------------------------------------------------------------------------------
  108. ' Purpose: Prepares the parameters for the SetExpressCheckout API Call.
  109. ' Inputs:
  110. ' paymentAmount: Total value of the shopping cart
  111. ' currencyCodeType: Currency code value the PayPal API
  112. ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  113. ' returnURL: the page where buyers return to after they are done with the payment review on PayPal
  114. ' cancelURL: the page where buyers return to when they cancel the payment review on PayPal
  115. ' shipToName: the Ship to name entered on the merchant's site
  116. ' shipToStreet: the Ship to Street entered on the merchant's site
  117. ' shipToCity: the Ship to City entered on the merchant's site
  118. ' shipToState: the Ship to State entered on the merchant's site
  119. ' shipToCountryCode: the Code for Ship to Country entered on the merchant's site
  120. ' shipToZip: the Ship to ZipCode entered on the merchant's site
  121. ' shipToStreet2: the Ship to Street2 entered on the merchant's site
  122. ' phoneNum: the phoneNum entered on the merchant's site
  123. '--------------------------------------------------------------------------------------------------------------------------------------------
  124. */
  125. function CallMarkExpressCheckout( $paymentAmount, $currencyCodeType, $paymentType, $returnURL,
  126. $cancelURL, $shipToName, $shipToStreet, $shipToCity, $shipToState,
  127. $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum
  128. )
  129. {
  130. //------------------------------------------------------------------------------------------------------------------------------------
  131. // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
  132.  
  133. $nvpstr="&PAYMENTREQUEST_0_AMT=". $paymentAmount;
  134. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
  135. $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
  136. $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
  137. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
  138. $nvpstr = $nvpstr . "&ADDROVERRIDE=1";
  139. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTONAME=" . $shipToName;
  140. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTREET=" . $shipToStreet;
  141. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTREET2=" . $shipToStreet2;
  142. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOCITY=" . $shipToCity;
  143. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTATE=" . $shipToState;
  144. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE=" . $shipToCountryCode;
  145. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOZIP=" . $shipToZip;
  146. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOPHONENUM=" . $phoneNum;
  147.  
  148. $_SESSION["currencyCodeType"] = $currencyCodeType;
  149. $_SESSION["PaymentType"] = $paymentType;
  150.  
  151. //'---------------------------------------------------------------------------------------------------------------
  152. //' Make the API call to PayPal
  153. //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
  154. //' If an error occured, show the resulting errors
  155. //'---------------------------------------------------------------------------------------------------------------
  156. $resArray=hash_call("SetExpressCheckout", $nvpstr);
  157. $ack = strtoupper($resArray["ACK"]);
  158. if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
  159. {
  160. $token = urldecode($resArray["TOKEN"]);
  161. $_SESSION['TOKEN']=$token;
  162. }
  163.  
  164. return $resArray;
  165. }
  166.  
  167. /*
  168. '-------------------------------------------------------------------------------------------
  169. ' Purpose: Prepares the parameters for the GetExpressCheckoutDetails API Call.
  170. '
  171. ' Inputs:
  172. ' None
  173. ' Returns:
  174. ' The NVP Collection object of the GetExpressCheckoutDetails Call Response.
  175. '-------------------------------------------------------------------------------------------
  176. */
  177. function GetShippingDetails( $token )
  178. {
  179. //'--------------------------------------------------------------
  180. //' At this point, the buyer has completed authorizing the payment
  181. //' at PayPal. The function will call PayPal to obtain the details
  182. //' of the authorization, incuding any shipping information of the
  183. //' buyer. Remember, the authorization is not a completed transaction
  184. //' at this state - the buyer still needs an additional step to finalize
  185. //' the transaction
  186. //'--------------------------------------------------------------
  187.  
  188. //'---------------------------------------------------------------------------
  189. //' Build a second API request to PayPal, using the token as the
  190. //' ID to get the details on the payment authorization
  191. //'---------------------------------------------------------------------------
  192. $nvpstr="&TOKEN=" . $token;
  193.  
  194. //'---------------------------------------------------------------------------
  195. //' Make the API call and store the results in an array.
  196. //' If the call was a success, show the authorization details, and provide
  197. //' an action to complete the payment.
  198. //' If failed, show the error
  199. //'---------------------------------------------------------------------------
  200. $resArray=hash_call("GetExpressCheckoutDetails",$nvpstr);
  201. $ack = strtoupper($resArray["ACK"]);
  202. if($ack == "SUCCESS" || $ack=="SUCCESSWITHWARNING")
  203. {
  204. $_SESSION['payer_id'] = $resArray['PAYERID'];
  205. }
  206. return $resArray;
  207. }
  208.  
  209. /*
  210. '-------------------------------------------------------------------------------------------------------------------------------------------
  211. ' Purpose: Prepares the parameters for the GetExpressCheckoutDetails API Call.
  212. '
  213. ' Inputs:
  214. ' sBNCode: The BN code used by PayPal to track the transactions from a given shopping cart.
  215. ' Returns:
  216. ' The NVP Collection object of the GetExpressCheckoutDetails Call Response.
  217. '--------------------------------------------------------------------------------------------------------------------------------------------
  218. */
  219. function ConfirmPayment( $FinalPaymentAmt )
  220. {
  221. /* Gather the information to make the final call to
  222. finalize the PayPal payment. The variable nvpstr
  223. holds the name value pairs
  224. */
  225.  
  226.  
  227. //Format the other parameters that were stored in the session from the previous calls
  228. $token = urlencode($_SESSION['TOKEN']);
  229. $paymentType = urlencode($_SESSION['PaymentType']);
  230. $currencyCodeType = urlencode($_SESSION['currencyCodeType']);
  231. $payerID = urlencode($_SESSION['payer_id']);
  232.  
  233. $serverName = urlencode($_SERVER['SERVER_NAME']);
  234.  
  235. $nvpstr = '&TOKEN=' . $token . '&PAYERID=' . $payerID . '&PAYMENTREQUEST_0_PAYMENTACTION=' . $paymentType . '&PAYMENTREQUEST_0_AMT=' . $FinalPaymentAmt;
  236. $nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE=' . $currencyCodeType . '&IPADDRESS=' . $serverName;
  237.  
  238. /* Make the call to PayPal to finalize payment
  239. If an error occured, show the resulting errors
  240. */
  241. $resArray=hash_call("DoExpressCheckoutPayment",$nvpstr);
  242.  
  243. /* Display the API response back to the browser.
  244. If the response from PayPal was a success, display the response parameters'
  245. If the response was an error, display the errors received using APIError.php.
  246. */
  247. $ack = strtoupper($resArray["ACK"]);
  248.  
  249. return $resArray;
  250. }
  251.  
  252. /*
  253. '-------------------------------------------------------------------------------------------------------------------------------------------
  254. ' Purpose: This function makes a DoDirectPayment API call
  255. '
  256. ' Inputs:
  257. ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  258. ' paymentAmount: total value of the shopping cart
  259. ' currencyCode: currency code value the PayPal API
  260. ' firstName: first name as it appears on credit card
  261. ' lastName: last name as it appears on credit card
  262. ' street: buyer's street address line as it appears on credit card
  263. ' city: buyer's city
  264. ' state: buyer's state
  265. ' countryCode: buyer's country code
  266. ' zip: buyer's zip
  267. ' creditCardType: buyer's credit card type (i.e. Visa, MasterCard ... )
  268. ' creditCardNumber: buyers credit card number without any spaces, dashes or any other characters
  269. ' expDate: credit card expiration date
  270. ' cvv2: Card Verification Value
  271. '
  272. '-------------------------------------------------------------------------------------------
  273. '
  274. ' Returns:
  275. ' The NVP Collection object of the DoDirectPayment Call Response.
  276. '--------------------------------------------------------------------------------------------------------------------------------------------
  277. */
  278.  
  279.  
  280. function DirectPayment( $paymentType, $paymentAmount, $creditCardType, $creditCardNumber,
  281. $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip,
  282. $countryCode, $currencyCode )
  283. {
  284. //Construct the parameter string that describes DoDirectPayment
  285. $nvpstr = "&AMT=" . $paymentAmount;
  286. $nvpstr = $nvpstr . "&CURRENCYCODE=" . $currencyCode;
  287. $nvpstr = $nvpstr . "&PAYMENTACTION=" . $paymentType;
  288. $nvpstr = $nvpstr . "&CREDITCARDTYPE=" . $creditCardType;
  289. $nvpstr = $nvpstr . "&ACCT=" . $creditCardNumber;
  290. $nvpstr = $nvpstr . "&EXPDATE=" . $expDate;
  291. $nvpstr = $nvpstr . "&CVV2=" . $cvv2;
  292. $nvpstr = $nvpstr . "&FIRSTNAME=" . $firstName;
  293. $nvpstr = $nvpstr . "&LASTNAME=" . $lastName;
  294. $nvpstr = $nvpstr . "&STREET=" . $street;
  295. $nvpstr = $nvpstr . "&CITY=" . $city;
  296. $nvpstr = $nvpstr . "&STATE=" . $state;
  297. $nvpstr = $nvpstr . "&COUNTRYCODE=" . $countryCode;
  298. $nvpstr = $nvpstr . "&IPADDRESS=" . $_SERVER['REMOTE_ADDR'];
  299.  
  300. $resArray=hash_call("DoDirectPayment", $nvpstr);
  301.  
  302. return $resArray;
  303. }
  304.  
  305.  
  306. /**
  307. * hash_call: Function to perform the API call to PayPal using API signature
  308. * @methodName is name of API method.
  309. * @nvpStr is nvp string.
  310. * returns an associtive array containing the response from the server.
  311. */
  312. function hash_call($methodName,$nvpStr)
  313. {
  314. //declaring of global variables
  315. global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;
  316. global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  317. global $gv_ApiErrorURL;
  318. global $sBNCode;
  319.  
  320. //setting the curl parameters.
  321. $ch = curl_init();
  322. curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
  323. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  324.  
  325. //turning off the server and peer verification(TrustManager Concept).
  326. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  327. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  328.  
  329. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  330. curl_setopt($ch, CURLOPT_POST, 1);
  331.  
  332. //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
  333. //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
  334. if($USE_PROXY)
  335. curl_setopt ($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
  336.  
  337. //NVPRequest for submitting to server
  338. $nvpreq="METHOD=" . urlencode($methodName) . "&VERSION=" . urlencode($version) . "&PWD=" . urlencode($API_Password) . "&USER=" . urlencode($API_UserName) . "&SIGNATURE=" . urlencode($API_Signature) . $nvpStr . "&BUTTONSOURCE=" . urlencode($sBNCode);
  339.  
  340. //setting the nvpreq as POST FIELD to curl
  341. curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
  342.  
  343. //getting response from server
  344. $response = curl_exec($ch);
  345.  
  346. //convrting NVPResponse to an Associative Array
  347. $nvpResArray=deformatNVP($response);
  348. $nvpReqArray=deformatNVP($nvpreq);
  349. $_SESSION['nvpReqArray']=$nvpReqArray;
  350.  
  351. if (curl_errno($ch))
  352. {
  353. // moving to display page to display curl errors
  354. $_SESSION['curl_error_no']=curl_errno($ch) ;
  355. $_SESSION['curl_error_msg']=curl_error($ch);
  356.  
  357. //Execute the Error handling module to display errors.
  358. }
  359. else
  360. {
  361. //closing the curl
  362. curl_close($ch);
  363. }
  364.  
  365. return $nvpResArray;
  366. }
  367.  
  368. /*'----------------------------------------------------------------------------------
  369. Purpose: Redirects to PayPal.com site.
  370. Inputs: NVP string.
  371. Returns:
  372. ----------------------------------------------------------------------------------
  373. */
  374. function RedirectToPayPal ( $token )
  375. {
  376. global $PAYPAL_URL;
  377.  
  378. // Redirect to paypal.com here
  379. $payPalURL = $PAYPAL_URL . $token;
  380. header("Location: ".$payPalURL);
  381. }
  382.  
  383.  
  384. /*'----------------------------------------------------------------------------------
  385. * This function will take NVPString and convert it to an Associative Array and it will decode the response.
  386. * It is usefull to search for a particular key and displaying arrays.
  387. * @nvpstr is NVPString.
  388. * @nvpArray is Associative Array.
  389. ----------------------------------------------------------------------------------
  390. */
  391. function deformatNVP($nvpstr)
  392. {
  393. $intial=0;
  394. $nvpArray = array();
  395.  
  396. while(strlen($nvpstr))
  397. {
  398. //postion of Key
  399. $keypos= strpos($nvpstr,'=');
  400. //position of value
  401. $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);
  402.  
  403. /*getting the Key and Value values and storing in a Associative Array*/
  404. $keyval=substr($nvpstr,$intial,$keypos);
  405. $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
  406. //decoding the respose
  407. $nvpArray[urldecode($keyval)] =urldecode( $valval);
  408. $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
  409. }
  410. return $nvpArray;
  411. }
  412.  
  413. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement