PayPal IPN模拟器返回INVALID回复

时间:2015-12-23 13:24:43

标签: yii paypal paypal-ipn

我正在尝试实现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();
}

1 个答案:

答案 0 :(得分:0)

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