Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Paypal payment failed.

New Here ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

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

Views

971
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

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 ?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

Hi,

I've removed the space and the issue still remains.

I am following their tutorial but no matter what I try I still get the same message.

Thanks

Danyaal,

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 12, 2017 Mar 12, 2017

Copy link to clipboard

Copied

If I was to attach my files. Would someone be able to take a look and give me advice.

Due to my time limit I am unable to learn PHP at this moment.

This is for a school project and neither of my teachers are of any use.

Thanks

Danyaal.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 12, 2017 Mar 12, 2017

Copy link to clipboard

Copied

In other words what you're asking is not "how can I do this?" but rather "who can do this for me?".

Look, if you're paying a university to teach you and they aren't of any use then it would make sense to offer to pay someone to do the work if you're asking for someone to do something for you, right? I'm sure there's people here that are willing to help you learn. There's not many you'll find around here that are willing to just do the work for you. That might be better suited for craigslist or something.

Here's a hint to help you learn something. The condition that determines if a payment was made is conducted at the following line of code:

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

Either one or both of those conditions are not satisfied. To debug try printing those values on your page. That will help you determine what isn't satisfying your conditional requirements for determining if a payment was made.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 12, 2017 Mar 12, 2017

Copy link to clipboard

Copied

As mentoned print out your 2 variables $txn_id and $payment_gross just to see if they contain anything (see in red below). Im still not sure where the information for your variables is coming from.

The INSERT query will only be executed if the variable $txn_id is NOT empty (meaning it needs to contain a value) and $payment_gross is equal to the price column of  id no 1 in your database.

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

The code between these 2 brackets will only be executed if BOTH conditions are met.

}

You could test the Insert query by inserting 'static' values

$txn_id = 123456789;

$payment_gross = 20.00;

Then make sure the price is also 20.00 in your database for id 1.

If the query still doesn't execute then there is something else wrong.

<?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 echo  "<p>$txn_id</p>"; ?>

<?php echo  "<p>$payment_gross</p>"; ?>

<?php

}

?>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 12, 2017 Mar 12, 2017

Copy link to clipboard

Copied

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."')");

  }

  }

  }

}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 12, 2017 Mar 12, 2017

Copy link to clipboard

Copied

LATEST

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

Thanks for the suggestions osgood_.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines