colinodell / omnipay-bundle
Symfony 2.3+ 和 3.0+ 的 Omnipay 包
Requires
- php: >=5.6
- omnipay/common: ^3.0
- symfony/framework-bundle: ^2.3||^3.0
Requires (Dev)
- omnipay/paypal: ^3.0
- php-http/guzzle6-adapter: ^1.1.1
- phpunit/phpunit: ^3.7|^4.8
This package is auto-updated.
Last update: 2022-03-26 06:36:02 UTC
README
用于在您的 Symfony 应用程序中实现 Omnipay 的简单包。
版本
omnipay-bundle | Symfony | Omnipay | PHP |
---|---|---|---|
1.x | 2.x 或 3.x | 2.x | 5.4+ |
2.x | 2.x 或 3.x | 3.x | 5.6+ |
安装
通过 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
例如,要配置 Stripe 和 PayPal 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)。请参阅 许可文件 获取更多信息。