Advertisement
Guest User

so

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