Advertisement
Guest User

Untitled

a guest
May 27th, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.98 KB | None | 0 0
  1. <?php
  2.  
  3. defined('BASEPATH') || exit('No direct script access allowed');
  4.  
  5. /**
  6. * Billing Controller
  7. */
  8. class Billing extends Admin_Controller {
  9.  
  10. protected $PROXY_HOST;
  11. protected $PROXY_PORT;
  12. protected $SandboxFlag;
  13. protected $API_UserName;
  14. protected $API_Password;
  15. protected $API_Signature;
  16. protected $sBNCode;
  17. protected $API_Endpoint;
  18. protected $PAYPAL_URL;
  19. protected $USE_PROXY;
  20. protected $version;
  21.  
  22. /**
  23. * Constructor
  24. *
  25. * @return void
  26. */
  27. public function __construct() {
  28. parent::__construct();
  29. $this->load->model('dashboard/billing_model');
  30. $this->lang->load('dashboard');
  31. // Load helpers
  32. $this->load->helper('url');
  33.  
  34. /* * ******************************************
  35. PayPal API Module
  36.  
  37. Defines all the global variables and the wrapper public functions
  38. * ****************************************** */
  39. $this->PROXY_HOST = '127.0.0.1';
  40. $this->PROXY_PORT = '808';
  41.  
  42. $this->SandboxFlag = true;
  43.  
  44. //'------------------------------------
  45. //' PayPal API Credentials
  46. //' Replace <API_USERNAME> with your API Username
  47. //' Replace <API_PASSWORD> with your API Password
  48. //' Replace <API_SIGNATURE> with your Signature
  49. //'------------------------------------
  50. $this->API_UserName = "contact.marketads-facilitator_api1.gmail.com";
  51. $this->API_Password = "XUNQMEZVEVERGAN2";
  52. $this->API_Signature = "AFcWxV21C7fd0v3bYYYRCpSSRl31AllyO.8F-Rd6WmJQQ-OH72l0sZu1";
  53.  
  54. // BN Code is only applicable for partners
  55. $this->sBNCode = "PP-ECWizard";
  56.  
  57.  
  58. /*
  59. ' Define the PayPal Redirect URLs.
  60. ' This is the URL that the buyer is first sent to do authorize payment with their paypal account
  61. ' change the URL depending if you are testing on the sandbox or the live PayPal site
  62. '
  63. ' For the sandbox, the URL is https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=
  64. ' For the live site, the URL is https://www.paypal.com/webscr&cmd=_express-checkout&token=
  65. */
  66.  
  67. if ($this->SandboxFlag == true) {
  68. $this->API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
  69. $this->PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
  70. } else {
  71. $this->API_Endpoint = "https://api-3t.paypal.com/nvp";
  72. $this->PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
  73. }
  74.  
  75. $this->USE_PROXY = false;
  76. $this->version = "93";
  77.  
  78. if (session_id() == "")
  79. session_start();
  80. Template::set_block('sub_nav', 'billing/_sub_nav');
  81. }
  82.  
  83. /**
  84. * Billing Index
  85. *
  86. * @return void
  87. */
  88. public function index() {
  89.  
  90. Template::set('toolbar_title', lang('billing_manage'));
  91. Template::render();
  92. }
  93.  
  94. public function deposit() {
  95.  
  96.  
  97. Template::set('toolbar_title', lang('billing_manage'));
  98. Template::render();
  99. }
  100.  
  101. public function checkout() {
  102. if (isset($_POST['continue'])) {
  103. $amount = (int) $this->input->post('amount');
  104. $this->session->set_userdata('Payment_Amount', $amount);
  105. Template::set('toolbar_title', lang('billing_manage'));
  106. Template::set('amount', $amount);
  107. Template::render();
  108. } else {
  109. die('INVALID REQUEST!');
  110. }
  111. }
  112.  
  113. public function cancel() {
  114.  
  115. die('Transaction Canceled!');
  116. }
  117.  
  118. public function confirm() {
  119. if (!isset($_POST['confirm'])) {
  120. $amount = $this->session->userdata('Payment_Amount');
  121. Template::set('toolbar_title', lang('billing_manage'));
  122. Template::set('amount', $amount);
  123. Template::render();
  124. } else {
  125. $PaymentOption = "PayPal";
  126. if ($PaymentOption == "PayPal") {
  127. // ==================================
  128. // PayPal Express Checkout Module
  129. // ==================================
  130. //'------------------------------------
  131. //' The paymentAmount is the total value of
  132. //' the shopping cart, that was set
  133. //' earlier in a session variable
  134. //' by the shopping cart page
  135. //'------------------------------------
  136. $paymentAmount = $this->session->userdata("Payment_Amount");
  137.  
  138. //'------------------------------------
  139. //' When you integrate this code
  140. //' set the variables below with
  141. //' shipping address details
  142. //' entered by the user on the
  143. //' Shipping page.
  144. //'------------------------------------
  145. $shipToName = "";
  146. $shipToStreet = "";
  147. $shipToStreet2 = ""; //Leave it blank if there is no value
  148. $shipToCity = "";
  149. $shipToState = "";
  150. $shipToCountryCode = ""; // Please refer to the PayPal country codes in the API documentation
  151. $shipToZip = "";
  152. $phoneNum = "";
  153.  
  154. //'------------------------------------
  155. //' The currencyCodeType and paymentType
  156. //' are set to the selections made on the Integration Assistant
  157. //'------------------------------------
  158. $currencyCodeType = "USD";
  159. $paymentType = "Sale";
  160.  
  161. //'------------------------------------
  162. //' The returnURL is the location where buyers return to when a
  163. //' payment has been succesfully authorized.
  164. //'
  165. //' This is set to the value entered on the Integration Assistant
  166. //'------------------------------------
  167. $returnURL = "http://marketads.net/app/dashboard/billing/paymentcomplete";
  168.  
  169. //'------------------------------------
  170. //' The cancelURL is the location buyers are sent to when they hit the
  171. //' cancel button during authorization of payment during the PayPal flow
  172. //'
  173. //' This is set to the value entered on the Integration Assistant
  174. //'------------------------------------
  175. $cancelURL = "http://marketads.net/app/dashboard/billing/cancel";
  176.  
  177. //'------------------------------------
  178. //' Calls the SetExpressCheckout API call
  179. //'
  180. //' The CallMarkExpressCheckout function is defined in the file PayPalFunctions.php,
  181. //' it is included at the top of this file.
  182. //'-------------------------------------------------
  183. $resArray = $this->CallMarkExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum
  184. );
  185.  
  186. $ack = strtoupper($resArray["ACK"]);
  187. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  188. $token = urldecode($resArray["TOKEN"]);
  189. $_SESSION['reshash'] = $token;
  190. $this->RedirectToPayPal($token);
  191. } else {
  192. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  193. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  194. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  195. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  196. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  197.  
  198. echo "SetExpressCheckout API call failed. ";
  199. echo "Detailed Error Message: " . $ErrorLongMsg;
  200. echo "Short Error Message: " . $ErrorShortMsg;
  201. echo "Error Code: " . $ErrorCode;
  202. echo "Error Severity Code: " . $ErrorSeverityCode;
  203. }
  204. } else {
  205. if ((( $PaymentOption == "Visa") || ( $PaymentOption == "MasterCard") || ($PaymentOption == "Amex") || ($PaymentOption == "Discover")) && ( $PaymentProcessorSelected == "PayPal Direct Payment"))
  206.  
  207. //'------------------------------------
  208. //' The paymentAmount is the total value of
  209. //' the shopping cart, that was set
  210. //' earlier in a session variable
  211. //' by the shopping cart page
  212. //'------------------------------------
  213. $paymentAmount = $_SESSION["Payment_Amount"];
  214.  
  215. //'------------------------------------
  216. //' The currencyCodeType and paymentType
  217. //' are set to the selections made on the Integration Assistant
  218. //'------------------------------------
  219. $currencyCodeType = "USD";
  220. $paymentType = "Sale";
  221.  
  222. //' Set these values based on what was selected by the user on the Billing page Html form
  223.  
  224. $creditCardType = "<<Visa/MasterCard/Amex/Discover>>"; //' Set this to one of the acceptable values (Visa/MasterCard/Amex/Discover) match it to what was selected on your Billing page
  225. $creditCardNumber = "<<CC number>>"; //' Set this to the string entered as the credit card number on the Billing page
  226. $expDate = "<<Expiry Date>>"; //' Set this to the credit card expiry date entered on the Billing page
  227. $cvv2 = "<<cvv2>>"; //' Set this to the CVV2 string entered on the Billing page
  228. $firstName = "<<firstName>>"; //' Set this to the customer's first name that was entered on the Billing page
  229. $lastName = "<<lastName>>"; //' Set this to the customer's last name that was entered on the Billing page
  230. $street = "<<street>>"; //' Set this to the customer's street address that was entered on the Billing page
  231. $city = "<<city>>"; //' Set this to the customer's city that was entered on the Billing page
  232. $state = "<<state>>"; //' Set this to the customer's state that was entered on the Billing page
  233. $zip = "<<zip>>"; //' Set this to the zip code of the customer's address that was entered on the Billing page
  234. $countryCode = "<<PayPal Country Code>>"; //' Set this to the PayPal code for the Country of the customer's address that was entered on the Billing page
  235. $currencyCode = "<<PayPal Currency Code>>"; //' Set this to the PayPal code for the Currency used by the customer
  236.  
  237. /*
  238. '------------------------------------------------
  239. ' Calls the DoDirectPayment API call
  240. '
  241. ' The DirectPayment function is defined in PayPalFunctions.php included at the top of this file.
  242. '-------------------------------------------------
  243. */
  244.  
  245. $resArray = DirectPayment($paymentType, $paymentAmount, $creditCardType, $creditCardNumber, $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip, $countryCode, $currencyCode);
  246.  
  247. $ack = strtoupper($resArray["ACK"]);
  248. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  249. //Getting transaction ID from API responce.
  250. $TransactionID = urldecode($resArray["TRANSACTIONID"]);
  251.  
  252. echo "Your payment has been successfully processed";
  253. } else {
  254. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  255. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  256. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  257. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  258. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  259.  
  260. echo "Direct credit card payment API call failed. ";
  261. echo "Detailed Error Message: " . $ErrorLongMsg;
  262. echo "Short Error Message: " . $ErrorShortMsg;
  263. echo "Error Code: " . $ErrorCode;
  264. echo "Error Severity Code: " . $ErrorSeverityCode;
  265. }
  266. }
  267. }
  268. }
  269.  
  270. public function payment_done() {
  271.  
  272. $token = $this->input->get('token');
  273.  
  274. // If the Request object contains the variable 'token' then it means that the user is coming from PayPal site.
  275. if ($token != "") {
  276.  
  277.  
  278. /*
  279. '------------------------------------
  280. ' Calls the GetExpressCheckoutDetails API call
  281. '
  282. ' The GetShippingDetails function is defined in PayPalFunctions.jsp
  283. ' included at the top of this file.
  284. '-------------------------------------------------
  285. */
  286.  
  287.  
  288. $resArray = $this->GetShippingDetails($token);
  289. $ack = strtoupper($resArray["ACK"]);
  290. if ($ack == "SUCCESS" || $ack == "SUCESSWITHWARNING") {
  291. /*
  292. ' The information that is returned by the GetExpressCheckoutDetails call should be integrated by the partner into his Order Review
  293. ' page
  294. */
  295. $email = $resArray["EMAIL"]; // ' Email address of payer.
  296. $payerId = $resArray["PAYERID"]; // ' Unique PayPal customer account identification number.
  297. $payerStatus = $resArray["PAYERSTATUS"]; // ' Status of payer. Character length and limitations: 10 single-byte alphabetic characters.
  298. // $salutation = $resArray["SALUTATION"]; // ' Payer's salutation.
  299. $firstName = $resArray["FIRSTNAME"]; // ' Payer's first name.
  300. // $middleName = $resArray["MIDDLENAME"]; // ' Payer's middle name.
  301. $lastName = $resArray["LASTNAME"]; // ' Payer's last name.
  302. // $suffix = $resArray["SUFFIX"]; // ' Payer's suffix.
  303. $cntryCode = $resArray["COUNTRYCODE"]; // ' Payer's country of residence in the form of ISO standard 3166 two-character country codes.
  304. $business = $resArray["BUSINESS"]; // ' Payer's business name.
  305. $shipToName = $resArray["PAYMENTREQUEST_0_SHIPTONAME"]; // ' Person's name associated with this address.
  306. $shipToStreet = $resArray["PAYMENTREQUEST_0_SHIPTOSTREET"]; // ' First street address.
  307. // $shipToStreet2 = $resArray["PAYMENTREQUEST_0_SHIPTOSTREET2"]; // ' Second street address.
  308. $shipToCity = $resArray["PAYMENTREQUEST_0_SHIPTOCITY"]; // ' Name of city.
  309. $shipToState = $resArray["PAYMENTREQUEST_0_SHIPTOSTATE"]; // ' State or province
  310. $shipToCntryCode = $resArray["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"]; // ' Country code.
  311. $shipToZip = $resArray["PAYMENTREQUEST_0_SHIPTOZIP"]; // ' U.S. Zip code or other country-specific postal code.
  312. $addressStatus = $resArray["ADDRESSSTATUS"]; // ' Status of street address on file with PayPal
  313. // $invoiceNumber = $resArray["INVNUM"]; // ' Your own invoice or tracking number, as set by you in the element of the same name in SetExpressCheckout request .
  314. // $phonNumber = $resArray["PHONENUM"]; // ' Payer's contact telephone number. Note: PayPal returns a contact telephone number only if your Merchant account profile settings require that the buyer enter one.
  315. } else {
  316. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  317. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  318. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  319. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  320. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  321.  
  322. echo "GetExpressCheckoutDetails API call failed. ";
  323. echo "Detailed Error Message: " . $ErrorLongMsg;
  324. echo "Short Error Message: " . $ErrorShortMsg;
  325. echo "Error Code: " . $ErrorCode;
  326. echo "Error Severity Code: " . $ErrorSeverityCode;
  327. }
  328. }
  329. $PaymentOption = "PayPal";
  330. if ($PaymentOption == "PayPal") {
  331. /*
  332. '------------------------------------
  333. ' The paymentAmount is the total value of
  334. ' the shopping cart, that was set
  335. ' earlier in a session variable
  336. ' by the shopping cart page
  337. '------------------------------------
  338. */
  339.  
  340. $finalPaymentAmount = $this->session->userdata("Payment_Amount");
  341.  
  342. /*
  343. '------------------------------------
  344. ' Calls the DoExpressCheckoutPayment API call
  345. '
  346. ' The ConfirmPayment function is defined in the file PayPalFunctions.jsp,
  347. ' that is included at the top of this file.
  348. '-------------------------------------------------
  349. */
  350.  
  351. $resArray = $this->ConfirmPayment($finalPaymentAmount);
  352. $ack = strtoupper($resArray["ACK"]);
  353. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  354. /*
  355. '********************************************************************************************************************
  356. '
  357. ' THE PARTNER SHOULD SAVE THE KEY TRANSACTION RELATED INFORMATION LIKE
  358. ' transactionId & orderTime
  359. ' IN THEIR OWN DATABASE
  360. ' AND THE REST OF THE INFORMATION CAN BE USED TO UNDERSTAND THE STATUS OF THE PAYMENT
  361. '
  362. '********************************************************************************************************************
  363. */
  364.  
  365. $transactionId = $resArray["PAYMENTINFO_0_TRANSACTIONID"]; // ' Unique transaction ID of the payment. Note: If the PaymentAction of the request was Authorization or Order, this value is your AuthorizationID for use with the Authorization & Capture APIs.
  366. $transactionType = $resArray["PAYMENTINFO_0_TRANSACTIONTYPE"]; //' The type of transaction Possible values: l cart l express-checkout
  367. $paymentType = $resArray["PAYMENTINFO_0_PAYMENTTYPE"]; //' Indicates whether the payment is instant or delayed. Possible values: l none l echeck l instant
  368. $orderTime = $resArray["PAYMENTINFO_0_ORDERTIME"]; //' Time/date stamp of payment
  369. $amt = $resArray["PAYMENTINFO_0_AMT"]; //' The final amount charged, including any shipping and taxes from your Merchant Profile.
  370. $currencyCode = $resArray["PAYMENTINFO_0_CURRENCYCODE"]; //' A three-character currency code for one of the currencies listed in PayPay-Supported Transactional Currencies. Default: USD.
  371. $feeAmt = $resArray["PAYMENTINFO_0_FEEAMT"]; //' PayPal fee amount charged for the transaction
  372. // $settleAmt = $resArray["PAYMENTINFO_0_SETTLEAMT"]; //' Amount deposited in your PayPal account after a currency conversion.
  373. $taxAmt = $resArray["PAYMENTINFO_0_TAXAMT"]; //' Tax charged on the transaction.
  374. // $exchangeRate = $resArray["PAYMENTINFO_0_EXCHANGERATE"]; //' Exchange rate if a currency conversion occurred. Relevant only if your are billing in their non-primary currency. If the customer chooses to pay with a currency other than the non-primary currency, the conversion occurs in the customer's account.
  375.  
  376. /*
  377. ' Status of the payment:
  378. 'Completed: The payment has been completed, and the funds have been added successfully to your account balance.
  379. 'Pending: The payment is pending. See the PendingReason element for more information.
  380. */
  381.  
  382. $paymentStatus = $resArray["PAYMENTINFO_0_PAYMENTSTATUS"];
  383.  
  384. /*
  385. 'The reason the payment is pending:
  386. ' none: No pending reason
  387. ' address: The payment is pending because your customer did not include a confirmed shipping address and your Payment Receiving Preferences is set such that you want to manually accept or deny each of these payments. To change your preference, go to the Preferences section of your Profile.
  388. ' echeck: The payment is pending because it was made by an eCheck that has not yet cleared.
  389. ' intl: The payment is pending because you hold a non-U.S. account and do not have a withdrawal mechanism. You must manually accept or deny this payment from your Account Overview.
  390. ' multi-currency: You do not have a balance in the currency sent, and you do not have your Payment Receiving Preferences set to automatically convert and accept this payment. You must manually accept or deny this payment.
  391. ' verify: The payment is pending because you are not yet verified. You must verify your account before you can accept this payment.
  392. ' other: The payment is pending for a reason other than those listed above. For more information, contact PayPal customer service.
  393. */
  394.  
  395. $pendingReason = $resArray["PAYMENTINFO_0_PENDINGREASON"];
  396.  
  397. /*
  398. 'The reason for a reversal if TransactionType is reversal:
  399. ' none: No reason code
  400. ' chargeback: A reversal has occurred on this transaction due to a chargeback by your customer.
  401. ' guarantee: A reversal has occurred on this transaction due to your customer triggering a money-back guarantee.
  402. ' buyer-complaint: A reversal has occurred on this transaction due to a complaint about the transaction from your customer.
  403. ' refund: A reversal has occurred on this transaction because you have given the customer a refund.
  404. ' other: A reversal has occurred on this transaction due to a reason not listed above.
  405. */
  406.  
  407. $reasonCode = $resArray["PAYMENTINFO_0_REASONCODE"];
  408.  
  409. redirect(site_url('dashboard/billing/success'));
  410. } else {
  411. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  412. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  413. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  414. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  415. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  416.  
  417. echo "GetExpressCheckoutDetails API call failed. ";
  418. echo "Detailed Error Message: " . $ErrorLongMsg;
  419. echo "Short Error Message: " . $ErrorShortMsg;
  420. echo "Error Code: " . $ErrorCode;
  421. echo "Error Severity Code: " . $ErrorSeverityCode;
  422. }
  423. }
  424. }
  425.  
  426. public function success() {
  427. print_r($this->session->all_userdata()); die;
  428. Template::set('toolbar_title', lang('billing_manage'));
  429. Template::render();
  430. }
  431.  
  432. public function paypal_checkout() {
  433.  
  434.  
  435. // ==================================
  436. // PayPal Express Checkout Module
  437. // ==================================
  438. //'------------------------------------
  439. //' The paymentAmount is the total value of
  440. //' the shopping cart, that was set
  441. //' earlier in a session variable
  442. //' by the shopping cart page
  443. //'------------------------------------
  444. $paymentAmount = $this->session->userdata('Payment_Amount');
  445. //'------------------------------------
  446. //' The currencyCodeType and paymentType
  447. //' are set to the selections made on the Integration Assistant
  448. //'------------------------------------
  449. $currencyCodeType = "USD";
  450. $paymentType = "Sale";
  451.  
  452. //'------------------------------------
  453. //' The returnURL is the location where buyers return to when a
  454. //' payment has been succesfully authorized.
  455. //'
  456. //' This is set to the value entered on the Integration Assistant
  457. //'------------------------------------
  458. $returnURL = "http://marketads.net/app/dashboard/billing/confirm";
  459.  
  460. //'------------------------------------
  461. //' The cancelURL is the location buyers are sent to when they hit the
  462. //' cancel button during authorization of payment during the PayPal flow
  463. //'
  464. //' This is set to the value entered on the Integration Assistant
  465. //'------------------------------------
  466. $cancelURL = "http://marketads.net/app/dashboard/billing/cancel";
  467.  
  468. //'------------------------------------
  469. //' Calls the SetExpressCheckout API call
  470. //'
  471. //' The CallShortcutExpressCheckout public function is defined in the file PayPalFunctions.php,
  472. //' it is included at the top of this file.
  473. //'-------------------------------------------------
  474.  
  475. $resArray = $this->CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL);
  476. $ack = strtoupper($resArray["ACK"]);
  477. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  478. $this->RedirectToPayPal($resArray["TOKEN"]);
  479. } else {
  480. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  481. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  482. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  483. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  484. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  485.  
  486. echo "SetExpressCheckout API call failed. ";
  487. echo "Detailed Error Message: " . $ErrorLongMsg;
  488. echo "Short Error Message: " . $ErrorShortMsg;
  489. echo "Error Code: " . $ErrorCode;
  490. echo "Error Severity Code: " . $ErrorSeverityCode;
  491. }
  492. }
  493.  
  494. private function CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL) {
  495. //------------------------------------------------------------------------------------------------------------------------------------
  496. // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
  497. $nvpstr = "&PAYMENTREQUEST_0_AMT=" . $paymentAmount;
  498. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
  499. $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
  500. $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
  501. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
  502.  
  503. $this->session->set_userdata("currencyCodeType", $currencyCodeType);
  504. $this->session->set_userdata("PaymentType", $paymentType);
  505.  
  506. //'---------------------------------------------------------------------------------------------------------------
  507. //' Make the API call to PayPal
  508. //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
  509. //' If an error occured, show the resulting errors
  510. //'---------------------------------------------------------------------------------------------------------------
  511. $resArray = $this->hash_call("SetExpressCheckout", $nvpstr);
  512. $ack = strtoupper($resArray["ACK"]);
  513. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  514. $token = urldecode($resArray["TOKEN"]);
  515. $this->session->set_userdata('TOKEN', $token);
  516. }
  517.  
  518. return $resArray;
  519. }
  520.  
  521. /*
  522. '-------------------------------------------------------------------------------------------------------------------------------------------
  523. ' Purpose: Prepares the parameters for the SetExpressCheckout API Call.
  524. ' Inputs:
  525. ' paymentAmount: Total value of the shopping cart
  526. ' currencyCodeType: Currency code value the PayPal API
  527. ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  528. ' returnURL: the page where buyers return to after they are done with the payment review on PayPal
  529. ' cancelURL: the page where buyers return to when they cancel the payment review on PayPal
  530. ' shipToName: the Ship to name entered on the merchant's site
  531. ' shipToStreet: the Ship to Street entered on the merchant's site
  532. ' shipToCity: the Ship to City entered on the merchant's site
  533. ' shipToState: the Ship to State entered on the merchant's site
  534. ' shipToCountryCode: the Code for Ship to Country entered on the merchant's site
  535. ' shipToZip: the Ship to ZipCode entered on the merchant's site
  536. ' shipToStreet2: the Ship to Street2 entered on the merchant's site
  537. ' phoneNum: the phoneNum entered on the merchant's site
  538. '--------------------------------------------------------------------------------------------------------------------------------------------
  539. */
  540.  
  541. private function CallMarkExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum
  542. ) {
  543. //------------------------------------------------------------------------------------------------------------------------------------
  544. // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
  545.  
  546. $nvpstr = "&PAYMENTREQUEST_0_AMT=" . $paymentAmount;
  547. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
  548. $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
  549. $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
  550. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
  551. $nvpstr = $nvpstr . "&ADDROVERRIDE=1";
  552. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTONAME=" . $shipToName;
  553. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTREET=" . $shipToStreet;
  554. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTREET2=" . $shipToStreet2;
  555. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOCITY=" . $shipToCity;
  556. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTATE=" . $shipToState;
  557. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE=" . $shipToCountryCode;
  558. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOZIP=" . $shipToZip;
  559. $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOPHONENUM=" . $phoneNum;
  560.  
  561. $this->session->set_userdata("currencyCodeType", $currencyCodeType);
  562. $this->session->set_userdata("PaymentType", $paymentType);
  563.  
  564. //'---------------------------------------------------------------------------------------------------------------
  565. //' Make the API call to PayPal
  566. //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
  567. //' If an error occured, show the resulting errors
  568. //'---------------------------------------------------------------------------------------------------------------
  569. $resArray = $this->hash_call("SetExpressCheckout", $nvpstr);
  570. $ack = strtoupper($resArray["ACK"]);
  571. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  572. $token = urldecode($resArray["TOKEN"]);
  573. $this->session->set_userdata('TOKEN', $token);
  574. }
  575.  
  576. return $resArray;
  577. }
  578.  
  579. /*
  580. '-------------------------------------------------------------------------------------------
  581. ' Purpose: Prepares the parameters for the GetExpressCheckoutDetails API Call.
  582. '
  583. ' Inputs:
  584. ' None
  585. ' Returns:
  586. ' The NVP Collection object of the GetExpressCheckoutDetails Call Response.
  587. '-------------------------------------------------------------------------------------------
  588. */
  589.  
  590. private function GetShippingDetails($token) {
  591. //'--------------------------------------------------------------
  592. //' At this point, the buyer has completed authorizing the payment
  593. //' at PayPal. The public function will call PayPal to obtain the details
  594. //' of the authorization, incuding any shipping information of the
  595. //' buyer. Remember, the authorization is not a completed transaction
  596. //' at this state - the buyer still needs an additional step to finalize
  597. //' the transaction
  598. //'--------------------------------------------------------------
  599. //'---------------------------------------------------------------------------
  600. //' Build a second API request to PayPal, using the token as the
  601. //' ID to get the details on the payment authorization
  602. //'---------------------------------------------------------------------------
  603. $nvpstr = "&TOKEN=" . $token;
  604. //'---------------------------------------------------------------------------
  605. //' Make the API call and store the results in an array.
  606. //' If the call was a success, show the authorization details, and provide
  607. //' an action to complete the payment.
  608. //' If failed, show the error
  609. //'---------------------------------------------------------------------------
  610. $resArray = $this->hash_call("GetExpressCheckoutDetails", $nvpstr);
  611. $ack = strtoupper($resArray["ACK"]);
  612. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
  613. $this->session->set_userdata('payer_id', $resArray['PAYERID']);
  614. }
  615. return $resArray;
  616. }
  617.  
  618. /*
  619. '-------------------------------------------------------------------------------------------------------------------------------------------
  620. ' Purpose: Prepares the parameters for the GetExpressCheckoutDetails API Call.
  621. '
  622. ' Inputs:
  623. ' sBNCode: The BN code used by PayPal to track the transactions from a given shopping cart.
  624. ' Returns:
  625. ' The NVP Collection object of the GetExpressCheckoutDetails Call Response.
  626. '--------------------------------------------------------------------------------------------------------------------------------------------
  627. */
  628.  
  629. private function ConfirmPayment($FinalPaymentAmt) {
  630.  
  631. /* Gather the information to make the final call to
  632. finalize the PayPal payment. The variable nvpstr
  633. holds the name value pairs
  634. */
  635.  
  636.  
  637. //Format the other parameters that were stored in the session from the previous calls
  638. $token = urlencode($this->session->userdata('TOKEN'));
  639. $paymentType = urlencode($this->session->userdata('PaymentType'));
  640. $currencyCodeType = urlencode($this->session->userdata('currencyCodeType'));
  641. $payerID = urlencode($this->session->userdata('payer_id'));
  642.  
  643. $serverName = urlencode($_SERVER['SERVER_NAME']);
  644.  
  645. $nvpstr = '&TOKEN=' . $token . '&PAYERID=' . $payerID . '&PAYMENTREQUEST_0_PAYMENTACTION=' . $paymentType . '&PAYMENTREQUEST_0_AMT=' . $FinalPaymentAmt;
  646. $nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE=' . $currencyCodeType . '&IPADDRESS=' . $serverName;
  647.  
  648. /* Make the call to PayPal to finalize payment
  649. If an error occured, show the resulting errors
  650. */
  651. $resArray = $this->hash_call("DoExpressCheckoutPayment", $nvpstr);
  652.  
  653. /* Display the API response back to the browser.
  654. If the response from PayPal was a success, display the response parameters'
  655. If the response was an error, display the errors received using APIError.php.
  656. */
  657. $ack = strtoupper($resArray["ACK"]);
  658. return $resArray;
  659. }
  660.  
  661. /*
  662. '-------------------------------------------------------------------------------------------------------------------------------------------
  663. ' Purpose: This public function makes a DoDirectPayment API call
  664. '
  665. ' Inputs:
  666. ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  667. ' paymentAmount: total value of the shopping cart
  668. ' currencyCode: currency code value the PayPal API
  669. ' firstName: first name as it appears on credit card
  670. ' lastName: last name as it appears on credit card
  671. ' street: buyer's street address line as it appears on credit card
  672. ' city: buyer's city
  673. ' state: buyer's state
  674. ' countryCode: buyer's country code
  675. ' zip: buyer's zip
  676. ' creditCardType: buyer's credit card type (i.e. Visa, MasterCard ... )
  677. ' creditCardNumber: buyers credit card number without any spaces, dashes or any other characters
  678. ' expDate: credit card expiration date
  679. ' cvv2: Card Verification Value
  680. '
  681. '-------------------------------------------------------------------------------------------
  682. '
  683. ' Returns:
  684. ' The NVP Collection object of the DoDirectPayment Call Response.
  685. '--------------------------------------------------------------------------------------------------------------------------------------------
  686. */
  687.  
  688. private function DirectPayment($paymentType, $paymentAmount, $creditCardType, $creditCardNumber, $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip, $countryCode, $currencyCode) {
  689. //Construct the parameter string that describes DoDirectPayment
  690. $nvpstr = "&AMT=" . $paymentAmount;
  691. $nvpstr = $nvpstr . "&CURRENCYCODE=" . $currencyCode;
  692. $nvpstr = $nvpstr . "&PAYMENTACTION=" . $paymentType;
  693. $nvpstr = $nvpstr . "&CREDITCARDTYPE=" . $creditCardType;
  694. $nvpstr = $nvpstr . "&ACCT=" . $creditCardNumber;
  695. $nvpstr = $nvpstr . "&EXPDATE=" . $expDate;
  696. $nvpstr = $nvpstr . "&CVV2=" . $cvv2;
  697. $nvpstr = $nvpstr . "&FIRSTNAME=" . $firstName;
  698. $nvpstr = $nvpstr . "&LASTNAME=" . $lastName;
  699. $nvpstr = $nvpstr . "&STREET=" . $street;
  700. $nvpstr = $nvpstr . "&CITY=" . $city;
  701. $nvpstr = $nvpstr . "&STATE=" . $state;
  702. $nvpstr = $nvpstr . "&COUNTRYCODE=" . $countryCode;
  703. $nvpstr = $nvpstr . "&IPADDRESS=" . $_SERVER['REMOTE_ADDR'];
  704.  
  705. $resArray = $this->hash_call("DoDirectPayment", $nvpstr);
  706.  
  707. return $resArray;
  708. }
  709.  
  710. /**
  711. '-------------------------------------------------------------------------------------------------------------------------------------------
  712. * hash_call: Function to perform the API call to PayPal using API signature
  713. * @methodName is name of API method.
  714. * @nvpStr is nvp string.
  715. * returns an associtive array containing the response from the server.
  716. '-------------------------------------------------------------------------------------------------------------------------------------------
  717. */
  718. private function hash_call($methodName, $nvpStr) {
  719. //declaring of global variables
  720. // global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;
  721. // global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  722. // global $gv_ApiErrorURL;
  723. // global $sBNCode;
  724. //setting the curl parameters.
  725. $ch = curl_init();
  726. curl_setopt($ch, CURLOPT_URL, $this->API_Endpoint);
  727. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  728.  
  729. //turning off the server and peer verification(TrustManager Concept).
  730. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  731. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  732.  
  733. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  734. curl_setopt($ch, CURLOPT_POST, 1);
  735.  
  736. //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
  737. //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
  738. if ($this->USE_PROXY)
  739. curl_setopt($ch, CURLOPT_PROXY, $this->PROXY_HOST . ":" . $this->PROXY_PORT);
  740.  
  741. //NVPRequest for submitting to server
  742. $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);
  743.  
  744. //setting the nvpreq as POST FIELD to curl
  745. curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
  746.  
  747. //getting response from server
  748. $response = curl_exec($ch);
  749.  
  750. //convrting NVPResponse to an Associative Array
  751. $nvpResArray = $this->deformatNVP($response);
  752. $nvpReqArray = $this->deformatNVP($nvpreq);
  753. $this->session->set_userdata('nvpReqArray', $nvpReqArray);
  754.  
  755. if (curl_errno($ch)) {
  756. // moving to display page to display curl errors
  757. $this->session->set_userdata('curl_error_no', curl_errno($ch));
  758. $this->session->set_userdata('curl_error_msg', curl_error($ch));
  759.  
  760. //Execute the Error handling module to display errors.
  761. } else {
  762. //closing the curl
  763. curl_close($ch);
  764. }
  765.  
  766. return $nvpResArray;
  767. }
  768.  
  769. /* '----------------------------------------------------------------------------------
  770. Purpose: Redirects to PayPal.com site.
  771. Inputs: NVP string.
  772. Returns:
  773. ----------------------------------------------------------------------------------
  774. */
  775.  
  776. private function RedirectToPayPal($token) {
  777. // global $PAYPAL_URL;
  778. // Redirect to paypal.com here
  779. $payPalURL = $this->PAYPAL_URL . $token;
  780. header("Location: " . $payPalURL);
  781. exit;
  782. }
  783.  
  784. /* '----------------------------------------------------------------------------------
  785. * This public function will take NVPString and convert it to an Associative Array and it will decode the response.
  786. * It is usefull to search for a particular key and displaying arrays.
  787. * @nvpstr is NVPString.
  788. * @nvpArray is Associative Array.
  789. ----------------------------------------------------------------------------------
  790. */
  791.  
  792. private function deformatNVP($nvpstr) {
  793. $intial = 0;
  794. $nvpArray = array();
  795.  
  796. while (strlen($nvpstr)) {
  797. //postion of Key
  798. $keypos = strpos($nvpstr, '=');
  799. //position of value
  800. $valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr);
  801.  
  802. /* getting the Key and Value values and storing in a Associative Array */
  803. $keyval = substr($nvpstr, $intial, $keypos);
  804. $valval = substr($nvpstr, $keypos + 1, $valuepos - $keypos - 1);
  805. //decoding the respose
  806. $nvpArray[urldecode($keyval)] = urldecode($valval);
  807. $nvpstr = substr($nvpstr, $valuepos + 1, strlen($nvpstr));
  808. }
  809. return $nvpArray;
  810. }
  811.  
  812. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement