首页 文章

php paypal . 在接受付款之前验证付款的方法

提问于
浏览
1

有没有办法在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="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,然后支付订单 .

我现在的问题是 . 有没有办法让PayPal在我这边调用一个Web服务(例如:http://mydomain.com/ValidatePayment.php)并传递paypal收到的购物车的所有项目以确认价格是否正确 . 如果价格不正确,我想回复PayPal付款无效并取消交易 . 客户点击PayPow页面的PayNow之前的所有内容 . 如果有可能,我怎么能这样做?

非常感谢

1 回答

  • 4

    我认为不可能做你想做的事 . 一旦您将客户端发送到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
        }
    }
    

    我希望这有帮助 . 请随时询问后续问题 .

    祝好运!

相关问题