douji5746 2013-07-31 07:36
浏览 43
已采纳

Codeigniter paypal_lib IPN无法正常工作

I am trying to capture when the user made a payment in paypal(sandbox). IPN was the answer for this but it seems I am not getting any response from it. I tried to put it in a log and even the DB but there are no data entering. I know IPN is a listener but I dunno why isnt working.. Any clarification and thoughts about this?

I am using codeigniter Paypal_Lib

Here are my script:

First I declared the fields

$this->paypal_lib->add_field('cmd','_cart');
$this->paypal_lib->add_field('upload','1');
$this->paypal_lib->add_field('business', $business_email);
$this->paypal_lib->add_field('return', site_url('invoice/payment_success/'.$invoice_id.'/'.$album_id));
$this->paypal_lib->add_field('cancel_return', site_url('invoice/payment_cancel/'.$invoice_id));
$this->paypal_lib->add_field('notify_url', site_url('invoice/payment_validate/'.$invoice_id)); // <-- IPN url
$this->paypal_lib->add_field('custom', $invoice_id); // <-- Verify return

Here is for my IPN , just checking if it works by having a log but no luck.

public function payment_validate(){
        //$this->paypal_lib->dump();
        $invoice_id = $this->uri->segment(3);

        if ($this->paypal_lib->validate_ipn()){

            $payer_email = $this->paypal_lib->ipn_data['payer_email'];


             $fp=fopen('temp/logs/paypal_ipn.log','a');
                fwrite($fp, $this->paypal_lib->ipn_data  . "
");

                fclose($fp);  // close file

        }

    }

And here is the validate_ipn in the library.

function validate_ipn() {

// get instance
$CI =& get_instance();

// parse the paypal URL
$url_parsed = parse_url($this->paypal_url);

// generate the post string from the _POST vars aswell as load the
// _POST vars into an arry so we can play with them from the calling
// script.
$post_string = '';
#if(count($_POST))
if($_POST)
{
    #foreach (array_keys($_POST) as $field)
    foreach ($_POST as $field => $value)
    {
        #$value = $CI->input->post($field, true);
        $this->ipn_data[$field] = $value;
        $post_string .= $field.'='.urlencode(stripslashes($value)).'&';
    }
}

$post_string.='cmd=_notify-validate'; // append ipn command

// open the connection to paypal
$fp = fsockopen($url_parsed['host'],'80',$err_num,$err_str,30);
if(!$fp)
{
    // could not open the connection.  If loggin is on, the error message
    // will be in the log.
    $this->last_error = 'fsockopen error no. '.$err_num.': '.$err_str;
    $this->log_ipn_results(false);
    return false;
}
else
{
    // Post the data back to paypal
    fputs($fp, "POST $url_parsed[path] HTTP/1.1
");
    fputs($fp, "Host: $url_parsed[host]
");
    fputs($fp, "Content-type: application/x-www-form-urlencoded
");
    fputs($fp, "Content-length: ".strlen($post_string)."
");
    fputs($fp, "Connection: close

");
    fputs($fp, $post_string . "

");

    // loop through the response from the server and append to variable
    while(!feof($fp))
        $this->ipn_response .= fgets($fp, 1024);

    fclose($fp); // close connection
}


if (eregi('VERIFIED',$this->ipn_response))
{
    // Valid IPN transaction.
    if ($this->ipn_log_method == 'db')
    {
        return $this->log_ipn_results(true);
    }


    /*// Valid IPN transaction.
    $this->log_ipn_results(true);*/
    return true;
}
else
{
    // Invalid IPN transaction.  Check the log for details.
    $this->last_error = 'IPN Validation Failed.';
    $this->log_ipn_results(false);
    return false;
}

}

  • 写回答

2条回答 默认 最新

  • duanqinbi9029 2013-10-03 06:44
    关注

    Answering my own question:

    Unable to receive paypal IPN because there's a problem when connecting to paypal , fixing this issue all you have to do is to change the port from:

    $fp = fsockopen($url_parsed['host'],'80',$err_num,$err_str,30); 
    

    to

    $fp = fsockopen($url_parsed['host'],'443',$err_num,$err_str,30);
    

    Now receiving the connection is established and receiving paypal IPN notification.

    Later part I experience that testing IPN in sandbox is failing and I found out that IPN in sandbox is not working. You can refer here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度