janwebdev / symfony-omnipay-bundle
Symfony 4.4 / 5.x / 6.x 的 Omnipay 扩展包
Requires
- php: ^7.4|^8.0|^8.1
- ext-json: *
- league/omnipay: ^3
- symfony/cache: ^4.4||^5.4||^6
- symfony/framework-bundle: ^4.4||^5.4||^6
Requires (Dev)
- omnipay/paypal: ^3.0
- omnipay/tests: ^4
- php-http/guzzle7-adapter: ^1.0.0
- phpro/grumphp: ^1.3
- phpstan/phpstan: ^0.12.81
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.5
README
以下扩展包基于 Omnipay 包 及其组件。
支持 Symfony 4.4, 5.x, 6.x 和 PHP 7.4+, 8.0.x, 8.1.x
安装
通过 Composer
$ composer require janwebdev/symfony-omnipay-bundle
确保在 ./config/bundles.php
文件中已启用
// ... Janwebdev\OmnipayBundle\OmnipayBundle::class => ['all' => true],
使用
此扩展包提供了一个名为 OmnipayManager
的新服务。它包含一个名为 get()
的方法,该方法返回一个完全配置好的网关。使用依赖注入来使用此服务
<?php // ... public function makePayment(OmnipayManager $omnipay) { $stripe = $omnipay->get('Stripe'); $paypal = $omnipay->get('PayPal_Express'); // ...
get()
接收支付集成名称作为字符串,就像在 Omnipay 包 中调用时一样。然后您可以使用这些网关像平常一样使用。
注意: 网关是“缓存”的 - 多次调用 get('Some_Gateway')
总是返回相同的对象。
配置
创建配置文件 ./config/packages/omnipay.yaml
或从 示例 中复制粘贴。
可以在该文件中配置网关,例如。
omnipay: gateways: # Your config goes here
例如,要配置 Stripe 和 PayPal Express 网关
omnipay: gateways: Stripe: apiKey: sk_test_BQokikJOvBiI2HlWgH4olfQ2 PayPal_Express: username: test-facilitator_api1.example.com password: 3MPI3VB4NVQ3XSVF signature: 6fB0XmM3ODhbVdfev2hUXL2x7QWxXlb1dERTKhtWaABmpiCK1wtfcWd. testMode: false solutionType: Sole landingPage: Login
注意: 考虑使用参数和/或 ENV 变量而不是直接在 omnipay.yaml
文件中存储凭据。
方法名称应该是您通常会传递给 Omnipay::create()
的任何内容。
配置设置因网关而异 - 有关更多详细信息,请参阅 Omnipay 文档中的 配置网关。
注册自定义网关
可以通过容器通过标记为 omnipay.gateway
来注册自定义网关
# services.yaml services: my.custom.gateway: class: Path\To\CustomGateway tags: - { name: omnipay.gateway, alias: CustomGateway } # omnipay.yaml omnipay: methods: # Reference the gateway alias here CustomGateway: apiKey: pa$$w0rd
然后您可以通过其别名获取完全配置好的网关
// ... private function getCustomGateway(OmnipayManager $omnipay): GatewayInteface { return $omnipay->get('CustomGateway'); } // ...
额外的配置和定制
默认网关
将默认网关密钥添加到您的配置中
# omnipay.yaml omnipay: gateways: MyGateway1: apiKey: pa$$w0rd MyGateway2: apiKey: pa$$w0rd default: MyGateway1
现在您可以获得默认网关实例
$omnipay->getDefaultGateway();
禁用网关
如果您需要禁用网关但想要保留所有配置,请将 disabled
键添加到配置中
# omnipay.yaml omnipay: gateways: MyGateway1: apiKey: pa$$w0rd MyGateway2: apiKey: pa$$w0rd disabled: [ MyGateway1 ]
MyGateway1
网关现在将在网关注册过程中被跳过。
定制 Omnipay 服务
如果您需要特定的网关选择机制或需要一次性获取多个网关,请考虑扩展默认扩展包的 Omnipay 服务。创建您的自定义 App\Omnipay\MyService
类,从基础类扩展它并添加自定义获取器。
例如,您可能希望获取实现某些接口的所有网关。
<?php // App/Omnipay/MyService.php namespace App\Omnipay; use App\Payment\Processing\Gateway\CardSavingInterface; use Janwebdev\OmnipayBundle\Manager\OmnipayManager as BaseOmnipay; use Omnipay\Common\GatewayInterface; class MyService extends BaseOmnipay { /** * @return CardSavingInterface[] */ public function getCardSavingGateways(): array { return array_filter($this->registeredGateways, function (GatewayInterface $gateway) { return $gateway instanceof CardSavingInterface; }); } }
#services.yaml parameters: omnipay.class: App\Omnipay\MyService
现在您应该能够在您的应用程序中获得“卡片保存”感知网关
// ... foreach ($omnipay->getCardSavingGateways() as $gateway) { $gateway->saveCreditCard($creditCard); // assuming saveCreditCard is a part of CardSavingInterface interface } // ...
在注册时初始化网关
默认情况下,网关仅在您调用 get()
方法时才初始化。如果您使用像 getCardSavingGateways
(如上面示例所示)这样的自定义获取器(并在其中使用 $this->registeredGateways
)您可能希望在注册时自动初始化它们。只需添加适当的配置键即可。
# omnipay.yaml omnipay: gateways: MyGateway1: apiKey: @pa$$w0rd# init_on_boot: true
单元测试
$ phpunit
变更日志
请参阅 变更日志 了解最近更改的详细信息。
许可协议
MIT 许可协议 (MIT)。更多信息请参阅 许可文件。