简体   繁体   English

使用数据库插入处理 PayPal IPN

[英]Handling PayPal IPN with Database Insertion

I am hoping to use the PayPal Pro Hosted Solution to handle payments for my website, and what i would like to achieve is that user submitted data is NOT inserted into my database until PayPal confirms i have received payment for their entry.我希望使用 PayPal Pro 托管解决方案来处理我的网站的付款,我想要实现的是用户提交的数据不会插入到我的数据库中,直到 PayPal 确认我已收到他们的条目付款。

From what I've read, i understand the IPN is the best way to achieve this.根据我的阅读,我了解 IPN 是实现这一目标的最佳方式。

So at the moment, users are entering their data with a form, which i am then previewing to them, and if they approve their entry, i am inserting into a database (using PHP/MySQL).所以目前,用户正在使用表单输入他们的数据,然后我正在向他们预览,如果他们批准他们的输入,我将插入数据库(使用 PHP/MySQL)。 The form data at the moment is being passed along in SESSION variables and working fine.目前的表单数据正在 SESSION 变量中传递并且工作正常。 The file process is:文件流程为:

  1. User enters data用户输入数据
  2. User is presented with their entered data on a knew page and if they approve...用户会在已知页面上看到他们输入的数据,如果他们同意...
  3. They click a button which handles the insert into the database.他们单击处理插入数据库的按钮。

However what i would like to do is, if they approve their entry on the preview page, when they click approve, instead of the database being updated there and then, send them to PayPal to make the payment and only update the database with their entry if the payment is approved, like this:但是我想做的是,如果他们在预览页面上批准他们的条目,当他们单击批准时,而不是在那里更新数据库,然后将他们发送到 PayPal 进行付款,并且只用他们的条目更新数据库如果付款被批准,像这样:

  1. User enters data用户输入数据
  2. User is presented with their entered data on a knew page and if they approve...用户会在已知页面上看到他们输入的数据,如果他们同意...
  3. They click a button which takes them to the payment page他们点击一个按钮,将他们带到付款页面
  4. If payment is received, their data is added to the database.如果收到付款,他们的数据将被添加到数据库中。

Does anyone have any experience of this type of approach point me in the right direction or give me some guidance on how to go about this please?有没有人有这种方法的经验,可以为我指明正确的方向,或者给我一些关于如何 go 的指导?

I have looked over the PayPal documentation but because I'm new to this, i need things explained in a pretty simple manner.我已经查看了 PayPal 文档,但是因为我是新手,所以我需要以一种非常简单的方式解释事情。

My original idea was just to store the form is SESSION variables but i will lose this by redirecting people to the payment page.我最初的想法只是将表单存储为 SESSION 变量,但我会通过将人们重定向到支付页面来丢失它。 Another thought i had was to create an identical database to what i already have as a temporary holding stage for data, then if the IPN comes back approved, move the data to the final hosting database, but this seems like over engineering the problem a bit.我的另一个想法是创建一个与我已经拥有的数据库相同的数据库作为数据的临时保存阶段,然后如果 IPN 重新获得批准,则将数据移动到最终的托管数据库,但这似乎有点过度设计问题.

I hope someone can help.我希望有人能帮帮忙。

Thanks Dan谢谢丹

Using PayPal IPN seems to be the best solution in this case.在这种情况下,使用 PayPal IPN 似乎是最好的解决方案。

In my opinion, using temporary table seems to be the best solution.在我看来,使用临时表似乎是最好的解决方案。 It'll be following KISS rule.它将遵循 KISS 规则。

Please consider using following scenario:请考虑使用以下场景:

  • user enters the data用户输入数据
  • the form is being submitted正在提交表单
  • data is stored in temporary table in database数据存储在数据库的临时表中
  • while redirecting to PayPal website you can add custom field that will be used to identify user when we be back on your page在重定向到 PayPal 网站时,您可以添加自定义字段,当我们返回您的页面时,该字段将用于识别用户
  • update transaction status更新交易状态
  • insert data in the table of your needs在您需要的表中插入数据

