PHP Facebook登录


PayPal是一个支付处理系统,我们可以通过使用PHP将PayPal与网站集成。

PayPal集成文件系统

PayPal集成文件系统包含4个文件,如下所示。

  • constants.php - 此文件包含API用户名,密码和签名。

  • CallerService.php - 该文件包含PayPal服务,用于调用PayPal服务。

  • confirmation.php - 此文件包含一个表单,其中包含用于进行付款流程所需的最少字段,它将返回付款成功或失败。

  • PayPal_entry.php - 此页面用于将用户的数据发送给PayPal。 它充当PayPal和用户表单之间的适配器。

constants.php

<?php
/****************************************************
constants.php

This is the configuration file for the samples.This file defines the parameters needed to make an API call.

PayPal includes the following API Signature for making API calls to the PayPal sandbox:

Called by CallerService.php.
****************************************************/
/**
# API user: The user that is identified as making the call. you can
# also use your own API username that you created on PayPal抯 sandbox
# or the PayPal live site
*/
define('API_USERNAME', 'YOUR USER NAME HERE');
/**
# API_password: The password associated with the API user
# If you are using your own API username, enter the API password that
# was generated by PayPal below
# IMPORTANT - HAVING YOUR API PASSWORD INCLUDED IN THE MANNER IS NOT
# SECURE, AND ITS ONLY BEING SHOWN THIS WAY FOR TESTING PURPOSES
*/

define('API_PASSWORD', 'YOUR PASSWORD HERE');

/**
# API_Signature:The Signature associated with the API user. which is generated by paypal.
*/

define('API_SIGNATURE', 'YOUR API SIGNATURE HERE');

/**
# Endpoint: this is the server URL which you have to connect for submitting your API request.
*/

/**
    For sandbox URL is: https://api-3t.sandbox.paypal.com/nvp;
*/

define('API_ENDPOINT', 'https://api-3t.paypal.com/nvp');

/**
USE_PROXY: Set this variable to TRUE to route all the API requests through proxy.
like define('USE_PROXY',TRUE);
*/
define('USE_PROXY',FALSE);
/**
PROXY_HOST: Set the host name or the IP address of proxy server.
PROXY_PORT: Set proxy port.

PROXY_HOST and PROXY_PORT will be read only if USE_PROXY is set to TRUE
*/
define('PROXY_HOST', '127.0.0.1');
define('PROXY_PORT', '808');

/* Define the PayPal URL. This is the URL that the buyer is first sent to to authorize payment with their paypal account change the URL depending if you are testing on the sandbox
   or going to 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=
*/

define('PAYPAL_URL', 'https://www.paypal.com/webscr&cmd=_express-checkout&token=');

/**
# Version: this is the API version in the request.
# It is a mandatory parameter for each API request.
# The only supported value at this time is 2.3
*/

define('VERSION', '53.0');

?>

CallerService.php

<?php
/****************************************************
CallerService.php

This file uses the constants.php to get parameters needed to make an API call and calls the server.if you want use your own credentials, you have to change the constants.php

Called by TransactionDetails.php, ReviewOrder.php, DoDirectPaymentReceipt.php and DoExpressCheckoutPayment.php.

****************************************************/
set_time_limit(0);
require_once 'constants.php';

$API_UserName=API_USERNAME;


$API_Password=API_PASSWORD;


$API_Signature=API_SIGNATURE;


$API_Endpoint =API_ENDPOINT;


$version=VERSION;

//session_start();

/**
  * 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.
*/


function hash_call($methodName,$nvpStr)
{
    //declaring of global variables
    global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header;

    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$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(USE_PROXY)
    curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT);

    //NVPRequest for submitting to server
    $nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($version)."&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature).$nvpStr;

    //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=deformatNVP($response);
    $nvpReqArray=deformatNVP($nvpreq);
    $_SESSION['nvpReqArray']=$nvpReqArray;

    if (curl_errno($ch)) {
        // moving to display page to display curl errors
          $_SESSION['curl_error_no']=curl_errno($ch) ;
          $_SESSION['curl_error_msg']=curl_error($ch);
          $location = "APIError.php";
          header("Location: $location");
     } else {
         //closing the curl service
            curl_close($ch);
      }

return $nvpResArray;
}

/** This 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.
  */

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;
}
?>

