【问题标题】:PayPal IPN development贝宝 IPN 开发
【发布时间】:2013-01-13 15:13:33
【问题描述】:

在我的网站上设置 PayPal IPN 以管理订阅付款方面取得了一定的成功。我目前在 PayPal 中设置付款很好,并且将完成的付款从 PayPal 沙箱发送到我的 IPN 效果很好,并将 MySQL 行设置为 is_paid 为 Yes 而不是 No,这反过来又允许用户登录。

我现在想知道,当用户的订阅付款失败或他们取消时(所有那些在现实世界中发生的糟糕和讨厌的事情!),IPN 将如何让我知道?我希望 IPN 向管理员发送电子邮件

<?php
// tell PHP to log errors to ipn_errors.log in this directory
ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__).'/ipn_errors.log');

// intantiate the IPN listener
include('ipnlistener.php');
$listener = new IpnListener();

// tell the IPN listener to use the PayPal test sandbox
$listener->use_sandbox = true;

// try to process the IPN POST
try {
    $listener->requirePostMethod();
    $verified = $listener->processIpn();
} catch (Exception $e) {
    error_log($e->getMessage());
    exit(0);
}

// TODO: Handle IPN Response here

if ($verified) {

    $errmsg = '';   // stores errors from fraud checks

    // 1. Make sure the payment status is "Completed" 
    if ($_POST['payment_status'] != 'Completed') { 
        // simply ignore any IPN that is not completed
        exit(0); 
    }

    // 2. Make sure seller email matches your primary account email.
    if ($_POST['receiver_email'] != 'EMAILHERE') {
        $errmsg .= "'receiver_email' does not match: ";
        $errmsg .= $_POST['receiver_email']."\n";
    }

    // 3. Make sure the amount(s) paid match
    if ($_POST['mc_gross'] != '3.99') {
        $errmsg .= "'mc_gross' does not match: ";
        $errmsg .= $_POST['mc_gross']."\n";
    }

    // 4. Make sure the currency code matches
    if ($_POST['mc_currency'] != 'GBP') {
        $errmsg .= "'mc_currency' does not match: ";
        $errmsg .= $_POST['mc_currency']."\n";
    }

    // 5. Ensure the transaction is not a duplicate.
    mysql_connect('', '', '') or exit(0);
    mysql_select_db('') or exit(0);

    $txn_id = mysql_real_escape_string($_POST['txn_id']);
    $sql = "SELECT COUNT(*) FROM payments WHERE txn_id = '$txn_id'";
    $r = mysql_query($sql);

    if (!$r) {
        error_log(mysql_error());
        exit(0);
    }

    $exists = mysql_result($r, 0);
    mysql_free_result($r);

    if ($exists) {
        $errmsg .= "'txn_id' has already been processed: ".$_POST['txn_id']."\n";
    }

    if (!empty($errmsg)) {

        // manually investigate errors from the fraud checking
        $body = "IPN failed fraud checks: \n$errmsg\n\n";
        $body .= $listener->getTextReport();
        mail('EMAILHERE', 'PayPal IPN Fraud Warning', $body);

    } else {

    // add this order to a table of completed orders
    $payer_email = mysql_real_escape_string($_POST['payer_email']);
    $mc_gross = mysql_real_escape_string($_POST['mc_gross']);
    $sql = "INSERT INTO payments VALUES 
            (NULL, '$txn_id', '$payer_email', $mc_gross)";

    if (!mysql_query($sql)) {
        error_log(mysql_error());
        exit(0);
    }

    // sends an email to the user to confirm subscription
    $websitepath="http://www.gaymate.co.uk";
    $message='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>gaymate.co.uk</title>

        <style type="text/css">

        p {
            font-family: Arial;
            font-size: 12px;
            padding: 0 20px 0 20px; }

        .boldTitle {
            font-size: 13px;
            font-weight: bold; }

        .headerTitle {
            font-size: 16px;
            font-weight: bold; }

        .divider2 {
            padding: 0 20px 0 20px; }

        </style>

        </head>

        <body>
        <table width="500" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td>
            <a href="http://www.gaymate.co.uk/"><img src="http://www.gaymate.co.uk/images/email-header.jpg"></a>
            <img class="divider2" src="'.$websitepath.'/images/email/email-divider2.gif" alt="email-divider2" width="460" height="4" />
        <p>We have recieved confirmation from PayPal that you have completed payment and we are pleased to tell you that your account will be live on our site as soon as it has been approved by one of the moderators.</br></br>The Admin Team @ GayMate.co.uk</p>
       <img src="'.$websitepath.'/images/email/email-divider.gif" alt="email-divider" width="500" height="10" />
    </td>
  </tr>
</table>
<p>&nbsp;</p>
</body>
</html>
          ';


    $to = filter_var($_POST['payer_email'], FILTER_SANITIZE_EMAIL);
    $subject = "GayMate.co.uk Subscription Set-Up Completed";
    $header = "MIME-Version: 1.0\r\n";
    $header .= "From: EMAILHERE\r\n";
    $header .= "Reply-To: EMAILHERE\r\n";
    $header .= "Content-type: text/html; charset=iso-8859-1\r\n";
    mail($to, $subject, $message, $header);  
    }
    $sql = "UPDATE member SET is_paid='Yes' WHERE email='$to'";

    if (!mysql_query($sql)) {
        error_log(mysql_error());
        exit(0);
    }

} else {
    // manually investigate the invalid IPN
    mail('EMAILHERE', 'Invalid IPN', $listener->getTextReport());
}


?>

【问题讨论】:

    标签: php paypal paypal-ipn


    【解决方案1】:

    将这些添加到您的代码中并将 // email code // 注释更改为您的电子邮件代码,它会正常工作。

    之前:

    if ($_POST['payment_status'] != 'Completed') { 
    

    添加:

    // denied payment
    if ($_POST['payment_status'] == 'Denied') { 
    // email code
    exit(0);
    }
    
    // failed subscription payment
    if ($_POST['txn_type'] == 'subscr_failed') { 
    // email code
    exit(0);
    }
    
    // canceled subscription
    if ($_POST['txn_type'] == 'subscr_cancel') { 
    // email code
    exit(0);
    }
    

    只需在评论中创建发送电子邮件的必要条件。

    【讨论】:

      【解决方案2】:

      您需要检查$_POST['txn_type'] 的值才能知道通知的内容。

      来自 PayPal 的文档:https://www.paypal.com/cgi-bin/webscr?cmd=p/acc/ipn-subscriptions-outside

      相信你对subscr_eotsubscr_failed感兴趣。

      一些可能对您有所帮助的额外信息:

      • subscr_cancel 会在用户取消订阅后立即发送。
      • subscr_eot 在订阅到期后发送,但他们的付款未通过。

      一旦用户取消订阅,PayPal 将向您发送subscr_cancel,然后在下一次付款到期时发送subscr_eot

      您可能还想阅读这个相关问题:Subscriptions with Paypal IPN

      【讨论】:

        猜你喜欢
        • 2015-06-16
        • 2016-05-09
        • 2023-03-11
        • 2014-04-24
        • 2012-08-19
        • 2012-08-15
        • 2014-08-25
        • 2013-07-10
        • 2013-07-28
        相关资源
        最近更新 更多