GithubHelp home page GithubHelp logo

colinodell / omnipay-bundle Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 12.0 54 KB

Omnipay bundle for Symfony 2.3+ and 3.0+

License: MIT License

PHP 100.00%
omnipay symfony-bundle symfony symfony2 symfony3 php

omnipay-bundle's Introduction

omnipay-bundle

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

Simple bundle for implementing Omnipay in your Symfony application.

Versions

omnipay-bundle Symfony Omnipay PHP
1.x 2.x or 3.x 2.x 5.4+
2.x 2.x or 3.x 3.x 5.6+

Install

Via Composer

$ composer require colinodell/omnipay-bundle

Enable the bundle in your AppKernel.php:

new ColinODell\OmnipayBundle\OmnipayBundle(),

Usage

This bundle provides a new service called Omnipay. It contains a single method get(), which returns a fully-configured gateway for you to use:

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

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

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

Gateways can be configured in your app/config/config.yml file

omnipay:
    methods:
        # Your config goes here

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

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

NOTE: You should probably consider using parameters instead of storing credentials directly in your config.yml 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 Gateways

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

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

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

Additional configuration and customization

Default gateway

Add default gateway key to your config:

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

    default_gateway: 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_gateways key to the config:

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

    disabled_gateways: [ 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 Omnipay service. Create your custom Omnipay class, extend it from base class and add custom getters. For example, you might want to get all gateways which implement some interface.

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

Now you should be able to get vault-aware gateways in your application:

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

Initialize gateways on registration

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

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

    initialize_gateway_on_registration: true

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

omnipay-bundle's People

Contributors

carlcraig avatar colinodell avatar noud avatar torchello avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

omnipay-bundle's Issues

Add Support for Symfony 4

I'm planning to use this bundle for a new application in Symfony 4 that will develop. However, it doesn't have Symfony 4 support. I could send a PR to update dependencies and deprecated code, but I would like to know if you are planning to support Symfony 4, and what will be, in your view, the best way of doing it (ie, creating a new 2.0 branch that only supports 4, or working on existing branch with compiler passes, etc)

Thanks!

Question: How to get parameters for a custom gateway?

Hi,

Thanks for the great work. Really appreciate it.

I am trying to use this bundle in one of my project as follows:

config.yml

omnipay:
    methods:
        WechatPay_Mweb:
            app_id: '%wechat_app_id%' #app_id
            app_key: '%wechat_app_key%' #app_key
            mch_id: '%wechat_mch_id%' #merchant_id
            notify_url: '%wechat_notify_url%' #notify_url
        Alipay_AopPage:
            appId: '%alipay_app_id%' #app-id
            appKey: '%alipay_app_key%' #appKey
            signType: '%alipay_sign_type%' #signType
            privateKey: '%alipay_private_key%' #privateKey
            alipayPublicKey: '%alipay_public_key%' #publicKey
            notifyUrl: '%alipay_notify_url%' #returnUrl

services.yml

service.pay.alipay:
       class: Omnipay\Alipay\AopPageGateway
       tags:
            - { name: omnipay.gateway, alias: Alipay_AopPage }

service.pay.wechat:
       class: Omnipay\WechatPay\MwebGateway
       tags:
            - { name: omnipay.gateway, alias: WechatPay_Mweb }

service.process.payment:
       class: AppBundle\Service\ProcessPayment
       arguments:
            - '@doctrine_mongodb.odm.default_document_manager'
            - '@omnipay'

PaymentController.php
This controller has a route which determines transaction and paymentType and passes to ProcessPayment Service

$gatewayResponse = $this->processPaymentService->pay($transaction, $paymentType);

ProcessPayment.php

/**
     * ProcessPayment constructor.
     * @param DocumentManager $dm
     * @param Omnipay $omnipay
     */
    public function __construct(
        DocumentManager $dm,
        Omnipay $omnipay,
    ) {
        $this->dm = $dm;
        $this->omnipay = $omnipay;
    }

    /**
     * @param Document\Transaction $transaction
     * @param Document\PaymentType $paymentType
     * @throws \Exception
     * @return array
     */
    public function pay(Document\Transaction $transaction, Document\PaymentType $paymentType)
    {
         $paymentGateway = null;
         switch ($paymentType) {
               case: 'WECHAT': $paymentGateway = $this->omnipay->get('WechatPay_Mweb');
                      break;
               case: 'ALIPAY': $paymentGateway = $this->omnipay->get('Alipay_AopPage');
                      break;
               default:
                      throw new \Exception('Requested payment gateway is not implemented.', 401);
                      break;
         }
        if (null !== $paymentGateway) {
                $params = $paymentGateway->getDefaultParameters();
                dump($params);
                $order = [
                     'app_id'            => '',
                     'mch_id'            => '',
                     'notify_url'        => '',
                     'body'              => 'The test order',
                     'out_trade_no'      => $transaction->getId(),
                     'total_fee'         => 1, //=0.01
                     'spbill_create_ip'  => '127.0.0.1',
                     'fee_type'          => 'CNY'
                ];
                $request = $paymentGateway->purchase($order);
                $response = $request->send();
                dump($response->getData());
        }
    }

Current result:

dump($params) returns [] (An empty array)

Expected result:

Array of parameters configured in config.yml

Can't install the bundle

Hi,

I tried to install this bundle on a fresh Symfony 3.4.12 project but I'm getting a ton of errors.

Here are the errors : https://pastebin.com/crnyVAMi
Here's the project's composer.json : https://pastebin.com/Cmg4hKXn

I tried downgrading doctrine-bundle (to 1.6.X) and it got rid of the first part about doctrine-bundle 1.9.1 but the other errors where still present.

Any idea on how I might solve this issue ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.