【问题标题】:Email not send to buyer in paypal using sandox ipn电子邮件未使用 Sandox ipn 在贝宝中发送给买家
【发布时间】:2014-10-29 01:08:27
【问题描述】:

您好,我正在开发一个可添加响应者、添加产品的 paypal 插件。

我正在开发一个沙盒进行测试。

我做响应者而不是添加与产品相关的响应者。在前端,当我单击“购买”按钮时,它会转到 sandbox.paypal,在这里我完成了付款程序,在我的虚拟商家帐户上我接受了付款并知道电子邮件发送给完成的买家,但电子邮件没有发送我阅读代码 10 次,但我没有得到问题。

这是我的代码:

Paypal 表单:

<form name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_blank">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="'.$paypalID.'">
    <input type="hidden" name="return" value="'.$return_url.'">
    <input type="hidden" name="currency_code" value="'.$currency.'">
    <input type="hidden" name="item_name" value="'.$product_name.'">
    <input type="hidden" name="amount" id="p'.$product_id.'" value="'.$product_price.'">
    <input type="hidden" name="custom" value="'.$responderID.'">
    <input name="notify_url" value="'.plugin_dir_url( __FILE__ ).'ipn_sandbox.php" type="hidden">
    <input type="image" src="'.$upload_image.'" border="0" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>

这是我的 ipn_sandbox.php 代码:

<?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'));

    if( !($res = curl_exec($ch)) ) {
        curl_close($ch);
        exit; 
    }
    curl_close($ch);

    $path = $_SERVER['DOCUMENT_ROOT'];

    include_once $path . '/wp-config.php';
    include_once $path . '/wp-load.php';
    include_once $path . '/wp-includes/wp-db.php';
    include_once $path . '/wp-includes/pluggable.php';

    global $wpdb;
    if (strcmp ($res, "VERIFIED") == 0) {
        $item_name              =   $_POST['item_name'];
        $item_number            =   $_POST['item_number'];
        $payment_status         =   $_POST['payment_status'];
        $payment_amount         =   $_POST['mc_gross'];
        $payment_currency       =   $_POST['mc_currency'];
        $txn_id                 =   $_POST['txn_id'];
        $receiver_email         =   $_POST['receiver_email'];
        $payer_email            =   $_POST['payer_email'];
        $responderID            =   $_POST['custom'];
        $name                   =   $_POST['first_name'];
        $payment_status         =   $_POST['payment_status'];
        $site_url               =   get_bloginfo('wpurl');
        $table_resp             =   $wpdb->prefix.'paypal_responders';
        $responder_to_use       =   $wpdb->get_row("SELECT * FROM $table_resp WHERE id ='$responderID'");
        $subject                =   $responder_to_use->subject;
        $from                   =   $responder_to_use->from_email;
        $attachment             =   $responder_to_use->attachment;
        $att_secure             =   $responder_to_use->att_secure;
        $message        .=  $responder_to_use->message_body;
        $message                .=  '<br /><br />
                                    <a title="Click here to download Attachment" href="'.plugin_dir_url(__FILE__).'responders/download.php?filename='.$att_secure.'" width="150" height="150" target="_blank">Click here to download Attachment</a>';

        if($message){
            $message    =   str_replace('[item_name]',$item_name,$message);
            $message    =   str_replace('[txn_id]',$txn_id,$message);
            $message    =   str_replace(' [mc_gross]',$payment_amount,$message);
            $message    =   str_replace('[mc_currency]',$payment_currency,$message);
            $message    =   str_replace('[receiver_email]',$receiver_email,$message);
            $message    =   str_replace('[payer_email]',$payer_email,$message);
            $message    =   str_replace('[name]',$name,$message);
            $message    =   str_replace('[site_url]',$site_url,$message);
            $message    =   str_replace('[payment_status]',$payment_status,$message);
        }else{
            $message    =       'Dear '.$name.',
                                Thank you for your purchase from '.$site_url.'. The details of your purchase are below.
                                Transaction ID: '.$txn_id.'
                                Item Name: '.$item_name.'
                                Payment Amount: '.$payment_amount.'
                                Payment Amount: '.$payment_status.'
                                Paid to: '.$receiver_email.'
                                Thanks and Enjoy!
                                ~Enigma Digital <br />
                                <br />
                                <a title="Click here to download Attachment" href="'.plugin_dir_url(__FILE__).'responders/download.php?filename='.$att_secure.'" width="150" height="150" target="_blank">Click here to download Attachment</a>';
        }

        $table          =   $wpdb->prefix . "paypal_transactions";
        $txn_id_check   =   $wpdb->get_results("SELECT * FROM $table WHERE txn_id ='$txn_id'");

        if(!$txn_id_check){
            $data   =   array(
                            'txn_id'            =>      $txn_id,
                            'product_name'      =>      $item_name,
                            'product_price'     =>      $payment_amount,
                            'payer_email'       =>      $payer_email,
                        );

            $wpdb->insert($table,$data) or die(mysql_error());
            $num = md5(time());

            $headers .= 'From: ' .$from. "\r\n" .'Reply-To: ' .$from . "\r\n";
            $headers  .= 'MIME-Version: 1.0' . "\r\n";
            $headers  .= "Content-Type: text/html; charset=iso-8859-1 ";
            $headers  .= "--".$num."--";

            //mail to buyer
            mail( $payer_email , $subject, $message, $headers );
        }
    }
