PHP Paypal IPN有时会失败

时间:2013-08-23 23:04:13

标签: paypal-ipn

我有一个PHP脚本,我已经使用了2年,并将其修改为paypal更改为http 1.1时的工作。它几乎适用于所有交易,但最近失败了,我无法弄清楚原因。以下是代码。

尝试查看响应时失败。它是无效的

它可能与地址中的非标准字符有关吗?

我一次又一次地尝试发送ipn请求,但总是失败

function paypal_ipn() {
    $req = 'cmd=_notify-validate';

    foreach($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&{$key}={$value}";
    }

    $res = '';
    $ch = curl_init(paypal_url());
    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'));   
    $res = curl_exec($ch);
    curl_close($ch);

    if(PAYPAL_IPN_DEBUG && !empty($_POST)) {
        $fname = 'ipn_intial_' . date('Y.m.d-h.i.s', time()) . '.txt';
        file_put_contents('log/' . $fname, $req);

        $fname = 'ipn_intial_result_' . date('Y.m.d-h.i.s', time()) . '.txt';
        file_put_contents('log/' . $fname, $res);

    }

    if(strcmp($res, "VERIFIED") == 0) {

        switch($_POST['txn_type']) 
{
        case 'web_accept':
            $data = array();
            $data['name'] = $_POST['first_name'] . ' ' . $_POST['last_name'];
            $data['email'] = $_POST['payer_email'];
            $data['txn_id'] = $_POST['txn_id'];
            $data['payment_status'] = 'Completed';
            $res = save_payment($data);
            if ($res) {
                $data = payment_details(array('txn_id' => $data['txn_id']));
                mail_notification($data);
            }            
            break;

        case 'subscr_signup':
            $params = array();
            $params['name'] = $_POST['first_name'] . ' ' . $_POST['last_name'];
            $params['email'] = $_POST['payer_email'];
            $params['subscr_id'] = $_POST['subscr_id'];
            $params['subscr_status'] = 'payment';

            $data = save_subscription($params);
            subscr_notification($data);
            break;
        case 'subscr_cancel':
            $data = subscr_details(array('subscr_id' => $_POST['subscr_id']));

            if ($data)
            {
                mysql_update('subscriptions', array('subscr_id' => $_POST['subscr_id']), array('subscr_status' => 'cancelled', 'cancel_date' => date('Y-m-d H:i:s')));
            }
            break;
        }

    }

    if(PAYPAL_IPN_DEBUG && !empty($_POST)) {
        $fname = 'ipn_' . date('Y.m.d-h.i.s', time()) . '.txt';
        file_put_contents('log/' . $fname, serialize($_POST));
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个棘手的问题。魔术引号已关闭,此脚本假定魔术引号已启用。因此,无需调用stripslashes这导致错误