Reputation: 67
I have followed the pay pal IPN documentation on this. Created a file called payment_notify.php its identical to whats found here : https://developer.paypal.com/docs/classic/ipn/ht_ipn/
My pay pal form is set to POST data to : https://www.sandbox.paypal.com/cgi-bin/webscr I have my pay pal sandbox business account notify url set to my website url / payment_notify.php.
My code allows users to click the buy now button, it brings them to the paypal login screen, allows them to 'pay' for the product, displays a paypal screen saying the transaction was successful then finally when it returns to my site it gives me back nothing. I have tried inserting the POST variables from paypal into my database but nothing happens.
Im a total beginner at paypal API but heres my attempt :
<form action="<?php echo $paypal_url; ?>" method="post">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
<!-- Specify details about the item that buyers will purchase. -->
<input type="hidden" name="item_number" value="<?php echo $row['id'];?>">
<input type="hidden" name="item_name" value="<?php echo $row['credit_price'].": credits"; ?>">
<input type="hidden" name="amount" value="<?php echo $row['credit_price']; ?>">
<input type="hidden" name="currency_code" value="<?echo $row['currency'];?>">
<!-- Specify URLs -->
<input type='hidden' name='cancel_return' value="<? echo 'http://www.$_SERVER[HTTP_HOST]/cancel.php';?>">
<input type='hidden' name='return' value="<? echo 'http://www.$_SERVER[HTTP_HOST]/success.php';?>">
<input type='hidden' name='notify_url' value="<? echo 'http://www.$_SERVER[HTTP_HOST]/payment_notify.php';?>">
<!-- Specify a Buy Now button. -->
<input type="hidden" name="cmd" value="_xclick">
<input type="image" name="submit" border="0"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>
Then for my payment_notify file :
<?php
session_start();
require("db_connection.php");
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$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 IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$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";
}
// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
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'));
if ( !($res = curl_exec($ch)) ) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);
// inspect IPN validation result and act accordingly
if (strcmp ($res, "VERIFIED") == 0) {
// The IPN is verified, process it:
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
foreach($_POST as $key => $value) {
echo $key . " = " . $value . "<br>";
}
// insert the valid transaction into the database :
$insert = "INSERT INTO tableNAME (item,txn_id,payment_gross,currency_code,paid_by,payment_status)VALUES('$item_number',$txn_id,'$payment_amount','$payment_currency','$payer_email','$payment_status')";
$result=mysqli_query($con,$insert);
} else if (strcmp ($res, "INVALID") == 0) {
// IPN invalid, log for manual investigation
echo "The response from IPN was: <b>" .$res ."</b>";
}
?>
Upvotes: 1
Views: 274
Reputation: 361
You have to check two things here:
$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
to
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
Upvotes: 0