有没有办法在 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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="422037312b2c2731311d272f232b2e02273a232f322e276c212d2f" rel="noreferrer noopener nofollow">[email protected]</a>">
<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 有没有办法调用我这边的网络服务(例如: http://mydomain.com/ValidatePayment.php )并传递 PayPal 收到的购物车中的所有商品,以确认价格是否正确。如果价格不正确,我想回复 paypal 付款无效并取消交易。所有这一切都发生在客户点击 paypal 页面的 PayNow 之前。 如果可能的话我该如何做到这一点?
非常感谢
最佳答案
我认为不可能做你想做的事。一旦您将客户发送至 Paypal,他们将处理付款并在最后向您发送确认信息。
如果您想确保客户支付了正确的金额,您应该检查 Paypal 向您发送的确认信息。
您可能知道 Paypal 有两种付款确认机制 - PDT 和 IPN。
PDT 依赖于客户在付款后返回您的网站。客户付款后,Paypal 将向您发送一条包含交易详细信息的 PDT 消息。您可以使用此信息向客户出示收据。问题是客户在付款后是否立即关闭浏览器。如果发生这种情况,您可能永远不会收到 PDT 消息。
IPN 不依赖于客户端返回您的网站。每次客户付款时,Paypal 都会向您发送一条 IPN 消息,其中包含交易详细信息。
您可以使用其中之一或同时使用两者来确认付款已完成。您还可以检查付款是否符合您预期的金额。
请参阅下面我在网站上使用的流程:
1 - 客户按下按钮使用 Paypal 付款
2 - 我的网站将客户发送到 Paypal 并附上交易详细信息
3 - Paypal 处理付款
4 - 付款完成后,Paypal 将通过 PDT 消息将客户发送回我的网站。
5 - 我的网站向 Paypal 发送一条确认消息,以检查 PDT 消息是否合法且来自 Paypal。
6 - Paypal 发回一条消息,其中包含交易的所有详细信息,包括支付的价格。
7 - 我的网站检查来自 Paypal 的确认消息,并在屏幕上向我的客户显示收据
8 - 一旦付款到我的卖家账户,Paypal 也会发送一条 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 Paypal .一种在接受付款之前验证付款的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7423809/