问题
I have a very simple online html form . The user fills in a form, and when they submit, it inserts the info into a MySQL database, then redirects them to PayPal to complete the payment. The problem is that the database does not have any information about whether they actually completed the payment.
The solution to this was to use IPN. I have search online and you-tube for tutorials but it was too complicated and i couldn't achieve the IPN to confirm payment into MySQL.
Kindly I ask if anyone can help me put this together.
回答1:
It's not too complicated.
You got a sandbox account for testing already? If not, signup for seller/buyer test-account at sandbox.paypal.com. Maybe need different e-mail addresses for both, not sure.
1.) In your submit form enter the path to your IPN script e.g. pp.notifiy.php
<input type="hidden" name="notify_url" value="http://my.test/pp.notifiy.php" />
Also in your PayPal merchant/sandbox account settings, specify the URL of your listener. The listener script must not be firewalled. It will receive $_POST from PayPal.
2.) After successful payment,
- Paypal will post to your listener script (e.g. pp.notifiy.php)
- and the listener would post a duplicate of the post-stuff back.
- You are checking the body for the string
VERIFIEDto process the order and update your db.
I prefer the cURL solution, as PayPal had changes recently, which caused some troubles, with the cURL solution it worked fine for me especially when sandbox testing. Need cURL extension to be active in PHP.
For the cURL solution, read more on PayPal Developer: How To Process Instant Payment Notification (IPN) Messages; the other solution, which I'd NOT recommend uses fsockopen.
Here are parts of my working cURL listener script (pp.notify.php)
#$pp_url = "https://www.paypal.com/cgi-bin/webscr";
$pp_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
// build the request / postback
$req = 'cmd=_notify-validate';
foreach($_POST AS $k => $v) {
$v=urlencode(stripslashes($v));
$req.="&$k=$v";
}
// post back to PayPal system to validate using curl
$ch = curl_init($pp_url);
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;
}
$res = trim($res);
curl_close($ch);
// inspect IPN validation result and act accordingly
if(strcmp($res,"VERIFIED")==0)
{
// The IPN is verified
// update your db/process order...
} else if (strcmp($res,"INVALID")==0)
{
// IPN invalid, log for manual investigation
}
This would work on the sandbox too. So best to do a couple of tests. You should get the VERIFIED and then process the order/update db. When all works fine, switch from sandbox to real PayPal and best to test that too at least once.
I hope this helps for getting started!
来源:https://stackoverflow.com/questions/22584462/add-paypal-payment-confirmation-to-mysql-after-form-is-submitted