confirmation.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Pay Throught Paypal</title>
</head>
<body>
<div align="center">
<table border="0" width="900" cellspacing="0" cellpadding="0">
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><form name="form1" method="POST" action="paypal_entry.php">
        <input type="hidden" name="paymentType" value="Sale" />
                <table width="880" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="left" valign="top" class="content4">
                    <table border="0" width="100%" cellspacing="0" cellpadding="0">
                        <tr>
                            <td width="50%" valign="top">
                            <div align="left">
                                <table border="0" width="98%" cellspacing="0" cellpadding="4" class="content4" style="border:1px solid #333333; font-family:Verdana; font-size:8pt; color:#000000">
                                    <tr>
                                        <td width="100%" colspan="2" background="images/collectionbg1_see.png">
                                        <b><font size="2">Personal Information</font></b></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" background="images/collectionbg1_see.png" align="right">
                                        <b>First Name:</b></td>
                                        <td width="50%" background="images/collectionbg1_see.png"><input type="text" name="fname" value="" /></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" background="images/collectionbg1_see.png" align="right">
                                        <b>Last Name:</b></td>
                                        <td width="50%" background="images/collectionbg1_see.png"><input type="text" name="lname" value="" /></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" background="images/collectionbg1_see.png" align="right">
                                        <b>Amount: </b> </td>
                                        <td width="50%" background="images/collectionbg1_see.png"><input type="text" name="ftotal" value=""></td>
                                    </tr>
                                    <tr>
                                        <td width="100%" colspan="2" background="images/collectionbg1_see.png"><b>
                                        <font size="2">Credit Card Information</font></b></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" align="right"><b>Card Type :</b></td>
                                        <td width="65%" align="left">
                                        <select name="creditCardType" style="width:150px;">
                                        <option value="Visa" selected="selected">Visa</option>
                                        <option value="MasterCard">MasterCard</option>
                                        <option value="Discover">Discover</option>
                                        <option value="Amex">American Express</option>
                                        </select></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" align="right"><b>Card Number :</b></td>
                                        <td width="65%" align="left">
                                        <input type="text" size="19" maxlength="19" name="creditCardNumber" style="width:150px;" /></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" align="right"><b>Expiration Date :</b></td>
                                        <td width="65%" align="left">
                                      <select name="expDateMonth">
                                        <option value="1">01</option>
                                        <option value="2">02</option>
                                        <option value="3">03</option>
                                        <option value="4">04</option>
                                        <option value="5">05</option>
                                        <option value="6">06</option>
                                        <option value="7">07</option>
                                        <option value="8">08</option>
                                        <option value="9">09</option>
                                        <option value="10">10</option>
                                        <option value="11">11</option>
                                        <option value="12">12</option>
                                        </select> <select name="expDateYear" size="1">
                                            <option value="2013" selected>2013</option>
                                            <option value="2014">2014</option>
                                            <option value="2015">2015</option>
                                            <option value="2016">2016</option>
                                            <option value="2017">2017</option>
                                            <option value="2019">2019</option>
                                            <option value="2020">2020</option>
                                            <option value="2021">2021</option>
                                            <option value="2022">2022</option>
                                            <option value="2023">2023</option>
                                            <option value="2024">2024</option>
                                            <option value="2025">2025</option>
                                            <option value="2026">2026</option>
                                            <option value="2027">2027</option>
                                            <option value="2028">2028</option>
                                            <option value="2029">2029</option>
                                            <option value="2030">2030</option>
                                        </select></td>
                                    </tr>
                                    <tr>
                                        <td width="35%" align="right"><b>CVV Number :</b></td>
                                        <td width="65%" align="left">
                                        <input type="text" size="3" maxlength="4" name="cvv2Number" /><input type="hidden" name="paymentType" value="Sale" /></td>
                                    </tr>
                                    </table>
                            </div>
                            </td>
                            <td width="50%" valign="top">
                            <?php if($_GET['flag']==2){ ?>
                            <div align="right">
                                <table border="0" width="98%" cellspacing="0" cellpadding="4" class="content4" style="border:1px solid #333333;">
                                    <tr>
                                        <td width="100%" background="images/collectionbg1_see.png" align="center">
                                        <b><font size="2" color="#FF0000">Transaction Declined</font></b></td>
                                    </tr>
                                    <tr>
                                        <td width="100%" align="left" height="10"></td>
                                    </tr>
                                    <tr>
                                        <td width="100%" align="left">
                                        <?php
                                            $err=$_SESSION['reshash'];
                                            if($err[L_LONGMESSAGE0] != ""){
                                                print  $err[L_LONGMESSAGE0]."<br><br>";
                                            }
                                            if($err[L_LONGMESSAGE1] != ""){
                                                print  $err[L_LONGMESSAGE1]."<br><br>";
                                            }
                                            if($err[L_LONGMESSAGE2] != ""){
                                                print  $err[L_LONGMESSAGE2]."<br><br>";
                                            }
                                        ?></td>
                                    </tr>
                                    </table>
                            </div>
                            <?php } ?></td>
                        </tr>
                        <tr>
                            <td width="50%" valign="top" align="right" style="padding-right: 10px">
                    <input type="submit" value="Submit" name="B1"></td>
                            <td width="50%" valign="top">
                            &nbsp;</td>
                        </tr>
                    </table>
                    </td>
                  </tr>      

                  </table>
                </form></td>
    </tr>
</table>
</div>
</body>
</html>

PayPal_entry.php

<?php

require_once 'CallerService.php';

/*** Get required parameters from the web form for the request ***/

$paymentType =urlencode( $_POST['paymentType']);
$firstName =urlencode( $_POST['fname']);
$lastName =urlencode( $_POST['lname']);
$creditCardType =urlencode( $_POST['creditCardType']);
$creditCardNumber = urlencode($_POST['creditCardNumber']);
$expDateMonth =urlencode( $_POST['expDateMonth']);

//Month must be padded with leading zero
$padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);

$expDateYear =urlencode( $_POST['expDateYear']);
$cvv2Number = urlencode($_POST['cvv2Number']);
$amount = urlencode($_POST['ftotal']);
$currencyCode="USD";

/* Construct the request string that will be sent to PayPal.
   The variable $nvpstr contains all the variables and is a
   name value pair string with & as a delimiter */

$nvpstr="&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber&EXPDATE=".$padDateMonth.$expDateYear."&CVV2=$cvv2Number&FIRSTNAME=$firstName&COUNTRYCODE=US&CURRENCYCODE=$currencyCode";

/* Make the API call to PayPal, using API signature.
   The API response is stored in an associative array called $resArray */
$resArray=hash_call("doDirectPayment",$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"]);

if($ack!="SUCCESS")  {
    $_SESSION['reshash']=$resArray;
    $location = "confirmation.php?flag=2";
         header("Location: $location");
   }
if($ack=="SUCCESS")  {
    $_SESSION['reshash']="SUCCESS";
    $location = "thankyou.php?stage=3";
         header("Location: $location");

}   
?>

用户将在上面的语法中声明用户名,密码和签名,这些语法位于constants.php中。这是一个实验性的例子,所以最后的金额将被添加到沙箱的帐户。