Ipn Data Subscriptions

cesgdav
Contributor
Contributor

Does someone can help me to know what data receive the IPN file in php i got this

 

 

<?php

/*
 * Read POST data
 * reading posted data directly from $_POST causes serialization
 * issues with array data in POST.
 * Reading raw POST data from input stream instead.
 */        
$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";
}

/*
 * Post IPN data back to PayPal to validate the IPN data is genuine
 * Without this step anyone can fake IPN data
 */
$paypalURL = "https://ipnpb.sandbox.paypal.com/cgi-bin/webscr";
$ch = curl_init($paypalURL);
if ($ch == FALSE) {
    return FALSE;
}
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_SSLVERSION, 6);
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', 'User-Agent: company-name'));
$res = curl_exec($ch);

/*
 * Inspect IPN validation result and act accordingly
 * Split response headers and payload, a better way for strcmp
 */ 
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp($res, "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) {
    //Include DB configuration file
    //Database credentials
    $dbHost = 'localhost';
    $dbUsername = 'dbUser';
    $dbPassword = '123';
    $dbName = 'dbName';

    //Connect with the database
    $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

    //Display error if failed to connect
    if ($db->connect_errno) {
        printf("Connect failed: %s\n", $db->connect_error);
        exit();
    }
    
    $unitPrice = 25;
    
    //Payment data
    $subscrid = $_POST['subscr_id'];
    $payerwemail = $_POST['payer_email'];
    $itemnumber = $_POST['item_number'];
    $txnid = $_POST['txn_id'];
    $paymentgross = $_POST['mc_gross'];
    $currencycode = $_POST['mc_currency'];
    $paymentstatus = $_POST['payment_status'];
    $custom = $_POST['custom'];
    $subscrmonth = ($payment_gross/$unitPrice);
    $subscrdays = ($subscr_month*30);
    $subscrdate_from = date("Y-m-d H:i:s");
    $subscrdateto = date("Y-m-d H:i:s", strtotime($subscrdatefrom. ' + '.$subscrdays.' days'));
    
    if(!empty($txn_id)){
        //Check if subscription data exists with the same TXN ID.
        $prevPayment = $db->query("SELECT id FROM usersubscriptions WHERE txnid = '".$txnid."'");
        if($prevPayment->num_rows > 0){
            exit();
        }else{
            //Insert tansaction data into the database
            $insert = $db->query("INSERT INTO usersubscriptions(userid,validity,validfrom,validto,itemnumber,txnid,paymentgross,currencycode,subscrid,paymentstatus,payerwemail) VALUES('".$custom."','".$subscrmonth."','".$subscrdatefrom."','".$subscrdateto."','".$itemnumber."','".$txnid."','".$paymentgross."','".$currencycode."','".$subscrid."','".$paymentstatus."','".$payerwemail."')");
            
            //Update subscription id in users table
            if($insert){
                $subscriptionid = $db->insertid;
                $update = $db->query("UPDATE users SET subscriptionid = {$subscriptionid} WHERE id = {$custom}");
            }
        }
    }
}
die;

 

and in the success i've this

 

<?php
//Include DB configuration file
include 'dbconnect.php';

if(!empty($_GET['item_number']) && !empty($_GET['tx']) && !empty($_GET['amt']) && $_GET['st'] == 'Completed'){
    //get transaction information from query string
    $item_number = $_GET['item_number'];
    $txn_id = $_GET['tx'];
    $payment_gross = $_GET['amt'];
    $currency_code = $_GET['cc'];
    $payment_status = $_GET['st'];
    $custom = $_GET['cm'];
    
    //Check if subscription data exists with the TXN ID
    $prevPaymentResult = $db->query("SELECT * FROM usersubscriptions WHERE txnid = '".$txn_id."'");
    
    if($prevPaymentResult->num_rows > 0){
        //get subscription info from database
        $paymentRow = $prevPaymentResult->fetch_assoc();
        
        //prepare subscription html to display
        $phtml  = '<h5 class="success">Thanks for payment, your payment was successful. Payment details are given below.</h5>';
        $phtml .= '<div class="paymentInfo">';
        $phtml .= '<p>Payment Reference Number: <span>MS'.$paymentRow['id'].'</span></p>';
        $phtml .= '<p>Transaction ID: <span>'.$paymentRow['txn_id'].'</span></p>';
        $phtml .= '<p>Paid Amount: <span>'.$paymentRow['payment_gross'].' '.$paymentRow['currency_code'].'</span></p>';
        $phtml .= '<p>Validity: <span>'.$paymentRow['valid_from'].' to '.$paymentRow['valid_to'].'</span></p>';
        $phtml .= '</div>';
    }else{
        $phtml = '<h5 class="error">Your payment was unsuccessful, please try again.</h5>';
    }
}elseif(!empty($_GET['item_number']) && !empty($_GET['tx']) && !empty($_GET['amt']) && $_GET['st'] != 'Completed'){
    $phtml = '<h5 class="error">Your payment was unsuccessful, please try again.</h5>';
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>PayPal Subscriptions Payment Payment Status</title>
    <meta charset="utf-8">
</head>
<body>
<div class="container">
    <h1>PayPal Subscriptions Payment Status</h1>
    <!-- render subscription details -->
    <?php echo !empty($phtml)?$phtml:''; ?>
</body>
</html>

when i make all the process of purchase all works untill I get to success and nothing is insert into the db so i hope someone help me because this is the only one thing that is missing to finish my project

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.