freddyhaddad / paypal-ipn-bundle
一个用于获取和记录 PayPal 即时支付通知 (PayPal IPN) 的 Symfony2 扩展包
Requires
- php: >=5.3.2
- symfony/symfony: >=2.0.12
This package is not auto-updated.
Last update: 2024-09-15 06:34:07 UTC
README
概览
symfony2-paypal-ipn 是一个用于与 PayPal IPN (即时支付通知) 服务交互的 Symfony2 扩展包。该扩展包充当 PayPal IPN 服务的监听器,并使用 Doctrine 将传入的订单记录到数据库中。发送订单确认邮件也很简单(详情请见 [示例 Twig 邮件控制器] twigcontroller)。
symfony2-paypal-ipn 可在 Packagist packagist 和 KnpBundles knpbundles 以及 GitHub 上找到。
描述
symfony2-paypal-ipn 是 [codeigniter-paypal-ipn] codeigniterpaypalipn 的直接移植,这是 Orderly Ltd. 为 CodeIgniter 用户提供的等效库。
这个库(在 Symfony 术语中称为“扩展包”)专注于“付款后”的工作流程,即付款完成后 PayPal 向 IPN 控制器发送即时支付通知调用所需的处理。
这个库处理以下内容:
- 验证 IPN 调用
- 记录 IPN 调用
- 从 IPN 调用中提取订单和行项目信息
- 解释 PayPal 的支付状态
- 将订单和行项目存储在数据库中
- 触发一个事件,以便其他扩展包可以执行其操作
所有预付款功能(例如,将结账信息发布到 PayPal)和自定义付款后工作流程(例如,发送电子邮件)都留给读者作为练习。如果您需要一个更通用的 Symfony2 工具包来处理 PayPal,请参阅 [JMSPaymentPaypalBundle] jmspaymentbundle。
这个库还支持从支付数据传输 (PDT) 获取状态。
依赖关系
Symfony PayPal IPN 扩展包依赖于 Symfony2 symfony2、[Doctrine 2.0] doctrine2.0 和 curl。
还提供了一个使用 Twig twig 模板语言的示例订单确认电子邮件。
安装
1. 从 GitHub 安装
我们将直接将 PayPalIPNBundle 安装到您的 Symfony vendor
目录中
$ cd {{YOUR SYMFONY APP}}/vendor
$ git clone git://github.com/orderly/symfony2-paypal-ipn.git
...
$ mv symfony2-paypal-ipn orderly
现在所有重要的 OrderlyPayPalIpnBundle.php
文件应该已经在这里了
{{YOUR SYMFONY APP}}/vendor/orderly/src/Orderly/PaypalIpnBundle/OrderlyPayPalIpnBundle.php
2. 注册扩展包
现在我们需要注册新的扩展包。编辑您的 AppKernel.php
文件
{{YOUR SYMFONY APP}}/app/AppKernel.php
并将以下行添加到 registerBundles()
方法中 $bundles
数组的末尾
new Orderly\PayPalIpnBundle\OrderlyPayPalIpnBundle(),
现在编辑您的 autoload.php
文件
{{YOUR SYMFONY APP}}/app/autoload.php
并将以下行添加到您的 registerNamespaces()
调用末尾
'Orderly' => __DIR__.'/../vendor/orderly/src',
3. 部署数据库表
使用PayPalIPN Bundle,您可以选择将数据存储在MySQL这样的合理数据库中,或者使用MongoDB。下一节提供了如何使用这两种不同系统的信息。
3.1 使用ORM(MySQL)部署数据库表
如果您打算使用MySQL这样的合理数据库系统,您必须配置驱动程序以使用Doctrine ORM。您可以通过向您的bundle配置中添加以下配置来实现这一点
orderly_pay_pal_ipn: # ... Rest of the configuration drivers: orm: object_manager: doctrine.orm.entity_manager classes: ~
下一步是更新/创建PayPalIpnBundle所需的数据库表。有三种不同的方式部署三个数据库表
使用Symfony控制台
您可以在项目控制台中使用以下命令安装表
$ php app/console doctrine:schema:update --force
请注意,此方法不会复制SQL文件中找到的表字段注释。
手动运行MySQL脚本
或者,您可以在数据库上运行MySQL文件create_mysql_tables.sql
。您可以在以下位置找到文件
Orderly/PayPalIpnBundle/Resources/config
如果您选择此选项,您可能需要在运行之前修改每个表的DEFAULT CHARSET
(当前设置为"utf8")。
3.2 使用ODM(MongoDB)部署数据库表
要使用MongoDB作为数据存储,在激活ODM驱动程序之前,请确保在您的部分中安装了DoctrineMongoDBBundle。有关如何配置bundle的更多信息,请参阅Symfony2网站。
orderly_pay_pal_ipn: #.... drivers: odm: object_manager: doctrine.odm.mongodb.document_manager classes: ~
注意:一个完全配置的示例可以在4.1 使用MongoDB后端
部分找到。
创建数据库表
通过运行以下命令将创建/更新MongoDB集合
$ php app/console doctrine:mongodb:update --force
4. 配置
现在我们需要配置bundle。将以下内容添加到您的Symfony2 YAML配置文件app/config/config.yml
# PaypalIpnBundle Configuration orderly_pay_pal_ipn: # If set to false then service loads settings with "sandbox_" prefix islive: false # Constants for the live environment (default settings in Configuration.php) email: sales@CHANGEME.com url: https://www.paypal.com/cgi-bin/webscr proxy: ~ debug: %kernel.debug% pdttoken: pdt-token # Constants for the sandbox environment (default settings in Configuration.php) sandbox_email: system_CHANGEME_biz@CHANGEME.com sandbox_url: https://www.sandbox.paypal.com/cgi-bin/webscr sandbox_proxy: ~ sandbox_debug: true sandbox_response: VERIFIED sandbox_pdttoken: pdt-token sandbox_pdtresponse: payment_status: Completed mc_gross: 123.00 drivers: orm: object_manager: doctrine.orm.entity_manager classes: ~
确保更新email
和sandbox_email
设置为您自己的PayPal账户。
当islive
设置为false时,可以激活以下配置
sandbox_response
允许您模拟IPN PayPal的响应。如果您不设置其值,您的服务器将连接到PayPal服务器,并很可能返回INVALID。sandbox_pdtresponse
允许您模拟PDT PayPal的响应。如果您不设置其值,您的服务器将连接到PayPal服务器,并很可能返回FAIL。要模拟值,只需传递一个键值对列表,该服务器将返回。
关于debug
设置:如果设置为true,则PayPalIpnBundle会将最后访问的IPN数据(即POST变量)存储到数据库中。然后当您直接访问不带数据的IPN URL时,它将重新加载缓存的数据。因此,它实际上是一个“回放”模式,允许您直接检查validateIPN()
IPN处理程序正在做什么。
4.1 使用MongoDB后端
要使用MongoDB后端使用IPN监听器,将驱动程序部分更改为odm
,并提供您的MongoDB对象管理器的名称。以下是一个基本配置示例。
# PaypalIpnBundle Configuration orderly_pay_pal_ipn: # If set to false then service loads settings with "sandbox_" prefix islive: false # Constants for the live environment (default settings in Configuration.php) email: sales@CHANGEME.com url: https://www.paypal.com/cgi-bin/webscr proxy: ~ debug: %kernel.debug% pdttoken: pdt-token # Constants for the sandbox environment (default settings in Configuration.php) sandbox_email: system_CHANGEME_biz@CHANGEME.com sandbox_url: https://www.sandbox.paypal.com/cgi-bin/webscr sandbox_proxy: ~ sandbox_debug: true sandbox_response: VERIFIED sandbox_pdttoken: pdt-token sandbox_pdtresponse: payment_status: Completed mc_gross: 123.00 drivers: odm: object_manager: doctrine.odm.mongodb.document_manager classes: ~
5. 设置路由(可选但推荐)
要告诉Symfony的路由系统找到我们的示例控制器之一,首先打开以下文件
{{YOUR SYMFONY APP}}/app/config/routing.yml
使用Twig发送订单确认
假设您想使用Twig发送订单确认电子邮件,请添加以下控制器
OrderlyPayPalIpnBundleEmail: resource: "@OrderlyPayPalIpnBundle/Controller/TwigNotificationEmailController.php" type: annotation prefix: /ipn/
您的网站现在将监听在URL上的即时支付通知
http://{{YOUR DOMAIN}}/ipn/ipn-twig-email-notification
请注意,提供的示例电子邮件模板依赖于 Twig 的 [number_format
] numberformat 过滤器,该过滤器于2012年12月添加(您可能需要更新 Twig 版本才能使用此功能)。别忘了告诉 PayPal 您的新 PayPal IPN URL。
仅记录订单但不发送通知
或者,如果您只想在数据库中记录订单(而不发送任何通知),则在控制器中添加以下内容
OrderlyPayPalIpnBundleNoEmail:
resource: "@OrderlyPayPalIpnBundle/Controller/NoNotificationController.php"
type: annotation
prefix: /ipn/
您的网站现在将监听以下 incoming IPN
http://{{YOUR DOMAIN}}/ipn/ipn-no-notification
别忘了告诉 PayPal 您的新 PayPal IPN URL。
免责声明:提供的示例控制器完全是示例。请在将此包投入生产之前,使用自己的业务逻辑更新其中一个或多个示例文件。
接收 PDT 变量
这是您的 PDT 调用的一个示例。
namespace Your\OwnBundle; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class YourPaypalController extends Controller { /** * Your return action from Paypal. */ public function completeAction() { $tx = $this->getRequest()->get('tx'); if (!$tx) { // Redirect to a non-found page return $this->redirect($this->generateUrl('notFound')); } $pdt = $this->get('orderly_pay_pal_pdt'); $pdtArray = $pdt->getPdt($tx); $status = 'unknown'; if (isset($pdtArray['payment_status'])) { $status = $pdtArray['payment_status']; } return $this->render('YourOwnBundle:YourPaypal:complete.html.twig', array('status' => $status) ); } }
8. 订阅事件(如果您需要自定义处理)
收到 IPN 后,如果它是有效的,则会分发一个包含传入 IPN 的事件。然后,您可以订阅一个监听器并执行所需操作。使用类似以下内容更改您的 config.yml
services:
# ...
paypal_im_received:
class: Your\OwnBundle\Event\YourPayPalListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: paypal.ipn.receive, method: onIPNReceive }
现在您只需要编写自定义监听器
namespace Your\OwnBundle\Event;
use Orderly\PayPalIpnBundle\Event\PayPalEvent;
use Doctrine\Common\Persistence\ObjectManager;
class YourPayPalListener {
private $om;
public function __construct(ObjectManager $om) {
$this->om = $om;
}
public function onIPNReceive(PayPalEvent $event) {
$ipn = $event->getIPN();
// do your stuff
}
}
11. 测试和调试
现在是时候进行测试了。首先,请确保 islive
设置为 false,sandbox_debug
设置为 true。
开启调试后,这样测试
- 按照常规流程运行结账过程,例如进行 PayPal 沙盒支付等
- PayPal 将发送 IPN POST 数据到您的新 PayPal IPN URL
- 检查 PayPal IPN 历史记录以确定成功或失败(例如,HTTP 状态代码 500)
- 检查您是否收到了订单确认电子邮件(如果您发送了电子邮件)
- 检查订单和行项目是否存储在您的数据库中
如果您的任何检查有问题,则下一步是手动在浏览器中调用您的 IPN URL,看看会发生什么(例如 PHP 或 Symfony 错误,或者可能是数据库或 Twig 未找到错误)。这是因为第 3 步中解释的调试 "重放" 功能。接下来
- 修复错误
- 重复
22. 完整配置
# PaypalIpnBundle Configuration orderly_pay_pal_ipn: # If set to false then service loads settings with "sandbox_" prefix islive: false # Constants for the live environment (default settings in Configuration.php) email: sales@CHANGEME.com url: https://www.paypal.com/cgi-bin/webscr proxy: ~ debug: %kernel.debug% pdttoken: pdt-token # Constants for the sandbox environment (default settings in Configuration.php) sandbox_email: system_CHANGEME_biz@CHANGEME.com sandbox_url: https://www.sandbox.paypal.com/cgi-bin/webscr sandbox_proxy: ~ sandbox_debug: true sandbox_response: VERIFIED sandbox_pdttoken: pdt-token sandbox_pdtresponse: payment_status: Completed mc_gross: 123.00 drivers: # Define one driver only. orm: object_manager: doctrine.orm.entity_manager classes: ipn_log: Orderly\PayPalIpnBundle\Entity\IpnLog ipn_order_items: Orderly\PayPalIpnBundle\Entity\IpnOrderItems ipn_orders: Orderly\PayPalIpnBundle\Entity\IpnOrders # OR for MongoDB support odm: object_manager: doctrine.odm.mongodb.document_manager classes: ipn_log: Orderly\PayPalIpnBundle\Document\IpnLog ipn_order_items: Orderly\PayPalIpnBundle\Document\IpnOrderItems ipn_orders: Orderly\PayPalIpnBundle\Document\IpnOrders
支持和错误
对于支持请求,请发送电子邮件至 [Orderly Ltd] orderlyemail。如果您认为库中存在错误,或者您希望添加的功能,请提出 [新的 GitHub 问题] newissue。
致谢
此库是从 [Codeigniter PayPal IPN 库] codeigniterpaypalipn 转移的,也是由 Orderly Ltd 发布的。
非常感谢为 symfony2-paypal-ipn 做出贡献的所有人员
- 作者: Kemor kemor
- 贡献者: Strife strife
- 贡献者: [Alex Dean] alexdean
- 贡献者: [Ivo Azirjans] ivoaz
- 贡献者: [Alexander Pirsig] piscis
- 贡献者: [Carles Climent] carlescliment
免责声明和警告
Orderly Ltd 对 Symfony2 PayPal IPN Bundle 的任何处理错误或通过其使用而产生的任何经济损失不承担任何责任。
特别是,此库 不 满足 PayPal IPN 要求
验证实际支付金额是否与您打算收取的金额相符。虽然这并不是IPN问题,但如果您没有对按钮进行加密,其他人可能截获原始传输并更改价格。如果没有这个检查,您可能会接受低于预期的支付金额。
(此验证步骤不在此捆绑包的范围内,因为这需要与您的产品目录集成。)
此外,这个库没有正确处理退款。通常退款会以带有负余额的新订单行存储在ipn_orders
中,但这甚至也不是100%可预测的。
版权
symfony2-paypal-ipn版权所有(c)2012 Orderly Ltd。
许可证
根据Apache许可证第2版(“许可证”);除非遵守许可证,否则不得使用此文件。您可以在以下位置获取许可证副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则根据许可证分发的软件按“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的权限和限制的特定语言,请参阅许可证。