php贝宝.在接受付款之前验证付款的一种方法


php paypal. A way to validate a payment before accepting the payments

是否有一种方法可以在paypal继续进行订单之前验证付款?

我用自己的购物车。当客户单击Submit Order时,我将用户重定向到另一个页面调用,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="business_email@example.com">
<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然后支付订单。

我的问题是。是否有一种方式与贝宝调用web服务在我身边(例如:http://mydomain.com/ValidatePayment.php),并通过所有的项目的购物车,贝宝收到确认价格是正确的。如果价格不正确,我想回复paypal支付无效,并取消交易。所有这些都是在客户点击paypal页面的PayNow之前。如果可能的话,我该怎么做呢?

Thanks to lot

我认为做你想做的事是不可能的。一旦您将客户发送到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将在付款完成后再次发送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
    }
}

我希望这对你有帮助。请随时提出后续问题。

祝你好运!