Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') || exit('No direct script access allowed');
- /**
- * Billing Controller
- */
- class Billing extends Admin_Controller {
- protected $PROXY_HOST;
- protected $PROXY_PORT;
- protected $SandboxFlag;
- protected $API_UserName;
- protected $API_Password;
- protected $API_Signature;
- protected $sBNCode;
- protected $API_Endpoint;
- protected $PAYPAL_URL;
- protected $USE_PROXY;
- protected $version;
- /**
- * Constructor
- *
- * @return void
- */
- public function __construct() {
- parent::__construct();
- $this->load->model('dashboard/billing_model');
- $this->lang->load('dashboard');
- // Load helpers
- $this->load->helper('url');
- /* * ******************************************
- PayPal API Module
- Defines all the global variables and the wrapper public functions
- * ****************************************** */
- $this->PROXY_HOST = '127.0.0.1';
- $this->PROXY_PORT = '808';
- $this->SandboxFlag = true;
- //'------------------------------------
- //' PayPal API Credentials
- //' Replace <API_USERNAME> with your API Username
- //' Replace <API_PASSWORD> with your API Password
- //' Replace <API_SIGNATURE> with your Signature
- //'------------------------------------
- $this->API_UserName = "contact.marketads-facilitator_api1.gmail.com";
- $this->API_Password = "XUNQMEZVEVERGAN2";
- $this->API_Signature = "AFcWxV21C7fd0v3bYYYRCpSSRl31AllyO.8F-Rd6WmJQQ-OH72l0sZu1";
- // BN Code is only applicable for partners
- $this->sBNCode = "PP-ECWizard";
- /*
- ' Define the PayPal Redirect URLs.
- ' This is the URL that the buyer is first sent to do authorize payment with their paypal account
- ' change the URL depending if you are testing on the sandbox or the live PayPal site
- '
- ' For the sandbox, the URL is https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=
- ' For the live site, the URL is https://www.paypal.com/webscr&cmd=_express-checkout&token=
- */
- if ($this->SandboxFlag == true) {
- $this->API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
- $this->PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
- } else {
- $this->API_Endpoint = "https://api-3t.paypal.com/nvp";
- $this->PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
- }
- $this->USE_PROXY = false;
- $this->version = "93";
- if (session_id() == "")
- session_start();
- Template::set_block('sub_nav', 'billing/_sub_nav');
- }
- /**
- * Billing Index
- *
- * @return void
- */
- public function index() {
- Template::set('toolbar_title', lang('billing_manage'));
- Template::render();
- }
- public function deposit() {
- Template::set('toolbar_title', lang('billing_manage'));
- Template::render();
- }
- public function checkout() {
- if (isset($_POST['continue'])) {
- $amount = (int) $this->input->post('amount');
- $this->session->set_userdata('Payment_Amount', $amount);
- Template::set('toolbar_title', lang('billing_manage'));
- Template::set('amount', $amount);
- Template::render();
- } else {
- die('INVALID REQUEST!');
- }
- }
- public function cancel() {
- die('Transaction Canceled!');
- }
- public function confirm() {
- if (!isset($_POST['confirm'])) {
- $amount = $this->session->userdata('Payment_Amount');
- Template::set('toolbar_title', lang('billing_manage'));
- Template::set('amount', $amount);
- Template::render();
- } else {
- $PaymentOption = "PayPal";
- if ($PaymentOption == "PayPal") {
- // ==================================
- // PayPal Express Checkout Module
- // ==================================
- //'------------------------------------
- //' The paymentAmount is the total value of
- //' the shopping cart, that was set
- //' earlier in a session variable
- //' by the shopping cart page
- //'------------------------------------
- $paymentAmount = $this->session->userdata("Payment_Amount");
- //'------------------------------------
- //' When you integrate this code
- //' set the variables below with
- //' shipping address details
- //' entered by the user on the
- //' Shipping page.
- //'------------------------------------
- $shipToName = "";
- $shipToStreet = "";
- $shipToStreet2 = ""; //Leave it blank if there is no value
- $shipToCity = "";
- $shipToState = "";
- $shipToCountryCode = ""; // Please refer to the PayPal country codes in the API documentation
- $shipToZip = "";
- $phoneNum = "";
- //'------------------------------------
- //' The currencyCodeType and paymentType
- //' are set to the selections made on the Integration Assistant
- //'------------------------------------
- $currencyCodeType = "USD";
- $paymentType = "Sale";
- //'------------------------------------
- //' The returnURL is the location where buyers return to when a
- //' payment has been succesfully authorized.
- //'
- //' This is set to the value entered on the Integration Assistant
- //'------------------------------------
- $returnURL = "http://marketads.net/app/dashboard/billing/paymentcomplete";
- //'------------------------------------
- //' The cancelURL is the location buyers are sent to when they hit the
- //' cancel button during authorization of payment during the PayPal flow
- //'
- //' This is set to the value entered on the Integration Assistant
- //'------------------------------------
- $cancelURL = "http://marketads.net/app/dashboard/billing/cancel";
- //'------------------------------------
- //' Calls the SetExpressCheckout API call
- //'
- //' The CallMarkExpressCheckout function is defined in the file PayPalFunctions.php,
- //' it is included at the top of this file.
- //'-------------------------------------------------
- $resArray = $this->CallMarkExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum
- );
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- $token = urldecode($resArray["TOKEN"]);
- $_SESSION['reshash'] = $token;
- $this->RedirectToPayPal($token);
- } else {
- //Display a user friendly Error on the page using any of the following error information returned by PayPal
- $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
- $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
- $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
- $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
- echo "SetExpressCheckout API call failed. ";
- echo "Detailed Error Message: " . $ErrorLongMsg;
- echo "Short Error Message: " . $ErrorShortMsg;
- echo "Error Code: " . $ErrorCode;
- echo "Error Severity Code: " . $ErrorSeverityCode;
- }
- } else {
- if ((( $PaymentOption == "Visa") || ( $PaymentOption == "MasterCard") || ($PaymentOption == "Amex") || ($PaymentOption == "Discover")) && ( $PaymentProcessorSelected == "PayPal Direct Payment"))
- //'------------------------------------
- //' The paymentAmount is the total value of
- //' the shopping cart, that was set
- //' earlier in a session variable
- //' by the shopping cart page
- //'------------------------------------
- $paymentAmount = $_SESSION["Payment_Amount"];
- //'------------------------------------
- //' The currencyCodeType and paymentType
- //' are set to the selections made on the Integration Assistant
- //'------------------------------------
- $currencyCodeType = "USD";
- $paymentType = "Sale";
- //' Set these values based on what was selected by the user on the Billing page Html form
- $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
- $creditCardNumber = "<<CC number>>"; //' Set this to the string entered as the credit card number on the Billing page
- $expDate = "<<Expiry Date>>"; //' Set this to the credit card expiry date entered on the Billing page
- $cvv2 = "<<cvv2>>"; //' Set this to the CVV2 string entered on the Billing page
- $firstName = "<<firstName>>"; //' Set this to the customer's first name that was entered on the Billing page
- $lastName = "<<lastName>>"; //' Set this to the customer's last name that was entered on the Billing page
- $street = "<<street>>"; //' Set this to the customer's street address that was entered on the Billing page
- $city = "<<city>>"; //' Set this to the customer's city that was entered on the Billing page
- $state = "<<state>>"; //' Set this to the customer's state that was entered on the Billing page
- $zip = "<<zip>>"; //' Set this to the zip code of the customer's address that was entered on the Billing page
- $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
- $currencyCode = "<<PayPal Currency Code>>"; //' Set this to the PayPal code for the Currency used by the customer
- /*
- '------------------------------------------------
- ' Calls the DoDirectPayment API call
- '
- ' The DirectPayment function is defined in PayPalFunctions.php included at the top of this file.
- '-------------------------------------------------
- */
- $resArray = DirectPayment($paymentType, $paymentAmount, $creditCardType, $creditCardNumber, $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip, $countryCode, $currencyCode);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- //Getting transaction ID from API responce.
- $TransactionID = urldecode($resArray["TRANSACTIONID"]);
- echo "Your payment has been successfully processed";
- } else {
- //Display a user friendly Error on the page using any of the following error information returned by PayPal
- $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
- $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
- $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
- $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
- echo "Direct credit card payment API call failed. ";
- echo "Detailed Error Message: " . $ErrorLongMsg;
- echo "Short Error Message: " . $ErrorShortMsg;
- echo "Error Code: " . $ErrorCode;
- echo "Error Severity Code: " . $ErrorSeverityCode;
- }
- }
- }
- }
- public function payment_done() {
- $token = $this->input->get('token');
- // If the Request object contains the variable 'token' then it means that the user is coming from PayPal site.
- if ($token != "") {
- /*
- '------------------------------------
- ' Calls the GetExpressCheckoutDetails API call
- '
- ' The GetShippingDetails function is defined in PayPalFunctions.jsp
- ' included at the top of this file.
- '-------------------------------------------------
- */
- $resArray = $this->GetShippingDetails($token);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCESSWITHWARNING") {
- /*
- ' The information that is returned by the GetExpressCheckoutDetails call should be integrated by the partner into his Order Review
- ' page
- */
- $email = $resArray["EMAIL"]; // ' Email address of payer.
- $payerId = $resArray["PAYERID"]; // ' Unique PayPal customer account identification number.
- $payerStatus = $resArray["PAYERSTATUS"]; // ' Status of payer. Character length and limitations: 10 single-byte alphabetic characters.
- // $salutation = $resArray["SALUTATION"]; // ' Payer's salutation.
- $firstName = $resArray["FIRSTNAME"]; // ' Payer's first name.
- // $middleName = $resArray["MIDDLENAME"]; // ' Payer's middle name.
- $lastName = $resArray["LASTNAME"]; // ' Payer's last name.
- // $suffix = $resArray["SUFFIX"]; // ' Payer's suffix.
- $cntryCode = $resArray["COUNTRYCODE"]; // ' Payer's country of residence in the form of ISO standard 3166 two-character country codes.
- $business = $resArray["BUSINESS"]; // ' Payer's business name.
- $shipToName = $resArray["PAYMENTREQUEST_0_SHIPTONAME"]; // ' Person's name associated with this address.
- $shipToStreet = $resArray["PAYMENTREQUEST_0_SHIPTOSTREET"]; // ' First street address.
- // $shipToStreet2 = $resArray["PAYMENTREQUEST_0_SHIPTOSTREET2"]; // ' Second street address.
- $shipToCity = $resArray["PAYMENTREQUEST_0_SHIPTOCITY"]; // ' Name of city.
- $shipToState = $resArray["PAYMENTREQUEST_0_SHIPTOSTATE"]; // ' State or province
- $shipToCntryCode = $resArray["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"]; // ' Country code.
- $shipToZip = $resArray["PAYMENTREQUEST_0_SHIPTOZIP"]; // ' U.S. Zip code or other country-specific postal code.
- $addressStatus = $resArray["ADDRESSSTATUS"]; // ' Status of street address on file with PayPal
- // $invoiceNumber = $resArray["INVNUM"]; // ' Your own invoice or tracking number, as set by you in the element of the same name in SetExpressCheckout request .
- // $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.
- } else {
- //Display a user friendly Error on the page using any of the following error information returned by PayPal
- $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
- $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
- $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
- $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
- echo "GetExpressCheckoutDetails API call failed. ";
- echo "Detailed Error Message: " . $ErrorLongMsg;
- echo "Short Error Message: " . $ErrorShortMsg;
- echo "Error Code: " . $ErrorCode;
- echo "Error Severity Code: " . $ErrorSeverityCode;
- }
- }
- $PaymentOption = "PayPal";
- if ($PaymentOption == "PayPal") {
- /*
- '------------------------------------
- ' The paymentAmount is the total value of
- ' the shopping cart, that was set
- ' earlier in a session variable
- ' by the shopping cart page
- '------------------------------------
- */
- $finalPaymentAmount = $this->session->userdata("Payment_Amount");
- /*
- '------------------------------------
- ' Calls the DoExpressCheckoutPayment API call
- '
- ' The ConfirmPayment function is defined in the file PayPalFunctions.jsp,
- ' that is included at the top of this file.
- '-------------------------------------------------
- */
- $resArray = $this->ConfirmPayment($finalPaymentAmount);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- /*
- '********************************************************************************************************************
- '
- ' THE PARTNER SHOULD SAVE THE KEY TRANSACTION RELATED INFORMATION LIKE
- ' transactionId & orderTime
- ' IN THEIR OWN DATABASE
- ' AND THE REST OF THE INFORMATION CAN BE USED TO UNDERSTAND THE STATUS OF THE PAYMENT
- '
- '********************************************************************************************************************
- */
- $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.
- $transactionType = $resArray["PAYMENTINFO_0_TRANSACTIONTYPE"]; //' The type of transaction Possible values: l cart l express-checkout
- $paymentType = $resArray["PAYMENTINFO_0_PAYMENTTYPE"]; //' Indicates whether the payment is instant or delayed. Possible values: l none l echeck l instant
- $orderTime = $resArray["PAYMENTINFO_0_ORDERTIME"]; //' Time/date stamp of payment
- $amt = $resArray["PAYMENTINFO_0_AMT"]; //' The final amount charged, including any shipping and taxes from your Merchant Profile.
- $currencyCode = $resArray["PAYMENTINFO_0_CURRENCYCODE"]; //' A three-character currency code for one of the currencies listed in PayPay-Supported Transactional Currencies. Default: USD.
- $feeAmt = $resArray["PAYMENTINFO_0_FEEAMT"]; //' PayPal fee amount charged for the transaction
- // $settleAmt = $resArray["PAYMENTINFO_0_SETTLEAMT"]; //' Amount deposited in your PayPal account after a currency conversion.
- $taxAmt = $resArray["PAYMENTINFO_0_TAXAMT"]; //' Tax charged on the transaction.
- // $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.
- /*
- ' Status of the payment:
- 'Completed: The payment has been completed, and the funds have been added successfully to your account balance.
- 'Pending: The payment is pending. See the PendingReason element for more information.
- */
- $paymentStatus = $resArray["PAYMENTINFO_0_PAYMENTSTATUS"];
- /*
- 'The reason the payment is pending:
- ' none: No pending reason
- ' 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.
- ' echeck: The payment is pending because it was made by an eCheck that has not yet cleared.
- ' 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.
- ' 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.
- ' verify: The payment is pending because you are not yet verified. You must verify your account before you can accept this payment.
- ' other: The payment is pending for a reason other than those listed above. For more information, contact PayPal customer service.
- */
- $pendingReason = $resArray["PAYMENTINFO_0_PENDINGREASON"];
- /*
- 'The reason for a reversal if TransactionType is reversal:
- ' none: No reason code
- ' chargeback: A reversal has occurred on this transaction due to a chargeback by your customer.
- ' guarantee: A reversal has occurred on this transaction due to your customer triggering a money-back guarantee.
- ' buyer-complaint: A reversal has occurred on this transaction due to a complaint about the transaction from your customer.
- ' refund: A reversal has occurred on this transaction because you have given the customer a refund.
- ' other: A reversal has occurred on this transaction due to a reason not listed above.
- */
- $reasonCode = $resArray["PAYMENTINFO_0_REASONCODE"];
- redirect(site_url('dashboard/billing/success'));
- } else {
- //Display a user friendly Error on the page using any of the following error information returned by PayPal
- $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
- $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
- $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
- $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
- echo "GetExpressCheckoutDetails API call failed. ";
- echo "Detailed Error Message: " . $ErrorLongMsg;
- echo "Short Error Message: " . $ErrorShortMsg;
- echo "Error Code: " . $ErrorCode;
- echo "Error Severity Code: " . $ErrorSeverityCode;
- }
- }
- }
- public function success() {
- print_r($this->session->all_userdata()); die;
- Template::set('toolbar_title', lang('billing_manage'));
- Template::render();
- }
- public function paypal_checkout() {
- // ==================================
- // PayPal Express Checkout Module
- // ==================================
- //'------------------------------------
- //' The paymentAmount is the total value of
- //' the shopping cart, that was set
- //' earlier in a session variable
- //' by the shopping cart page
- //'------------------------------------
- $paymentAmount = $this->session->userdata('Payment_Amount');
- //'------------------------------------
- //' The currencyCodeType and paymentType
- //' are set to the selections made on the Integration Assistant
- //'------------------------------------
- $currencyCodeType = "USD";
- $paymentType = "Sale";
- //'------------------------------------
- //' The returnURL is the location where buyers return to when a
- //' payment has been succesfully authorized.
- //'
- //' This is set to the value entered on the Integration Assistant
- //'------------------------------------
- $returnURL = "http://marketads.net/app/dashboard/billing/confirm";
- //'------------------------------------
- //' The cancelURL is the location buyers are sent to when they hit the
- //' cancel button during authorization of payment during the PayPal flow
- //'
- //' This is set to the value entered on the Integration Assistant
- //'------------------------------------
- $cancelURL = "http://marketads.net/app/dashboard/billing/cancel";
- //'------------------------------------
- //' Calls the SetExpressCheckout API call
- //'
- //' The CallShortcutExpressCheckout public function is defined in the file PayPalFunctions.php,
- //' it is included at the top of this file.
- //'-------------------------------------------------
- $resArray = $this->CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- $this->RedirectToPayPal($resArray["TOKEN"]);
- } else {
- //Display a user friendly Error on the page using any of the following error information returned by PayPal
- $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
- $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
- $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
- $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
- echo "SetExpressCheckout API call failed. ";
- echo "Detailed Error Message: " . $ErrorLongMsg;
- echo "Short Error Message: " . $ErrorShortMsg;
- echo "Error Code: " . $ErrorCode;
- echo "Error Severity Code: " . $ErrorSeverityCode;
- }
- }
- private function CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL) {
- //------------------------------------------------------------------------------------------------------------------------------------
- // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
- $nvpstr = "&PAYMENTREQUEST_0_AMT=" . $paymentAmount;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
- $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
- $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
- $this->session->set_userdata("currencyCodeType", $currencyCodeType);
- $this->session->set_userdata("PaymentType", $paymentType);
- //'---------------------------------------------------------------------------------------------------------------
- //' Make the API call to PayPal
- //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
- //' If an error occured, show the resulting errors
- //'---------------------------------------------------------------------------------------------------------------
- $resArray = $this->hash_call("SetExpressCheckout", $nvpstr);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- $token = urldecode($resArray["TOKEN"]);
- $this->session->set_userdata('TOKEN', $token);
- }
- return $resArray;
- }
- /*
- '-------------------------------------------------------------------------------------------------------------------------------------------
- ' Purpose: Prepares the parameters for the SetExpressCheckout API Call.
- ' Inputs:
- ' paymentAmount: Total value of the shopping cart
- ' currencyCodeType: Currency code value the PayPal API
- ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
- ' returnURL: the page where buyers return to after they are done with the payment review on PayPal
- ' cancelURL: the page where buyers return to when they cancel the payment review on PayPal
- ' shipToName: the Ship to name entered on the merchant's site
- ' shipToStreet: the Ship to Street entered on the merchant's site
- ' shipToCity: the Ship to City entered on the merchant's site
- ' shipToState: the Ship to State entered on the merchant's site
- ' shipToCountryCode: the Code for Ship to Country entered on the merchant's site
- ' shipToZip: the Ship to ZipCode entered on the merchant's site
- ' shipToStreet2: the Ship to Street2 entered on the merchant's site
- ' phoneNum: the phoneNum entered on the merchant's site
- '--------------------------------------------------------------------------------------------------------------------------------------------
- */
- private function CallMarkExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum
- ) {
- //------------------------------------------------------------------------------------------------------------------------------------
- // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
- $nvpstr = "&PAYMENTREQUEST_0_AMT=" . $paymentAmount;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
- $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
- $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
- $nvpstr = $nvpstr . "&ADDROVERRIDE=1";
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTONAME=" . $shipToName;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTREET=" . $shipToStreet;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTREET2=" . $shipToStreet2;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOCITY=" . $shipToCity;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOSTATE=" . $shipToState;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE=" . $shipToCountryCode;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOZIP=" . $shipToZip;
- $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_SHIPTOPHONENUM=" . $phoneNum;
- $this->session->set_userdata("currencyCodeType", $currencyCodeType);
- $this->session->set_userdata("PaymentType", $paymentType);
- //'---------------------------------------------------------------------------------------------------------------
- //' Make the API call to PayPal
- //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
- //' If an error occured, show the resulting errors
- //'---------------------------------------------------------------------------------------------------------------
- $resArray = $this->hash_call("SetExpressCheckout", $nvpstr);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- $token = urldecode($resArray["TOKEN"]);
- $this->session->set_userdata('TOKEN', $token);
- }
- return $resArray;
- }
- /*
- '-------------------------------------------------------------------------------------------
- ' Purpose: Prepares the parameters for the GetExpressCheckoutDetails API Call.
- '
- ' Inputs:
- ' None
- ' Returns:
- ' The NVP Collection object of the GetExpressCheckoutDetails Call Response.
- '-------------------------------------------------------------------------------------------
- */
- private function GetShippingDetails($token) {
- //'--------------------------------------------------------------
- //' At this point, the buyer has completed authorizing the payment
- //' at PayPal. The public function will call PayPal to obtain the details
- //' of the authorization, incuding any shipping information of the
- //' buyer. Remember, the authorization is not a completed transaction
- //' at this state - the buyer still needs an additional step to finalize
- //' the transaction
- //'--------------------------------------------------------------
- //'---------------------------------------------------------------------------
- //' Build a second API request to PayPal, using the token as the
- //' ID to get the details on the payment authorization
- //'---------------------------------------------------------------------------
- $nvpstr = "&TOKEN=" . $token;
- //'---------------------------------------------------------------------------
- //' Make the API call and store the results in an array.
- //' If the call was a success, show the authorization details, and provide
- //' an action to complete the payment.
- //' If failed, show the error
- //'---------------------------------------------------------------------------
- $resArray = $this->hash_call("GetExpressCheckoutDetails", $nvpstr);
- $ack = strtoupper($resArray["ACK"]);
- if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
- $this->session->set_userdata('payer_id', $resArray['PAYERID']);
- }
- return $resArray;
- }
- /*
- '-------------------------------------------------------------------------------------------------------------------------------------------
- ' Purpose: Prepares the parameters for the GetExpressCheckoutDetails API Call.
- '
- ' Inputs:
- ' sBNCode: The BN code used by PayPal to track the transactions from a given shopping cart.
- ' Returns:
- ' The NVP Collection object of the GetExpressCheckoutDetails Call Response.
- '--------------------------------------------------------------------------------------------------------------------------------------------
- */
- private function ConfirmPayment($FinalPaymentAmt) {
- /* Gather the information to make the final call to
- finalize the PayPal payment. The variable nvpstr
- holds the name value pairs
- */
- //Format the other parameters that were stored in the session from the previous calls
- $token = urlencode($this->session->userdata('TOKEN'));
- $paymentType = urlencode($this->session->userdata('PaymentType'));
- $currencyCodeType = urlencode($this->session->userdata('currencyCodeType'));
- $payerID = urlencode($this->session->userdata('payer_id'));
- $serverName = urlencode($_SERVER['SERVER_NAME']);
- $nvpstr = '&TOKEN=' . $token . '&PAYERID=' . $payerID . '&PAYMENTREQUEST_0_PAYMENTACTION=' . $paymentType . '&PAYMENTREQUEST_0_AMT=' . $FinalPaymentAmt;
- $nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE=' . $currencyCodeType . '&IPADDRESS=' . $serverName;
- /* Make the call to PayPal to finalize payment
- If an error occured, show the resulting errors
- */
- $resArray = $this->hash_call("DoExpressCheckoutPayment", $nvpstr);
- /* Display the API response back to the browser.
- If the response from PayPal was a success, display the response parameters'
- If the response was an error, display the errors received using APIError.php.
- */
- $ack = strtoupper($resArray["ACK"]);
- return $resArray;
- }
- /*
- '-------------------------------------------------------------------------------------------------------------------------------------------
- ' Purpose: This public function makes a DoDirectPayment API call
- '
- ' Inputs:
- ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
- ' paymentAmount: total value of the shopping cart
- ' currencyCode: currency code value the PayPal API
- ' firstName: first name as it appears on credit card
- ' lastName: last name as it appears on credit card
- ' street: buyer's street address line as it appears on credit card
- ' city: buyer's city
- ' state: buyer's state
- ' countryCode: buyer's country code
- ' zip: buyer's zip
- ' creditCardType: buyer's credit card type (i.e. Visa, MasterCard ... )
- ' creditCardNumber: buyers credit card number without any spaces, dashes or any other characters
- ' expDate: credit card expiration date
- ' cvv2: Card Verification Value
- '
- '-------------------------------------------------------------------------------------------
- '
- ' Returns:
- ' The NVP Collection object of the DoDirectPayment Call Response.
- '--------------------------------------------------------------------------------------------------------------------------------------------
- */
- private function DirectPayment($paymentType, $paymentAmount, $creditCardType, $creditCardNumber, $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip, $countryCode, $currencyCode) {
- //Construct the parameter string that describes DoDirectPayment
- $nvpstr = "&AMT=" . $paymentAmount;
- $nvpstr = $nvpstr . "&CURRENCYCODE=" . $currencyCode;
- $nvpstr = $nvpstr . "&PAYMENTACTION=" . $paymentType;
- $nvpstr = $nvpstr . "&CREDITCARDTYPE=" . $creditCardType;
- $nvpstr = $nvpstr . "&ACCT=" . $creditCardNumber;
- $nvpstr = $nvpstr . "&EXPDATE=" . $expDate;
- $nvpstr = $nvpstr . "&CVV2=" . $cvv2;
- $nvpstr = $nvpstr . "&FIRSTNAME=" . $firstName;
- $nvpstr = $nvpstr . "&LASTNAME=" . $lastName;
- $nvpstr = $nvpstr . "&STREET=" . $street;
- $nvpstr = $nvpstr . "&CITY=" . $city;
- $nvpstr = $nvpstr . "&STATE=" . $state;
- $nvpstr = $nvpstr . "&COUNTRYCODE=" . $countryCode;
- $nvpstr = $nvpstr . "&IPADDRESS=" . $_SERVER['REMOTE_ADDR'];
- $resArray = $this->hash_call("DoDirectPayment", $nvpstr);
- 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 associtive array containing the response from the server.
- '-------------------------------------------------------------------------------------------------------------------------------------------
- */
- private function hash_call($methodName, $nvpStr) {
- //declaring of global variables
- // global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;
- // global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
- // global $gv_ApiErrorURL;
- // global $sBNCode;
- //setting the curl parameters.
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $this->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_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- //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 ($this->USE_PROXY)
- curl_setopt($ch, CURLOPT_PROXY, $this->PROXY_HOST . ":" . $this->PROXY_PORT);
- //NVPRequest for submitting to server
- $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);
- //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 = $this->deformatNVP($response);
- $nvpReqArray = $this->deformatNVP($nvpreq);
- $this->session->set_userdata('nvpReqArray', $nvpReqArray);
- if (curl_errno($ch)) {
- // moving to display page to display curl errors
- $this->session->set_userdata('curl_error_no', curl_errno($ch));
- $this->session->set_userdata('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: NVP string.
- Returns:
- ----------------------------------------------------------------------------------
- */
- private function RedirectToPayPal($token) {
- // global $PAYPAL_URL;
- // Redirect to paypal.com here
- $payPalURL = $this->PAYPAL_URL . $token;
- header("Location: " . $payPalURL);
- exit;
- }
- /* '----------------------------------------------------------------------------------
- * This public 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.
- ----------------------------------------------------------------------------------
- */
- private 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement