2013-04-30 65 views
0

我是新來的developer.paypal.com,我在www.sandbox.paypal.com創建了一個訂閱按鈕,是否可以將支付的金額客戶向我的網站訂閱或者在www.sandbox.paypal.com上發佈的詳細信息?如果是這樣,您能向我展示一些關於如何做到這一點的例子。是否有可能將金額返還給我的網站

我嘗試訂閱按鈕後,這是返回的價值,我找不到一些價值的鏈接,你提供或訂閱變量我怎麼能顯示它們?

我無法獲得我的訂閱的開始日期和結束日期 謝謝。

+0

什麼語言是您使用? – 2013-04-30 08:39:54

+0

@FeistyMango,我正在使用PHP – 2013-04-30 09:02:49

回答

1

您可以通過以下兩種方法之一來完成這一操作。付款完成後,您可以使用IPN或PDT將信息返回到您的網站。兩種方式中更好的方法是使用IPN,或至少將IPN與PDT結合使用。

Instant Payment Notification (IPN)是一種消息服務,通知您與PayPal交易相關的事件。您可以使用它來自動執行後臺和管理功能,例如履行訂單,跟蹤客戶以及提供與交易相關的狀態和其他信息。

你可以找到更多關於IPN頁here。同樣在該頁面的左邊是一些更有用的鏈接。有用於創建監聽器,設置,測試,IPN歷史記錄,IPF與FMF,IPN/PDT變量以及示例代碼here的頁面。還有一些sample code here的例子。

PayPal’s PDT系統向使用PayPal付款標準的商家網站發送訂單確認,並讓他們驗證此信息。然後這些站點可以在「訂單確認」頁面中本地顯示這些數據。 IPN比PDT更可靠,並且PDT也依賴於買家點擊一個按鈕返回到您的網站。如果他們沒有點擊按鈕返回到您的網站,則不會發回任何信息,您也不能像IPN那樣重新發送此信息。您可以在PDT here上找到更多信息。

我個人只在我的網站上使用PDT來創建一個動態感謝頁面,並使用IPN更新我的數據庫並自動完成一些任務。希望這可以幫助。 :)

示例PHP(V5.2)IPN SCRIPT

<?php 

// STEP 1: Read POST data 

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST 
// reading raw POST data from input stream instead. 
$raw_post_data = file_get_contents('php://input'); 
$raw_post_array = explode('&', $raw_post_data); 
$myPost = array(); 
foreach ($raw_post_array as $keyval) { 
    $keyval = explode ('=', $keyval); 
    if (count($keyval) == 2) 
    $myPost[$keyval[0]] = urldecode($keyval[1]); 
} 
// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
if(function_exists('get_magic_quotes_gpc')) { 
    $get_magic_quotes_exists = true; 
} 
foreach ($myPost as $key => $value) {   
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
     $value = urlencode(stripslashes($value)); 
    } else { 
     $value = urlencode($value); 
    } 
    $req .= "&$key=$value"; 
} 


// STEP 2: Post IPN data back to paypal to validate 

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr'); 
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); 

// In wamp like environments that do not come bundled with root authority certificates, 
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below. 
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); 
if(!($res = curl_exec($ch))) { 
    // error_log("Got " . curl_error($ch) . " when processing IPN data"); 
    curl_close($ch); 
    exit; 
} 
curl_close($ch); 


// STEP 3: Inspect IPN validation result and act accordingly 

if (strcmp ($res, "VERIFIED") == 0) { 
    // check whether the payment_status is Completed 
    // check that txn_id has not been previously processed 
    // check that receiver_email is your Primary PayPal email 
    // check that payment_amount/payment_currency are correct 
    // process payment 

    // assign posted variables to local variables 
    $item_name = $_POST['item_name']; 
    $item_number = $_POST['item_number']; 
    $payment_status = $_POST['payment_status']; 
    $payment_amount = $_POST['mc_gross']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email']; 
} else if (strcmp ($res, "INVALID") == 0) { 
    // log for manual investigation 
} 
?> 

樣品PDT PHP(V5.3)SCRIPT

<?php 

$pp_hostname = "www.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox 


// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-synch'; 

$tx_token = $_GET['tx']; 
$auth_token = "GX_sTf5bW3wxRfFEbgofs88nQxvMQ7nsI8m21rzNESnl_79ccFTWj2aPgQ0"; 
$req .= "&tx=$tx_token&at=$auth_token"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here, 
//if your server does not bundled with default verisign certificates. 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname")); 
$res = curl_exec($ch); 
curl_close($ch); 

if(!$res){ 
    //HTTP ERROR 
}else{ 
    // parse the data 
    $lines = explode("\n", $res); 
    $keyarray = array(); 
    if (strcmp ($lines[0], "SUCCESS") == 0) { 
     for ($i=1; $i<count($lines);$i++){ 
     list($key,$val) = explode("=", $lines[$i]); 
     $keyarray[urldecode($key)] = urldecode($val); 
    } 
    // check the payment_status is Completed 
    // check that txn_id has not been previously processed 
    // check that receiver_email is your Primary PayPal email 
    // check that payment_amount/payment_currency are correct 
    // process payment 
    $firstname = $keyarray['first_name']; 
    $lastname = $keyarray['last_name']; 
    $itemname = $keyarray['item_name']; 
    $amount = $keyarray['payment_gross']; 

    echo ("<p><h3>Thank you for your purchase!</h3></p>"); 

    echo ("<b>Payment Details</b><br>\n"); 
    echo ("<li>Name: $firstname $lastname</li>\n"); 
    echo ("<li>Item: $itemname</li>\n"); 
    echo ("<li>Amount: $amount</li>\n"); 
    echo (""); 
    } 
    else if (strcmp ($lines[0], "FAIL") == 0) { 
     // log for manual investigation 
    } 
} 

?> 


Your transaction has been completed, and a receipt for your purchase has been emailed to you.<br> You may log into your account at <a href='https://www.paypal.com'>www.paypal.com</a> to view details of this transaction.<br> 
+0

您可以通過測試付款並查看腳本是否符合要求,或者使用開發者帳戶中的IPN模擬器發送測試IPN POST。 – 2013-05-02 17:50:12

+0

您將無法使用IPN URL的本地主機地址。請記住,IPN是在幕後發送的,因此當PayPal服務器發送出去時,它會將它發送到該服務器上的本地主機,而不是您的本地主機。 – 2013-05-02 18:33:08

+0

我是否還需要將我的按鈕更改爲 2013-05-02 18:44:00

相關問題