提交表单后,将贝宝付款确认书添加到mysql [英] Add paypal payment confirmation to mysql after form is submitted

查看:81
本文介绍了提交表单后,将贝宝付款确认书添加到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的在线html表单.用户填写表格,提交后将信息插入MySQL数据库,然后将其重定向到PayPal以完成付款.问题在于数据库没有有关他们是否实际完成付款的任何信息.

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.

解决方案是使用IPN.我已经在网上搜索了内容,并且可以在YouTube上找到教程,但这太复杂了,我无法实现IPN来确认向MySQL付款.

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.

推荐答案

这不太复杂.

您已经有一个用于测试的沙箱帐户了吗?如果不是,请在 sandbox.paypal.com 上注册卖方/买方测试帐户.不确定两者可能需要不同的电子邮件地址.

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.)在提交表单中,输入IPN脚本的路径,例如pp.notifiy.php

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" />

还要在您的PayPal商家/沙盒帐户设置中,指定监听器的URL. 侦听器脚本不得进行防火墙保护.它将从PayPal收到$ _POST.

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.)付款成功后,

  • Paypal将发布到您的侦听器脚本(例如pp.notifiy.php)
  • ,然后听众将回发后置内容的副本.
  • 您正在检查正文中的字符串VERIFIED以处理订单并更新您的数据库.
  • 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 VERIFIED to process the order and update your db.

我更喜欢cURL解决方案,因为PayPal最近进行了更改,这带来了一些麻烦,使用cURL解决方案对我来说效果很好,特别是在沙盒测试时.需要 cURL扩展名才能在PHP中处于活动状态.

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.

对于cURL解决方案,请阅读有关PayPal Developer的更多信息:如何处理即时付款通知(IPN)消息;另一种解决方案,我不建议使用fsockopen .

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.

这是我正在使用的cURL侦听器脚本(pp.notify.php)的一部分

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
}

这也可以在沙箱上使用.所以最好做一些测试.您应该获取VERIFIED,然后处理订单/更新数据库.如果一切正常,请从沙盒切换到真实的PayPal,最好也至少进行一次测试.

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!

这篇关于提交表单后,将贝宝付款确认书添加到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