<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*Controller for Manage Payment for Users */
class Ipn extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->library('paypal_lib');
}

function index()
{
$this->load->library('session');
$this->load->model('commonmodel');
$this->load->model('usersmodel');
$this->load->library('email');
$this->load->helper(array('form', 'url'));

$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
$collumns = array();
$colvalues = array();
foreach ($raw_post_array as $keyval)
{
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
{
$myPost[$keyval[0]] = urldecode($keyval[1]);
array_push($collumns, $keyval[0]);
array_push($colvalues, "'".mysql_real_escape_string(html_entity_decode(urldecode($keyval[1])))."'");
}

}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate&';
$req .= $raw_post_data;
if(isset($_POST["ipn_track_id"]) && $_POST["ipn_track_id"] != '')
{
$paypalurl = "https://www.sandbox.paypal.com/cgi-bin/webscr";

// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init($paypalurl);
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'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);
// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
@$json = json_decode($_REQUEST['custom']);
@$planId = $json->planId;
@$uid = $json->userId;
@$_subscription_id = $_REQUEST['subscr_id'];
@$_player_email = $_REQUEST['payer_email'];
@$customerName = $_REQUEST['last_name'];
@$payment_currency = $_REQUEST['mc_currency'];
@$txn_id = $_REQUEST['txn_id'];
@$payment_status = $_REQUEST['payment_status'];

//== 'Completed';
@$_player_email2 = $this->usersmodel->get_email_by_uid($uid);

/* When Payment is 0 and subscribe the plan Stuff here*/
if($_REQUEST['txn_type'] == 'subscr_signup')
{
/*Your codeing stuff here*/

}
// when payment is more then 0 on recurring
else if($_REQUEST['txn_type'] == 'subscr_payment')
{

/*Your codeing stuff here*/

}

/*Method for Sample Paypal form Paymant - Manually Payment Process Stuff here*/
else if($_REQUEST['txn_type'] == 'web_accept')
{
/*Your codeing stuff here*/
}
/* when user unsubscripe plan*/
else if($_REQUEST['txn_type'] == 'subscr_cancel')
{
/*Your codeing stuff here*/
}

}
else if (strcmp ($res, "INVALID") == 0)
{
$this->email->from('yourmail@gmail.com', 'PAYMENT FAIL');
$this->email->to($this->config->item('mail_from'));
$this->email->subject('PAYMENT FAIL');
$this->email->message('Fail Processing.');
$this->email->send();
}
}
}
}
Advertisements