?>

除了mail(),我也使用wp_mail(),但没有任何反应。

请任何人帮助解决问题。

【问题讨论】:

  • 你是在本地机器上测试还是在在线服务器上测试?试试error_reporting也许它会帮助你调试问题..
  • @SyedQarib sahib 我正在使用在线服务器
  • 你试过报错了吗?您可以在日志中找到什么吗?

标签: php wordpress paypal paypal-ipn paypal-sandbox


【解决方案1】:

代码存在一些问题,您可以采取一些措施来开始调试过程。不过,您不太可能在这里得到一个简单的解决方法,因为这是一个开放式问题。

在做任何事情之前,请确保在您的php.ini 中将error_reporting 设置为E^ALL 并且display_errors 设置为On。 (Turorial, if you need it)。检查你的error_log - 如果有的话 - 立即获得线索。

您需要在向其附加更多数据之前声明$message。您开始在第 77 行 ($message .= $responder_to_use-&gt;message_body;) 将数据附加到 $message,但尚未设置,这将产生警告。

现在有 3 个原因导致电子邮件本身无法发送:

  1. 您的服务器未设置为发送邮件。
  2. $txn_id_check 未设置或为false。如果$txn_id 不在您的数据库中,就会出现这种情况。我假设您在开始交易时添加了这一行 - 但您检查了吗?
  3. 您在其中一个包含的文件中有语法错误。

让我们谈谈#1。在服务器上创建一个文件,其中包含向您发送一封简单电子邮件的 1 行:

<?php mail('your@email', 'Test email', 'Yay, I can send mail!'); ?>

在浏览器中访问该文件。检查您的收件箱,并检查您的垃圾邮件。没有邮件?这就是问题所在。


下一步#2:这可能是在用户访问 PayPal 之前将交易添加到数据库的脚本的问题。我们没有此代码,但您可以轻松检查 - 只需检查数据库以查看是否存在任何行。


并且#3:语法错误在这里应该很容易检查,只需直接在浏览器中访问 IPN 脚本。将向您显示致命错误。


还是不行?其他地方可能有问题。最好的检查方法是进行一些基本的调试...

首先通过电子邮件将完整的 $_POST 数组发送给自己,就在脚本的顶部。

<?php
    mail('your@email', 'Full Post Data - '.time(), print_r($_REQUEST, true));

    // 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);

    [...]

并在最后通过电子邮件将脚本的输出发送给自己。

<?php
    ob_start();

    $time = time(); // Referenced in your email subjects. It will get confusing with lots of these emails otherwise. Gmail and other clients who use a conversation view will properly catalogue these emails if the subjects match.

    mail('your@email', 'Full Post Data - '.$time, print_r($_REQUEST, true));

    // 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. 

    [...] // Rest of your script here...

    $output_buffer = ob_get_contents();
    ob_end_clean();

    mail('your@email', 'Output buffer - '.$time, $output_buffer);
?>

【讨论】:

    【解决方案2】:
    $cleanedFrom = 'Ashutosh';
                $to ='awesomeworks1@gmail.com; 
                $subject = 'CONTACT US';
                $headers = "From: " . $cleanedFrom . "\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                $message= '<html><body>';
                $message .= '<table rules="all" style="border-color: #68A1D7;" cellpadding="15" width="450" align="center">';
                $message .= "<tr style='background: #68A1D7; color: #fff;'><td colspan='2'><strong>CONTACT US</strong></td></tr>";
                $message .= "<tr><td><strong>Name:</strong> </td><td>" .$ram[0]['name'] . "</td></tr>";
    
                $message .= "<tr style='background:#68A1D7;'><td colspan='2'>&nbsp;</td></tr>";
                $message .= "</table>";
                $message .= "</body></html>";
                $send =    mail($to, $subject, $message, $headers);
                if($send)
                {
                echo "<script> alert('Message Sent. Thank you') </script>";
                echo "<script>window.location='$webroot'</script>";
                exit;
    
    
                }
                else
                {
                echo "<script> alert('Message Not Sent. Thank you') </script>";
                echo "<script>window.location='$webroot'</script>";
                exit;
    
                }
    

    【讨论】:

    • 请使用此代码作为消息,我已测试过此代码运行良好
    • 请在 $message 中传递正确的值 .= " Name: " .$ram[0]['name '] 。 " ";
    • 在这个问题的背景下,这个答案和你的评论似乎都没有多大意义。
    【解决方案3】:

    在 php.ini 中打开邮件。

    你需要有这样的东西:

    在 php.ini 中:

    [mail function]
    ; For Win32 only.
    ;SMTP = localhost
    ;smtp_port = 25
    
    ; For Win32 only.
    ;sendmail_from = me@example.com
    
    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    sendmail_path = /usr/sbin/sendmail -t -i -f  me@example.com
    

    以“;”开头的行被注释掉。因此,请确保根据您使用的操作系统取消注释正确的部分。

    【讨论】:

    • 我想我描述我正在开发一个插件并且插件安装了很多用户,请告诉我如何编辑他们的 php.ini 文件
    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多