It seems to be the simplest solution.这似乎是最简单的解决方案。

My original idea was just to store the form is SESSION variables but i will lose this by redirecting people to the payment page.我最初的想法只是将表单存储为 SESSION 变量,但我会通过将人们重定向到支付页面来丢失它。

Not necessarily.不必要。 Sessions can generally persist for as long as the current browser (session) is open.只要当前浏览器(会话)处于打开状态,会话通常可以持续存在。 This is not the same as "as long as the current page is viewed" provided you set the session cookie correctly.“只要当前页面被查看”不同,前提是您正确设置了 session cookie。 You can if you do it right have the sessions persist for days, months, years...如果你做得对,你可以让会话持续数天、数月、数年......

Another thought i had was to create an identical database to what i already have as a temporary holding stage for data, then if the IPN comes back approved, move the data to the final hosting database, but this seems like over engineering the problem a bit.我的另一个想法是创建一个与我已经拥有的数据库相同的数据库作为数据的临时保存阶段,然后如果 IPN 重新获得批准,则将数据移动到最终的托管数据库,但这似乎有点过度设计问题.

No this is not overkill.不,这不是矫枉过正。

It deals with the situation where a transaction is not completed.它处理事务未完成的情况。 This could occur for a number of reasons, for example your user goes to lunch and forgets to complete the process before the session times out (the default is 20 or so minutes) or where there is a problem with the Paypal end (unlikely but you have to presume it can occur) or where there is a general network issue (isp goes down mid transaction), or where your mobile users goes out of network coverage.发生这种情况的原因有很多,例如您的用户去吃午饭并忘记在 session 超时(默认为 20 分钟左右)之前完成该过程,或者 Paypal 端出现问题(不太可能,但您必须假设它可能会发生)或存在一般网络问题(isp 在交易过程中出现故障),或者您的移动用户超出网络覆盖范围。 Anything can disturb a transaction and you need to have a fall-back position.任何事情都可能干扰交易,您需要有一个备用 position。 Otherwise it becomes annoying for you (because you don't know anything about what interrupted the transaction and at what point) and for your user who has to start over again.否则它会变得烦人(因为您不知道什么中断了事务以及在什么时候中断)以及您必须重新开始的用户。

Having a temporary database allows you to monitor incomplete transactions and if necessary prompting the user to complete if they do not do so within a given period of time.拥有一个临时数据库可以让您监控未完成的事务,并在必要时提示用户在给定时间段内未完成事务。

One matter to recognize regarding IPN is that it is an 'Asynchronous' response from PayPal - it is not in the user's browser session, so session variables will not work if you are relying exclusively on IPN (other than if you receive the IPN response and then match it to the user's session). One matter to recognize regarding IPN is that it is an 'Asynchronous' response from PayPal - it is not in the user's browser session, so session variables will not work if you are relying exclusively on IPN (other than if you receive the IPN response and然后将其与用户的会话匹配)。 PayPal also offers PDT (Payment Data Transfer) which is an 'in-session' response which could return the user to your site. PayPal 还提供 PDT(支付数据传输),这是一种“会话中”响应,可以将用户返回到您的站点。

I would not rely exclusively on IPN for payment notifications (see my answer in the following SO topic) Can one rely on Paypal IPN solely to record purchases?我不会完全依赖 IPN 进行付款通知(请参阅我在以下 SO 主题中的回答) 可以仅依赖 Paypal IPN 来记录购买吗? . .

Our system uses a combination of both IPN and PDT, with the 'cart' data stored in a DB (as your 'temporary' record) until notification of the completed payment by either PDT or IPN - whichever arrives first which completes the transaction (your 'permanent' database insertion) and deletes the 'temporary' record (so a subsequent IPN or PDT does not trigger a duplicate transaction).我们的系统使用 IPN 和 PDT 的组合,将“购物车”数据存储在数据库中(作为您的“临时”记录),直到 PDT 或 IPN 通知完成付款 - 以先到者为准(您的“永久”数据库插入)并删除“临时”记录(因此后续 IPN 或 PDT 不会触发重复事务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM