2015-10-13 49 views
2

我想爲我的客戶將定期付款與付款流程(託管在貝寶上的頁面)相集成。但我沒有在php中找到相同的教程或示例代碼。支付流與託管結帳頁面重複發生

除此之外,我也無法獲得以下查詢的答案。

  • 通過貝寶付款後我將如何獲得付款成功或失敗的結果,因爲有成功和取消付款流程中的URL和IPN。請建議。
  • 在每次重複如何獲得付款成功或失敗的結果?

回答

0

爲避免雙重測試,您可以使用價格進行測試,如0,01。要創建一個定期支付使用這個HTML表單:

<form method="post" name="formName" id="submitThisForm" action="https://www.paypal.com/cgi-bin/webscr" > 
<input type="hidden" name="cmd" value="_xclick-subscriptions"> 
<input type="hidden" name="business" value="[email protected]" /> 
<input type="hidden" name="item_name" value="Your Membership" /> 
<input type="hidden" name="a3" value="0.01"> 
<input type="hidden" name="p3" value="1"> 
<input type="hidden" name="t3" value="M"> 
<input type="hidden" name="src" value="1"> 
<input type="hidden" name="sra" value="1"> 
<input type="hidden" name="item_number" value="2" /> 
<input type="hidden" name="custom" value="SECURITYCODE" /> 
<input type="hidden" name="currency_code" value="USD" /> 
<input type="hidden" name="quantity" value="1" /> 
<input type="hidden" name="no_shipping" value="1" /> 
<input type="hidden" name="return" value="page going after payment" /> 
<input type="hidden" name="cancel_return" value="" /> 
<input type="hidden" name="cbt" value="ITEM DESCRIPTION" /> 
<input type="hidden" name="rm" value="2" /> 
<input type="hidden" name="notify_url" value="your_listener_file.php" /> 

當用戶取消會員資格,支付寶會通知上的「notify_url」 - 在你的情況,這將是文件your_listener_file.php。在這個文件裏面,你必須檢查paypal POST變量'txn_type'='subscr_cancel'。有一個幾個要點:

  1. 您必須驗證IPN交易:

    $post   = array('cmd' => '_notify-validate'); 
    foreach($_POST as $key=>$value){ 
         $post[$key] = $value; 
    } 
    $c = curl_init(); 
    curl_setopt_array($c, array(
        CURLOPT_FOLLOWLOCATION => TRUE, 
        CURLOPT_RETURNTRANSFER => TRUE, 
        CURLOPT_CONNECTTIMEOUT => 15, 
        CURLOPT_MAXREDIRS  => 15, 
        CURLOPT_TIMEOUT   => 15, 
        CURLOPT_URL    => 'https://www.paypal.com/cgi-bin/webscr', 
        CURLOPT_POST   => TRUE, 
        CURLOPT_POSTFIELDS  => $post, 
    )); 
    $res = curl_exec($c); 
    curl_close($c); 
    $res = trim($res); 
    
    if($res != 'VERIFIED') { 
        exit(); 
    } 
    
  2. 二 - 檢查是在交易數據庫中使用的唯一密鑰存在。您必須使用Paypal POST變量'custom'。

  3. 如果交易存在,只是做一些簡單的檢查:

    if(!empty($_POST['txn_type']) && $_POST['txn_type'] == 'subscr_cancel') 
        $paypalData['approved'] = 0; 
    

另一種方法是使用貝寶快速結賬。我推薦這種方法。這裏一個簡單的(PHP)例如:

 
// Parameters for SetExpressCheckout, which will be sent to PayPal
$padata['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Product description'; $padata['L_BILLINGAGREEMENTDESCRIPTION0'] = $padata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' $'.$product->price.'/month'; $padata['L_PAYMENTREQUEST_0_DESC0'] = $padata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' $'.$product->price.'/month';
$padata['PAYMENTREQUEST_0_NOTIFYURL'] = 'http://site_url/paypal/ipn'; $padata['PAYMENTREQUEST_0_DESC'] = $product->name; $padata['RETURNURL'] = 'http://site_url/paypal/returnurl'; $padata['CANCELURL'] = 'http://site_url/paypal/cancelurl';
$padata['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD'; $padata['PAYMENTREQUEST_0_PAYMENTACTION'] = 'SALE'; $padata['PAYMENTREQUEST_0_ITEMAMT'] = $product->price;
$padata['PAYMENTREQUEST_0_AMT'] = $product->price;
$padata['L_BILLINGTYPE0'] = 'RecurringPayments';
$padata['L_PAYMENTREQUEST_0_NAME0'] = $product->name;
$padata['L_PAYMENTREQUEST_0_NUMBER0']= '322';
$padata['L_PAYMENTREQUEST_0_QTY0'] = '1';
$padata['L_PAYMENTREQUEST_0_AMT0'] = $product->price;
$paypal_data = http_build_query($padata); $httpParsedResponseAr = $this->PPHttpPost('SetExpressCheckout', $paypal_data); //Respond according to message we receive from Paypal if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])){ //Redirect user to PayPal store with Token received. $paypalurl ='https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$httpParsedResponseAr["TOKEN"].''; header('Location: '.$paypalurl); }else{ echo 'Error :'.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'';
}

頁RETURNURL:

 
$hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Recurring Description';
$hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'] = $hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' $'.$pr->price.'/month';
$hosteddata['L_PAYMENTREQUEST_0_NAME0'] = $pr->name;
$hosteddata['PROFILEREFERENCE'] = $GetExpressCheckoutDetails['L_PAYMENTREQUEST_0_NUMBER0'];
$hosteddata['PROFILESTARTDATE'] = date('Y-m-d') . 'T' . date('H:i:s').'Z';
$hosteddata['SUBSCRIBERNAME'] = $GetExpressCheckoutDetails['FIRSTNAME'] . ' ' . $GetExpressCheckoutDetails['LASTNAME'];
$hosteddata['TOKEN'] = urlencode($_POST['token']);
$hosteddata['DESC'] = $hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'];
$hosteddata['AMT'] = $pr->price;
$hosteddata['BILLINGPERIOD'] = 'Month';
$hosteddata['BILLINGFREQUENCY'] = '1';
$hosteddata['TOTALBILLINGCYCLES'] = '12';
$hosteddata['REGULARTOTALBILLINGCYCLES'] = '1';
$hosteddata['VERSION'] = '74.0';
$hosteddata['MAXFAILEDPAYMENTS'] = '1';
$hosteddata['L_PAYMENTREQUEST_0_QTY0'] = '1';
$hosteddata['L_BILLINGTYPE0'] = 'RecurringPayments';
$hosteddata['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Digital';
$hosteddata['L_PAYMENTREQUEST_0_AMT0'] = $pr->price;
$hosteddata['INITAMT'] = $pr->price;
$hosteddata['L_PAYMENTREQUEST_0_NUMBER0'] = $pr->id;
$hosteddata['PAYMENTREQUEST_0_NOTIFYURL'] = 'http://site_url/paypal/ipn';
$paypal_data = http_build_query($hosteddata); $hosted_saas_response = $this->PPHttpPost('CreateRecurringPaymentsProfile', $paypal_data);

我用一個單獨的方法來發布參數到PayPal

 
private function PPHttpPost($methodName_, $nvpStr_) {
$api_username = '[email protected]'; $api_password = 'QWEQWEWQEQWEQEQWE';
$api_signature = 'WQEQWEQWEQWEWQEQWEQWEQWEQWEQWE.cT';
$api_endpoint = "https://api-3t.paypal.com/nvp";
$version = '124.0'; $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
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);
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$api_password&USER=$api_username&SIGNATURE=$api_signature&$nvpStr_";
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
$httpResponse = curl_exec($ch); if(!$httpResponse) { exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
} // Extract the response details. $httpResponseAr = explode("&", $httpResponse);
$httpParsedResponseAr = array(); foreach ($httpResponseAr as $i => $value) { $tmpAr = explode("=", $value); if(sizeof($tmpAr) > 1) { $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1]; } } if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
exit("Invalid HTTP Response for POST request($nvpreq) to $api_endpoint.");
} return $httpParsedResponseAr;
}
+0

這不會對payflow工作.. –

+0

其他方式是使用PayPal快速結帳,我會加快速結帳的例子 – Pavel

+0

需要看看它。 – VBMali