1

我有一个有效的 IPN 脚本。它正在根据登录的用户正确更新数据库中的数据。因此,一旦付款成功,用户帐户将升级为付费会员。而且由于我使用的是贝宝订阅按钮。因此,从下个月开始计费流程将自动进行。
所以,我认为(我不确定),Paypal 不会与存储在我的服务器中的 IPN 脚本进行交互。

所以,我的问题是:-

如果我对 IPN 脚本的假设是正确的,那么我如何跟踪哪个用户为下一个计费周期付款?(我不想参与手动工作,例如从我的 Paypal 商家帐户跟踪用户付款信息。我只想通过脚本来完成。因此,一旦从用户 Paypal 帐户中扣除订阅金额,他的帐户就在我的网站将升级为付费会员。)

作为参考,我想通过我的 ipn 脚本更新什么。下面是我的 IPN 脚本。

<?php

// STEP 1: Read POST data

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST
// reading raw POST data from input stream instead. 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
     $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
        $value = urlencode(stripslashes($value)); 
   } else {
        $value = urlencode($value);
   }
   $req .= "&$key=$value";
}


// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether 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

    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_name=strip_tags($item_name);
    $item_number = strip_tags($_POST['item_number']);
    $payment_status = strip_tags($_POST['payment_status']);
    $payment_amount = strip_tags($_POST['mc_gross']);
    $payment_currency = strip_tags($_POST['mc_currency']);
    $txn_id = strip_tags($_POST['txn_id']);
    $user_id=strip_tags($_POST['custom']);
    $receiver_email = strip_tags($_POST['receiver_email']);
    $payer_email = strip_tags($_POST['payer_email']);
    //if(strcmp($receiver_email, "h_1356964205_per@gmail.com") == 0)
    //{
        /*if($payment_status != "Completed")
        {
            $msg="Transaction with id ".$txn_id." status is not completed..";
            mail("support@example.com","Transaction with the same id already exists in database.",$msg,"From:admin@leadstool.net");
    exit();

        }*/
        include_once('connection.php');
        //$user_id=getfield('id');
        // Query to check the duplicate transaction id.
        $query="SELECT `User_id` FROM `transaction` WHERE `Transaction_id`='".mysql_real_escape_string($txn_id)."'";
        if($query_run=mysql_query($query))
        {
            $num=mysql_num_rows($query_run);
            if($num == 0)
            {
                // Query to check the number of times for subscription.
                $query="SELECT `Transaction_id` FROM `transaction` WHERE `User_id`='".mysql_real_escape_string($user_id)."'";
                if($query_run=mysql_query($query))
                {
                    $num=mysql_num_rows($query_run);
                    if($num>=1)
                    {
                        $type_of_subscription=2;// This 2 will denote the user is rnewing his account
                    } else {
                        $type_of_subscription=1;// Here 1 is denoting that user has subscribed for the 1st time.
                        }
                    $query="SELECT `B_ad_no_paid_user`,`T_ad_no_paid_user` FROM `WebsiteContent` WHERE `Creator_id`='1' ORDER BY `Date_of_update` DESC LIMIT 1";
                    if($query_run=mysql_query($query))
                    {
                        while($rows=mysql_fetch_array($query_run))
                        {
                            $banner_ad_limit=$rows['B_ad_no_paid_user'];
                            $text_ad_limit=$rows['T_ad_no_paid_user'];
                        }
                    }   
                }// Query to check the number of times for subscription ends here.
        //Query to insert the transaction details in database.
        $query="INSERT INTO `transaction` VALUES('".$txn_id."','".$user_id."','".$payment_amount."','".$type_of_subscription."','".$payment_status."','1','".$payer_email."',now())";
        if($query_run=mysql_query($query))
        {
            $query="UPDATE `user` SET `User_type`='1', `Banner_ad_limit`='".$banner_ad_limit."', `Text_ad_limit`='".$text_ad_limit."' WHERE `id`='".mysql_real_escape_string($user_id)."'";
            if($query_run=mysql_query($query))
            {
                $msg="Thank you for subscribing to our service. Your Transaction Id is $txn_id.";
            mail("$payer_email","Subscription confirmation mail",$msg,"From:admin@example.com");
            } else {
                $msg="Thank you! Your transaction is successful with transaction id:- $txn_id. But we are unable to upgrade your profile right now. Please contact admin to resolve the problem.";
                mail("$payer_email","Subscription confirmation mail",$msg,"From:admin@example.com");
                }

        } else {
            $msg="For Transaction with id ".$txn_id." failed to update in database.";
            mail("support@example.com","Unable to update the details in database.",$msg,"From:admin@example.com");
    exit();
            }
        // Query to insert data in database ends here.

            } else {
                $msg="Transaction with id $txn_id already exists in database. Admin please verify the details manually and contact the user. Email id of user is: $payer_email";
                mail("support@example.com","Transaction with the same id already exists in database.",$msg,"From:admin@example.com");
    exit();
        }// Query to check the duplicate transaction id ends here.
        }

    //} else {
        //$msg="Investigate the reason why the registered email id with paypal does not matched with this id $receiver_email";
        //mail("support@example.com","Receiver email address do not matched",$msg,"From:admin@example.com");
    //exit();
        //}

} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation

    $msg="Dear administrator please verify the reason why the transaction failure occures. The details is:- $res";
    mail("support@example.com","IPN interaction was not verified.",$msg,"From:admin@example.com");
    exit();
}
?>
4

1 回答 1

0

这可以使用 IPN 和 notify_url 来实现。

这是一个很好的教程:

http://www.techrepublic.com/article/handling-recurring-payments-with-paypal-subscriptions-and-ipn/5331883

于 2013-01-13T13:33:43.137 回答