php - Codeigniter paypal_lib IPN not working -
i trying capture when user made payment in paypal(sandbox). ipn answer seems not getting response it. tried put in log , db there no data entering. know ipn listener dunno why isnt working.. clarification , thoughts this?
i using codeigniter paypal_lib
here script:
first declared 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 ipn , checking if works having log 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 . "\n"); fclose($fp); // close file } }
and here validate_ipn in library.
function validate_ipn() {
// instance $ci =& get_instance(); // parse paypal url $url_parsed = parse_url($this->paypal_url); // generate post string _post vars aswell load // _post vars arry can play them calling // script. $post_string = ''; #if(count($_post)) if($_post) { #foreach (array_keys($_post) $field) foreach ($_post $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 connection paypal $fp = fsockopen($url_parsed['host'],'80',$err_num,$err_str,30); if(!$fp) { // not open connection. if loggin on, error message // in log. $this->last_error = 'fsockopen error no. '.$err_num.': '.$err_str; $this->log_ipn_results(false); return false; } else { // post data paypal fputs($fp, "post $url_parsed[path] http/1.1\r\n"); fputs($fp, "host: $url_parsed[host]\r\n"); fputs($fp, "content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "content-length: ".strlen($post_string)."\r\n"); fputs($fp, "connection: close\r\n\r\n"); fputs($fp, $post_string . "\r\n\r\n"); // loop through response server , append 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 log details. $this->last_error = 'ipn validation failed.'; $this->log_ipn_results(false); return false; }
}
answering own question:
unable receive paypal ipn because there's problem when connecting paypal , fixing issue have change 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 connection established , receiving paypal ipn notification.
later part experience testing ipn in sandbox failing , found out ipn in sandbox not working. can refer here
Comments
Post a Comment