2015-10-06 133 views
0

我有一個關於PHP按鈕的問題。我想用paypal做一個動態購買按鈕。一個按鈕,我可以傳遞我的自定義變量。我知道如何通過貝寶形式的唯一途徑。PHP按鈕,處理一個PHP文件,然後重定向到另一頁

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<input type="hidden" name="cmd" value="_xclick"> 
<input type="hidden" name="business" value="[email protected]"> 
<input type="hidden" name="item_name" value="my product"> 
<input type="hidden" name="item_number" value="12345"> 
<input type="hidden" name="amount" value="9.99"> 
<input type="hidden" name="no_note" value="1"> 
<input type="hidden" name="currency_code" value="USD"> 
<input type="hidden" name="bn" value="PP-BuyNowBF"> 
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"> 
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> 
</form> 

但上述表格的金額值很容易被篡改。所以,而不是我想在HTML中只有一個按鈕,並通過一個PHP文件處理一切。當用戶點擊該按鈕時,價格計算過程將在php文件中發生,然後將用戶重定向到paypal,他們可以在這裏支付該項目。這樣的價格不能被篡改。有人能告訴我是否可以這樣做嗎?或者我必須使用openssl動態加密按鈕?或者有另一種方式?

+0

實際上,只要您驗證PayPal回撥(返回網址)上的值,是否有人篡改按鈕上的值並不重要。 –

+0

您需要訂單ID或交易ID。基本上,您的會話或數據庫中包含所有其他值,並使用ID來引用值。當用戶退出時,您可能需要執行一些cURL操作,以便您可以像表單一樣將所有數據提交到PayPal API,但不會將內容放置在瀏覽器中。 – Twisty

回答

2

除了直接發佈到PayPal,您可以收集所有數據服務器端,並使用cURL將數據發佈到PayPal。形式看起來更象:

<form action="confirm.php" method="post"> 
    <input type="hidden" name="business" value="[email protected]"> 
    <input type="hidden" name="item_number" value="12345"> 
    <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"> 
    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> 
</form> 

然後confirm.php會做的事情了一把。查找價格,計算總價等。

<?php 
$bizName = isset($_POST['business']):$_POST['business']:""; 
$itemNumber = isset($_POST['item_number'])?intval($_POST['item_number']):""; 

// Lookup details for the Item, purchase, and collect them into an array maybe 
$itemDetails = array(
    "cmd" => "_xclick", 
    "amount" => $itemPrice, // Get from DB 
    "no_note" => 1, 
    "currency_code" => "USD", 
    "bn" => "PP-BuyNowBF", 
    "business" => $bizName, 
    "item_name" => $itemName, // Get from DB 
    "item_number" => $itemNumber 
); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://www.paypal.com/cgi-bin/webscr"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($itemDetails)); 
// receive server response ... 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$svr_out = curl_exec ($ch); 
curl_close ($ch); 
// Do something with $svr_out, maybe display it, or if it's a redirect, redirect the user in turn. 
?> 

這是未經測試的,更多的是一個模板,讓你開始。我相信你的網站比較複雜,你可以從此建立起來。這只是你可以做的事情的一個例子。

+0

非常感謝! –

相關問題