为paypal

时间:2017-04-13 08:04:51

标签: php paypal

我正在尝试使用参考交易实施paypal,创建结算协议,然后引用此结算协议ID以用于将来的付款请求。

任何人都可以向我推荐一些示例代码。

1 个答案:

答案 0 :(得分:0)

检查以下代码:它只是示例代码,有关更多帮助,请查看Paypal documentation

---复制下面的代码并将其保存为localhost&中的“ refTest.php ”。检查一下。

$user = 'API_USERNAME';
$password = 'API_PASSWORD';
$signature = 'API_SIGNATURE';

$nvps = "&USER=$user".
        "&PWD=$password".
        "&SIGNATURE=$signature";
$amt=2;     
$tax=0.5;       
$tot=2.5;       
if(!isset($_REQUEST['token'])){
     $nvpset= $nvps
       . "&METHOD=SetExpressCheckout" 
       . "&VERSION=109.0" 
       . "&RETURNURL=http://localhost/refTest.php"
       . "&CANCELURL=http://localhost/refTest.php"
       . "&PAYMENTREQUEST_0_CURRENCYCODE=USD"
       . "&LOCALCODE=US"
       . "&PAYMENTREQUEST_0_AMT=".$tot
       . "&PAYMENTREQUEST_0_ITEMAMT=".$amt
       . "&PAYMENTREQUEST_0_TAXAMT=".$tax
       . "&PAYMENTREQUEST_0_DESC=Shopping"
       . "&PAYMENTREQUEST_0_PAYMENTACTION=Order"
       . "&L_PAYMENTREQUEST_0_NAME0=James Bond"
       . "&L_PAYMENTREQUEST_0_NUMBER0=101"
       . "&L_PAYMENTREQUEST_0_QTY0=1"
       . "&L_PAYMENTREQUEST_0_AMT0=".$amt
       . "&SOLUTIONTYPE=Sole"
       . "&L_BILLINGTYPE0=MerchantInitiatedBillingSingleAgreement"
       . "&L_BILLINGAGREEMENTDESCRIPTION0=FirstBillingAgreement"
       . "&LANDINGPAGE=Billing"
       . "&NOSHIPPING=1";

    $setEC = RequestPayPal($nvpset);   
    header('location: https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token='.$setEC['TOKEN']);     
}
elseif(isset($_REQUEST['token']) && isset($_REQUEST['PayerID']) && !isset($_REQUEST['action'])){
    echo "<a href='?token=".$_REQUEST['token']."&PayerID=".$_REQUEST['PayerID']."&action=createBillingAgree'>Let's Create Billing Agreement</a></br>";

    echo "Token Details</br>";

    $nvpget= $nvps
       . "&METHOD=GetExpressCheckoutDetails" 
       . "&VERSION=109.0" 
       . "&TOKEN=".$_REQUEST['token'];

    $getEC = RequestPayPal($nvpget);

    print_r($getEC);
}
elseif(isset($_REQUEST['token']) && isset($_REQUEST['PayerID']) && isset($_REQUEST['action']) && $_REQUEST['action']=='createBillingAgree'){
    echo "<a href='?token=".$_REQUEST['token']."&PayerID=".$_REQUEST['PayerID']."&action=PayIt'>Let's Do the payment Now</a></br>";

    $nvpget= $nvps
       . "&METHOD=CreateBillingAgreement" 
       . "&VERSION=109.0" 
       . "&TOKEN=".$_REQUEST['token'];

    $getEC = RequestPayPal($nvpget);

    print_r($getEC);
}
elseif(isset($_REQUEST['token']) && isset($_REQUEST['PayerID']) && isset($_REQUEST['action']) && $_REQUEST['action']=='PayIt'){
    echo "DO EXPRESS CHECK OUT DONE</br>";
    $nvpdo= $nvps
       . "&METHOD=DoExpressCheckoutPayment" 
       . "&VERSION=109.0" 
       . "&PAYMENTACTION=Order" 
       . "&AMT=".$tot 
       . "&TOKEN=".$_REQUEST['token']
       . "&PAYERID=".$_REQUEST['PayerID'];
    $doEC = RequestPayPal($nvpdo);

    echo "<a href='?token=".$_REQUEST['token']."&PayerID=".$_REQUEST['PayerID']."&action=captureFuturePay&refId=".$doEC['BILLINGAGREEMENTID']."'>Create Reference Transasction</a></br>";
    print_r($doEC);
}
else{
    echo "Reference Transasction Done </br>";

    $nvpdo= $nvps
       . "&METHOD=DoReferenceTransaction" 
       . "&VERSION=109.0" 
       . "&PAYMENTACTION=Order" 
       . "&AMT=".$tot 
       . "&TOKEN=".$_REQUEST['token']
       . "&REFERENCEID=".$_REQUEST['refId']
       . "&PAYERID=".$_REQUEST['PayerID'];
    $doEC = RequestPayPal($nvpdo);

    print_r($doEC);
}

function RequestPayPal($nvp){
    $url = 'https://api-3t.sandbox.paypal.com/nvp';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$nvp);

    $result = curl_exec($ch);
    $httpResponseAr = explode("&", $result);
    $httpParsedResponseAr = array();
    foreach ($httpResponseAr as $value) {
        $tmpAr = explode("=", $value);
        if(sizeof($tmpAr) > 1) {
            $httpParsedResponseAr[$tmpAr[0]] = urldecode($tmpAr[1]);
        }
    }

    curl_close ($ch); 
    return $httpParsedResponseAr;
}


?>