rickmuca/paypal-ipn-bundle

一个用于检索和记录 PayPal 即时支付通知(PayPal IPN)的 Symfony2 扩展包

安装: 5

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 45

类型:symfony-bundle

dev-master 2013-08-13 07:37 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:51:15 UTC


README

概览

symfony2-paypal-ipn 是一个用于与 PayPal IPN(即时支付通知)服务交互的 Symfony2 扩展包。该扩展包充当 PayPal IPN 服务的监听器,并使用 Doctrine 将接收到的订单记录到您的数据库中。发送订单确认邮件也很简单(请参阅 [示例 Twig 邮件控制器] twigcontroller 以获取详细信息)。

symfony2-paypal-ipn 可在 Packagist packagistKnpBundles knpbundles 以及 GitHub 上找到。

描述

symfony2-paypal-ipn 是 [codeigniter-paypal-ipn] codeigniterpaypalipn 的直接移植,一个为 CodeIgniter 用户提供的等效库,也由 Orderly Ltd. 提供。

此库(在 Symfony 术语中称为 "bundle")专注于 "付款后" 工作流程,即一旦付款完成,PayPal 向 IPN 控制器发送即时支付通知调用所需的处理。

此库处理

  1. 验证 IPN 调用
  2. 记录 IPN 调用
  3. 从 IPN 调用中提取订单和项目信息
  4. 解释 PayPal 的支付状态
  5. 将订单和项目存储到数据库中
  6. 触发一个事件,以便其他扩展包可以执行其操作

所有预付款功能(例如,将结账信息发布到 PayPal)和自定义付款后工作流程(例如,发送电子邮件)都留给读者作为练习。如果您需要一个更通用的 Symfony2 工具包来处理 PayPal,请参阅 [JMSPaymentPaypalBundle] jmspaymentbundle

依赖项

Symfony PayPal IPN 扩展包依赖于 Symfony2 symfony2 和 [Doctrine 2.0] doctrine2.0

还提供了一个使用 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 扩展包,您可以选择将数据存储在合理的数据库(如 MySQL)中,或者使用 MongoDB。下一节提供了关于如何使用这两个不同系统的信息。

3.1 使用 ORM (MySQL) 部署数据库表

如果您打算使用MySQL等理性数据库系统,您必须配置驱动程序以使用Doctrine ORM。您可以通过将以下配置添加到您的包配置中来实现这一点

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脚本

或者,您可以对数据库运行create_mysql_tables.sql MySQL文件。您可以在以下位置找到该文件

Orderly/PayPalIpnBundle/Resources/config

如果您选择此选项,您可能希望在运行之前修改每个表的DEFAULT CHARSET(目前设置为“utf8”)。

3.2 使用ODM(MongoDB)部署数据库表

要使用MongoDB作为数据存储,在激活ODM驱动程序之前,请确保在您的部分中安装了DoctrineMongoDBBundle。有关如何配置该包的更多信息,请参阅Symfony2网站

orderly_pay_pal_ipn:
    #....
    drivers:
        odm:
            object_manager: doctrine.odm.mongodb.document_manager
            classes: ~

注意:一个完整的配置示例可以在4.1 使用MongoDB后端下找到。

创建数据库表

通过运行以下命令,您将创建/更新MonogoDB集合

$ php app/console doctrine:mongodb:update --force

4. 配置

现在我们需要配置该包。将以下内容添加到您的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
    debug:   %kernel.debug%

    # 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_debug:   true

    drivers:
        orm:
            object_manager: doctrine.orm.entity_manager
            classes: ~

确保更新emailsandbox_email设置为您自己的PayPal账户。

关于debug设置的说明:如果设置为true,则PayPalIpnBundle会将包含IPN数据的最后一个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
    debug:   %kernel.debug%

    # 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_debug:   true

    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/

您的站点现在将监听在上的传入IPN

http://{{YOUR DOMAIN}}/ipn/ipn-no-notification

别忘了告诉PayPal您的新的PayPal IPN URL。

免责声明:提供的示例控制器正是如此 - 示例。在将此包投入生产之前,请用您自己的业务逻辑更新其中一个或多个示例文件。

6. 订阅事件(如果需要自定义处理)

收到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
    }
}

7. 测试和故障排除

现在是时候进行测试了。首先,确保将islive设置为false,将sandbox_debug设置为true。

一旦启用调试,可以这样测试

  • 像往常一样完成您的结账流程,进行PayPal沙盒支付等
  • PayPal会将IPN POST数据发送到您的新PayPal IPN URL
  • 检查PayPal IPN历史记录以查看成功或失败(例如HTTP状态代码500)
  • 检查您是否收到了订单确认邮件(如果您发送了邮件的话)
  • 检查订单和行项目是否已存储在您的数据库中

如果您在检查过程中遇到任何问题,则下一步是手动在浏览器中调用您的IPN URL以查看发生了什么(例如PHP或Symfony错误,或者可能是数据库或Twig未找到错误)。这要归功于上文第3步中解释的调试“回放”功能。接下来

  • 修复错误
  • 重复

8. 完整配置

# 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
    debug:   %kernel.debug%

    # 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_debug:   true

    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做出贡献的人

免责声明和警告

Orderly Ltd不对通过使用Symfony2 PayPal IPN Bundle产生的任何处理错误或由此产生的任何经济损失承担责任。

特别是,这个库不满足PayPal IPN的要求来

验证支付金额实际上是否与您打算收取的金额相符。虽然这不是技术上的IPN问题,但如果您不加密按钮,有人可能会截获原始传输并更改价格。如果没有这个检查,您可能会接受低于预期金额的支付。

(这个验证步骤超出了此包的范围,因为这需要与您的产品目录集成。)

此外,此库未能妥善处理退款。通常退款会存储在ipn_orders中的新订单行,余额为负,但即使这样也不是100%可预测的。

版权

symfony2-paypal-ipn版权所有(c)2012 Orderly Ltd。

许可证

根据Apache License,版本2.0(“许可证”)许可;除非您遵守许可证,否则不得使用此文件。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律或书面同意,否则在许可证下分发的软件按照“现状”分发,不提供任何形式(无论是明示的还是暗示的)的保证或条件。有关许可证的特定语言,请参阅许可证中的规定权限和限制。