Paypal成功付款,用户在php自动购买东西后获得价值

时间:2018-04-16 22:25:25

标签: php paypal

如何检查用户是否在php / paypal中自动购买了我的产品?

我有一个贝宝按钮:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="xxx">
<input type="image" src="/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal">
<img alt="" border="0" src="/pixel.gif" width="1" height="1">
</form>

我配置了一个返回网址。 但我不知道接下来要做什么来获得付款成功并允许该用户访问我的网站。

1 个答案:

答案 0 :(得分:1)

只需等待来自PayPal服务器的IPN instant-payment-notification请求

以下是基于these samples

的非常简单的示例
<?php
// the PHP script that is supposed to receive the IPN request 
require('PaypalIPN.php');

$ipn = new PaypalIPN();
$verified = $ipn->verifyIPN();
if ($verified) {
    /*
     * Process IPN
     * A list of variables is available here:
     * https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNandPDTVariables/
     */
    if(checkPriceForPayPalIPN($_POST['item_number'], $_POST['mc_gross'],$_POST['mc_currency'])){
        // correct payment, allow user to access (activate the product)
    }else{
        // the price paid for the item is not correct  
    }
}
// Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
header("HTTP/1.1 200 OK");


function checkPriceForPayPalIPN($item_number, $mc_gross, $mc_currency)
{
    //query the database to check the price of the item here
    // --  select * from myProducts
    // where item_number = ? and mc_gross = ? and mc_currency = ?
    if($correct) return true;
    return false;
}