Advertisement
Guest User

Untitled

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