maciejmiara/omnipay-bundle

此软件包已被弃用且不再维护。作者建议使用 colinodell/omnipay-bundle 软件包。

Symfony 的 Omnipay 扩展包

0.3.1 2016-08-22 13:29 UTC

README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

这是一个用于在您的 Symfony 应用程序中实现 Omnipay 的简单扩展包。

安装

通过 Composer

$ composer require colinodell/omnipay-bundle

在您的 AppKernel.php 中启用扩展包

new ColinODell\OmnipayBundle\OmnipayBundle(),

使用方法

此扩展包提供了一个名为 Omnipay 的新服务。它包含一个名为 get() 的单方法,该方法返回一个完全配置的网关供您使用

$stripe = $this->get('omnipay')->get('Stripe');

$paypal = $this->get('omnipay')->get('PayPal_Express');

您可以使用这些网关像平常一样。

注意: 网关被“缓存”——多次调用 get('Some_Gateway') 将始终返回相同的对象。

配置

可以在您的 app/config/config.yml 文件中配置网关

omnipay:
    methods:
        # Your config goes here

例如,要配置 StripePayPal Express 网关

omnipay:
    methods:
        Stripe:
            apiKey: sk_test_BQokikJOvBiI2HlWgH4olfQ2

        PayPal_Express:
            username:     test-facilitator_api1.example.com
            password:     3MPI3VB4NVQ3XSVF
            signature:    6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd.
            testMode:     false
            solutionType: Sole
            landingPage:  Login

注意: 您可能需要考虑使用参数而不是直接在 config.yml 中存储凭据。

方法名称应该是您通常传递给 Omnipay::create() 的任何名称。配置设置因网关而异——请参阅 Omnipay 文档中的 配置网关 以获取更多详细信息。

注册自定义网关

可以通过将 omnipay.gateway 标签添加到容器中来注册自定义网关

# services.yml
services:
    my.test.gateway:
        class: Path\To\MyTestGateway
        tags:
            - { name: omnipay.gateway, alias: MyTest }

# config.yml
omnipay:
    methods:
        # Reference the gateway alias here
        MyTest:
            apiKey: abcd1234!@#

然后您可以通过其别名字符串获取完全配置的网关

$this->get('omnipay')->get('MyTest');

附加配置和自定义

默认网关

将默认网关密钥添加到您的配置中

# config.yml
omnipay:
    methods:
        MyGateway1:
            apiKey: abcd1234!@#
        MyGateway2:
            apiKey: abcd45678!@#

    default_gateway: MyGateway1

现在您可以获得默认网关实例

$omnipay->getDefaultGateway();

禁用网关

如果您需要禁用网关但想保留所有配置,请将 disabled_gateways 键添加到配置中

# config.yml
omnipay:
    methods:
        MyGateway1:
            apiKey: abcd1234!@#
        MyGateway2:
            apiKey: abcd45678!@#

    disabled_gateways: [ MyGateway1 ]

现在将跳过在网关注册期间 MyGateway1 网关。

自定义 Omnipay 服务

如果您需要特定的网关选择机制或需要一次性获取多个网关,请考虑扩展默认的 Omnipay 服务。创建您自己的 Omnipay 类,从基础类扩展,并添加自定义获取器。例如,您可能希望获取实现某些接口的所有网关。

<?php

// AppBundle/Omnipay/Omnipay.php

namespace AppBundle\Omnipay;

use AppBundle\Payment\Processing\Gateway\VaultAwareGateway;
use ColinODell\OmnipayBundle\Service\Omnipay as BaseOmnipay;
use Omnipay\Common\GatewayInterface;

class Omnipay extends BaseOmnipay
{
    /**
     * @return VaultAwareGateway[]
     */
    public function getVaultAwareGateways()
    {
        return array_filter($this->registeredGateways, function (GatewayInterface $gateway) {
            return $gateway instanceof VaultAwareGateway;
        });
    }
}
#services.yml
parameters:
    omnipay.class: AppBundle\Omnipay\Omnipay

现在您应该能够在您的应用程序中获取感知保险库的网关

foreach ($omnipay->getVaultAwareGateways() as $gateway) {
    $gateway->saveCreditCard($creditCard); // assuming saveCreditCard is a part of VaultAwareGateway interface
}

在注册时初始化网关

默认情况下,网关仅在您调用 get() 方法时初始化。如果您使用自定义获取器(如上例中的 getVaultAwareGateways),并在 $this->registeredGateways 内部使用,则可能在注册时自动初始化它们。只需添加适当的配置键即可。

# config.yml
omnipay:
    methods:
        MyGateway1:
            apiKey: abcd1234!@#

    initialize_gateway_on_registration: true

测试

$ phpunit

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件 colinodell@gmail.com 联系,而不是使用问题跟踪器。

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件