Skip to main content
danyaalk74530408
Known Participant
March 10, 2017
Question

Paypal payment failed.

  • March 10, 2017
  • 1 reply
  • 1134 views

Hi,

First of all, I am new to coding and do not know much about PHP and MySQL .

I am trying to implement PayPal transactions into my project but everytime I complete the checkout and I am redirected to my success.php file I get the message 'Your payment has failed'.

I am not sure how to get the script working.

<?php

include_once("config.php");

//Store transaction information into database from PayPal

$item_number = isset($_POST['item_number']) ? $_GET['item_number'] : null;

$txn_id = isset($_POST['tx']) ? $_GET['tx'] : null;

$payment_gross = isset($_POST['amt']) ? $_GET['amt'] : null;

$currency_code = isset($_POST['cc']) ? $_GET['cc'] : null;

$payment_status = isset($_POST['st']) ? $_GET['st'] : null;

//Get product price to store into database

$sql= "SELECT * price FROM `products` WHERE category_id = '1'";

  $resultset = mysqli_query($db, $sql);

$row = mysqli_fetch_assoc($resultset);

if(!empty($txn_id) && $payment_gross == $row['price']){

    //Insert tansaction data into the database

    mysqli_query($db, "INSERT INTO payments(item_number,txn_id,payment_gross,currency_code,payment_status) VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$payment_status."')");

  $last_insert_id = mysqli_insert_id($db);   

?>

  <h1>Your payment has been successful.</h1>

    <h1>Your Payment ID - <?php echo $last_insert_id; ?>.</h1>

<?php

}else{

?>

  <h1>Your payment has failed.</h1>

<?php

}

?>

I appreciate any help/advice you can give.

Thanks

Danyaal

    This topic has been closed for replies.

    1 reply

    Participating Frequently
    March 10, 2017

    The code you've posted has a space in payment_status:

    VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$pay ment_status."')");

    Are you following the steps outlined at codexworld: PayPal Standard Payment Gateway Integration in PHP - CodexWorld ?

    Inspiring
    March 11, 2017

    servitor  wrote

    The code you've posted has a space in payment_status:

    VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$pay ment_status."')");

    That's not why it's saying payment failed. It's saying payment failed because the condition that determines that a payment was made is not met. OP has a bigger issue than fixing this simple problem. Looking at the big picture it would be better to inquire resources of learning php as a whole, because with a full grasp of how php works solving this issue is simply applying your knowledge of php.

    danyaalk74530408
    Known Participant
    March 12, 2017

    Hi,

    Thanks for the useful reply.

    I actually got my script working now but there is a problem with my ipn file. Once a payment is successful PayPal's IPN doesn't update  my MySQL database.

    Is there an error in my coding?

    <?php

    //Include DB configuration file

    include 'dbConfig.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://www.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(trim($res), "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) {

      //Payment data

      $txn_id = $_POST['txn_id'];

      $payment_gross = $_POST['mc_gross'];

      $currency_code = $_POST['mc_currency'];

      $payment_status = $_POST['payment_status'];

      $payer_email = $_POST['payer_email'];

      //Check if payment data exists with the same TXN ID.

      $prevPayment = $db->query("SELECT payment_id FROM payments WHERE txn_id = '".$txn_id."'");

      if($prevPayment->num_rows > 0){

      exit();

      }else{

      //Insert tansaction data into the database

      $insertPayment = $db->query("INSERT INTO payments(txn_id,payment_gross,currency_code,payment_status,payer_email) VALUES('".$txn_id."','".$payment_gross."','".$currency_code."','".$payment_status."','".$payer_email."')");

      if($insertPayment){

      //Insert order items into the database

      $payment_id = $db->insert_id;

      $num_cart_items = $_POST['num_cart_items'];

      for($i=1;$i<=$num_cart_items;$i++){

      $order_item_number = $_POST['item_number'.$i];

      $order_item_quantity = $_POST['quantity'.$i];

      $order_item_gross_amount = $_POST['mc_gross_'.$i];

      $insertOrderItem = $db->query("INSERT INTO order_items(payment_id,item_number,quantity,gross_amount) VALUES('".$payment_id."','".$order_item_number."','".$order_item_quantity."','".$order_item_gross_amount."')");

      }

      }

      }

    }


    I was able to fix this problem my self and my system is working fine.

    Thanks for the suggestions osgood_.