柳暗花明笔记

    paypal ipn 接收到消息后并返回给paypal

    所属分类:[ php&web ] 猫哲学家 发表于:2014-09-24 15:05 浏览:514

    我试过两种方法,第一种没成功,但第二种成功了

     

    第一种:

    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
    }

    // post back to PayPal system to validate
    $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    $fp = fsockopen(cfg('paypal', 'ipn_url'), 443, $errno, $errstr, 30);

    if (!$fp) {
    // HTTP ERROR
    error_log('Paypal new dispute post back http error.', 3, './log');
    } else {
    fputs($fp, $header . $req);
    while (!feof($fp)) {
    $res = fgets($fp, 1024);echo $res,'
    ';
    if (strcmp($res, 'VERIFIED') == 0) {
    // PAYMENT VALIDATED & VERIFIED!
    $post_back = 'VERIFIED';
    } else if (strcmp ($res, 'INVALID') == 0) {
    // PAYMENT INVALID & INVESTIGATE MANUALY!
    $post_back = 'INVALID';
    }
    }

    fclose ($fp);
    }

     

    第二种

    $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";
    }

    $paypal_url = cfg('paypal', 'ipn_url');

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

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

    $res = curl_exec($ch);
    if (curl_errno($ch) != 0) { // cURL error
    curl_close($ch);
    error_log('Paypal new dispute post back http error.', 3, './log');
    } else {
    curl_close($ch);
    }

    // Inspect IPN validation result and act accordingly
    if (strcmp ($res, "VERIFIED") == 0) {
    // 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'];

    $post_back = 'VERIFIED';

    } else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
    // Add business logic here which deals with invalid IPN messages
    $post_back = 'INVALID';
    }

     

    其实接收到paypal ipn消息后不回发也可以的,你觉得呢?

    标签: php 
    上一编文章 php一个相当好用的测试函数,我居然不知道 error_log 下一编文章 git 查看版本修改命令
    最近文章
    发表评论