在Paypal进行订单处理之前,有没有一种方法可以验证付款?
我用我自己的购物车。
当客户单击“提交订单”时,我在另一个页面调用PayPalRedirect.php上重定向用户
PayPalRedirect.php
<form name="paypalform" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="invoice" value="<? echo $idInvoice; ?>">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="notify_url" value="http://mydomain.com/catalog/IPNReceip">
<?
$cpt = 1;
foreach($ordering as $k => $v)
{
?>
<input type="hidden" name="item_name_<? echo $cpt?>" value="<? echo$v->Product->ProductNumber; ?>">
<input type="hidden" name="quantity_<? echo $cpt?>" value="<? echo $v->Qty; ?>">
<input type="hidden" name="amount_<? echo $cpt?>" value="<? echo $v->Price ?>">
<?
$cpt++;
}
?>
<input type="hidden" name="currency_code" value="CAD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
页面加载后,我使用此JavaScript提交表单:
<script>
document.forms.paypalform.submit();
</script>
这时一切都很好。用户将重定向到PayPal页面,并且可以登录到PayPal,然后付款。
我的问题是。贝宝(PayPal)是否有办法在我这边调用Web服务(例如:http://mydomain.com/ValidatePayment.php)并传递贝宝收到的购物车中的所有物品,以确认价格正确。如果价格不正确,我想回应贝宝付款无效,并取消交易。客户单击贝宝页面的PayNow之前的所有操作。
如果可能的话我该怎么办?
非常感谢
最佳答案
我认为您不可能做任何想做的事情。一旦您将客户转到Paypal,他们将处理付款并在最后向您发送确认。
如果要确保客户支付了正确的金额,则应检查Paypal发送给您的确认。
如您所知,Paypal有两种付款确认机制-PDT和IPN。
PDT依靠客户付款后返回您的网站。客户付款后,Paypal将向您发送包含交易详细信息的PDT消息。您可以使用此信息向客户显示收据。问题是客户付款后是否立即关闭浏览器。如果发生这种情况,您可能永远不会收到PDT消息。
IPN不依赖客户端返回您的网站。每次客户付款时,贝宝都会向您发送IPN消息,其中包含交易的详细信息。
您可以使用其中之一来确认付款已完成。您还可以检查是否已按预期金额付款。
请参阅下面我在网站上使用的流程:
1-客户按下按钮以使用Paypal付款
2-我的网站将客户的交易详细信息发送给贝宝
3-贝宝处理付款
4-付款完成后,Paypal会通过PDT消息将客户发送回我的网站。
5-我的网站向Paypal发送确认消息,以检查PDT消息是否合法并来自Paypal。
6-Paypal发回一条消息,其中包含交易的所有详细信息,包括已付的价格。
7-我的网站检查来自Paypal的确认消息,并在屏幕上向客户显示收据
8-付款至我的卖家帐户后,贝宝还将发送IPN消息。
9-当我的网站收到IPN消息时,它会向Paypal发回一条消息,以确认该消息是合法的并且起源于Paypal。
10-然后我的网站检查了从Paypal发回的确认消息,并确认付款正确。
请注意,在大多数情况下,我将从Paypal收到两条消息(一个PDT和一个IPN)。可以,因为我会跟踪每笔交易,并且如果我收到已经标记为已付款的交易的IPN,我只会丢弃IPN消息。
请注意,如果收到PDT消息,则实际上并不需要IPN消息。但是,我建议您同时实现这两种方法,以防万一客户在Paypal有机会将其发送回您的网站之前关闭其浏览器。
在这两个消息(PDT和IPN)中,Paypal都会告诉您付款是否已清除。您可能知道,某些付款类型要等到提交给Paypal几天后才能清除。贝宝(Paypal)建议您在付款清除后再发货。付款清除后,贝宝(Paypal)将向您发送另一条IPN消息。
我的网站上有两个脚本-一个用于处理PDT消息,另一个用于处理IPN消息。在处理付款确认时,它们非常相似。参见以下示例:
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "enter your own authorization token";
$req .= "&tx=$tx_token&at=$auth_token";
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//$fp = fsockopen ('http://www.sandbox.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// replace www.sandbox.paypal.com with www.paypal.com when you go live
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if (!$fp)
{
// HTTP ERROR
}
else
{
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp))
{
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0)
{
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}
// 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);
}
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$itemname = $keyarray['item_name'];
$amount = $keyarray['payment_gross'];
$invoiceid = $keyarray['invoice'];
$profileid = $keyarray['subscr_id'];
// 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
// show receipt to client
}
}
我希望这有帮助。请随时提出后续问题。
祝好运!
关于php - PHP的 Paypal 。在接受付款之前验证付款的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7423809/