定制 janwebdev/symfony-omnipay-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

janwebdev/symfony-omnipay-bundle

Composer 安装命令:

composer require janwebdev/symfony-omnipay-bundle

包简介

Omnipay bundle for Symfony 4.4 / 5.x / 6.x

README 文档

README

The following bundle is based on Omnipay package and its components.
It supports Symfony 4.4, 5.x, 6.x and PHP 7.4+, 8.0.x, 8.1.x

Unit Tests Latest Stable Version Total Downloads Latest Unstable Version License

Installation

via Composer

$ composer require janwebdev/symfony-omnipay-bundle

Make sure it is enabled in ./config/bundles.php file:

// ...
Janwebdev\OmnipayBundle\OmnipayBundle::class => ['all' => true],

Usage

This bundle provides a new service called OmnipayManager. It contains a single method get(), which returns a fully-configured gateway. Use dependency injection to use service:

<?php

// ...

public function makePayment(OmnipayManager $omnipay)
{
    $stripe = $omnipay->get('Stripe');
    $paypal = $omnipay->get('PayPal_Express');
// ...

get() receives payment integration name as string, as it is called in Omnipay package You can then use these gateways like usual.

Note: Gateways are "cached" - calling get('Some_Gateway') multiple times will always return the same object.

Configuration

Create config file ./config/packages/omnipay.yaml or copy-paste from example.
Gateways can be configured in this file, i.e.:

omnipay:
    gateways:
        # Your config goes here

For example, to configure the Stripe and PayPal Express gateways:

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

NOTE: Consider using parameters and/or ENV variables instead of storing credentials directly in your omnipay.yaml file like that.

The method names should be whatever you'd typically pass into Omnipay::create().
The configuration settings vary per gateway - see Configuring Gateways in the Omnipay documentation for more details.

Registering Custom Gateway

Custom gateways can be registered via the container by tagging them with 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

You can then obtain the fully-configured gateway by its alias:

// ...
private function getCustomGateway(OmnipayManager $omnipay): GatewayInteface
{
    return $omnipay->get('CustomGateway');
}
// ...

Additional configuration and customization

Default gateway

Add default gateway key to your config:

# omnipay.yaml
omnipay:
    gateways:
        MyGateway1:
            apiKey: pa$$w0rd
        MyGateway2:
            apiKey: pa$$w0rd

    default: MyGateway1

You can now get default gateway instance:

$omnipay->getDefaultGateway();

Disabling gateways

If need to disable a gateway but want to keep all the configuration add disabled key to the config:

# omnipay.yaml
omnipay:
    gateways:
        MyGateway1:
            apiKey: pa$$w0rd
        MyGateway2:
            apiKey: pa$$w0rd

    disabled: [ MyGateway1 ]

MyGateway1 gateway will be skipped during gateway registration now.

Customizing Omnipay service

If you need specific gateway selection mechanism or need to get multiple gateways at once consider to extend default bundle's Omnipay service. Create your custom App\Omnipay\MyService class, extend it from base class and add custom getters.
For example, you might want to get all gateways which implement some interface.

<?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

Now you should be able to get "card-saving"-aware gateways in your application:

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

Initialize gateways on registration

By default gateway is initialized only when you call get() method. If you use custom getters (like getCardSavingGateways from example above) with $this->registeredGateways inside you might want to initialize them automatically during registration. Simply add appropriate config key:

# omnipay.yaml
omnipay:
    gateways:
        MyGateway1:
            apiKey: @pa$$w0rd#

    init_on_boot: true

Unit tests

$ phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

janwebdev/symfony-omnipay-bundle 适用场景与选型建议

janwebdev/symfony-omnipay-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「php」 「payment」 「bundle」 「gateway」 「credit card」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 janwebdev/symfony-omnipay-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 janwebdev/symfony-omnipay-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 janwebdev/symfony-omnipay-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-06