【问题标题】:PayPal IPN simulator returning INVALID answerPayPal IPN 模拟器返回无效答案
【发布时间】:2016-03-29 22:49:32
【问题描述】:

我正在尝试实现 paypal IPN 侦听器。问题是,无论我尝试什么,我仍然从 IPN 模拟器得到无效的答案。我搜索了许多论坛,但没有任何帮助。有谁知道,可能是什么问题?

请在下面查看我的代码。 YII_DEBUG 为真 USE_SANDBOX 为真 我尝试了使用或不使用 cacert.pem 的实现,没有任何效果。

public function actionProcessPayment()
{
    // Read POST data
    // reading posted data 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');
    if(YII_DEBUG == true) {
        Yii::log(date('[Y-m-d H:i e] '). "Original data: " . $raw_post_data . PHP_EOL, 'warning');
    }

    $req = 'cmd=_notify-validate&'.$raw_post_data;


    // Post IPN data back to PayPal to validate the IPN data is genuine
    // Without this step anyone can fake IPN data
    if(self::USE_SANDBOX == true) {
        $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    } else {
        $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
    }

    $ch = curl_init($paypal_url);
    if ($ch == FALSE) {
        return FALSE;
    }
    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);
    if(YII_DEBUG == true) {
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    }

    // Set TCP timeout to 30 seconds
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    $cert = dirname(__FILE__).'/../components/cacert.pem';
    curl_setopt($ch, CURLOPT_CAINFO, $cert);

    $res = curl_exec($ch);
    if (curl_errno($ch) != 0) // cURL error
    {
        if(YII_DEBUG == true) {
            Yii::log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 'warning');
        }
        curl_close($ch);
        exit;
    } else {
        // Log the entire HTTP response if debug is switched on.
        if(YII_DEBUG == true) {
            Yii::log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 'warning');
            Yii::log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 'warning');
        }
        curl_close($ch);
    }

    // Inspect IPN validation result and act accordingly
    // Split response headers and payload, a better way for strcmp
    $tokens = explode("\r\n\r\n", trim($res));
    $res = trim(end($tokens));
    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 PayPal email
        // check that payment_amount/payment_currency are correct
        // process payment and mark item as paid.
        // assign posted variables to local variables
        //$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'];
        if(isset($_POST['custom'], $_POST['payment_status']))
            if($_POST['payment_status'] == 'Completed')
                Order::processPayment($_POST['custom']);

        if(YII_DEBUG == true) {
            Yii::log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 'warning');
        }
    } else if (strcmp ($res, "INVALID") == 0) {
        // log for manual investigation
        // Add business logic here which deals with invalid IPN messages
        if(YII_DEBUG == true) {
            Yii::log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 'warning');
        }
    }

    Yii::app()->end();
}

【问题讨论】:

    标签: yii paypal paypal-ipn


    【解决方案1】:

    问题是因为IPN模拟器自动用本地(捷克)格式的日期填充字段payment_date和(StředníEvropa(běžnýčas)字符串。我在IPN模拟器中将其替换为(GMT标准时间)并且everythink现在工作正常.. .

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 2016-10-07
      • 2018-10-29
      • 2016-11-28
      • 2016-09-25
      • 2012-04-26
      • 2010-12-01
      • 2014-10-21
      相关资源
      最近更新 更多