douchen7324 2013-04-27 17:27
浏览 53
已采纳

PHP PayPal IPN确认邮件问题

I have managed to setup PDT and now to IPN. The problem is that the seller account do get a confirmation mail but not the buyer even though I use the same code, it doesnt make sense.

Heres my IPN.php:

        <?php
header('Content-type: text/html; charset=utf-8');
?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?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);
}

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'] != '*****') {
        $errmsg .= "'receiver_email' does not match: ";
        $errmsg .= $_POST['receiver_email']."
";
    }

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

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

    // 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 orders 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']."
";
    }

    if (!empty($errmsg)) {

        // manually investigate errors from the fraud checking
        $body = "IPN failed fraud checks: 
$errmsg

";
        $body .= $listener->getTextReport();
        mail('*****', '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 orders VALUES 
            (NULL, '$txn_id', '$payer_email', $mc_gross)";

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

    // send user an email with a link to their digital download
    $num = $_POST['num_cart_items'];
    $amount = $_POST['mc_gross'];
    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $to = filter_var($_POST['payer_email'], FILTER_SANITIZE_EMAIL);
    $to2 = filter_var($_POST['*****'], FILTER_SANITIZE_EMAIL);
    $subject = "Tack för Ert köp! / Thank you for your order!";
    $subject2 = "(COPY) Tack för Ert köp! / Thank you for your order!";
    $headerFields = array(
    'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
    "Subject: =?UTF-8?Q?".imap_8bit($subject)."?=",
    "From: {$to}",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
    );
    $headerFields2 = array(
    'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
    "Subject: =?UTF-8?Q?".imap_8bit($subject2)."?=",
    "From: {$to}",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
    );
    $message = "$firstname $lastname, $payer_email <br />
Du köpte $num produkter för totalt $amount kronor. Alla priser är i svenska kronor inklusive moms, 12% respektive 25% (12% moms på EnergyUnion, 25% moms på resten av sortimentet) <br />
You bought $num products for a total of $amount Swedish kronor. All prices are in Swedish kronor, including VAT, 12% and 25% respectively (12% Tax on Energy Union, 25% VAT on the rest of the product range) <br />
Välkommen åter! <br>
Hälsningar, <br>
*****";
    $message2 = "$firstname $lastname, $payer_email <br />
Du köpte $num produkter för totalt $amount kronor. Alla priser är i svenska kronor inklusive moms, 12% respektive 25% (12% moms på EnergyUnion, 25% moms på resten av sortimentet) <br />
You bought $num products for a total of $amount Swedish kronor. All prices are in Swedish kronor, including VAT, 12% and 25% respectively (12% Tax on Energy Union, 25% VAT on the rest of the product range) <br />
Välkommen åter! <br>
Hälsningar, <br>
***** <br />
(NOTE: THIS IS A COPY)";
    mail($to, $subject, $message,  implode("
", $headerFields));
    mail($to2, $subject2, $message2,  implode("
", $headerFields2));   
    }

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


?>

And if you look at the bottom of it you can see that I try to send 2 mails, one to the buyer and one to the seller with practically identical code (only mailadress changes), but still only seller gets the mail. Why is this?

  • 写回答

1条回答 默认 最新

  • doutao6380 2013-04-30 16:07
    关注

    Found out that my webhost has a limit of 25mail/day OUTSIDE their domain and then it wont work anymore, also that receiver_email has to be defined so its "within" the domain. This applied to one.com as webhost.

    Updated the script with fully working one and added Date, From, UTF-8 in both header and message and nicely formatted text in the message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 输出区间内所有的完数的个数。 要简单点的程序
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码