oc-shopaholic / oc-omnipay-shopaholic-plugin

💳 Payment gaytways extension for Shopaholic plugin
GNU General Public License v3.0
13 stars 6 forks source link

Added support for omnipay v3 #19

Closed SebastiaanKloos closed 3 years ago

SebastiaanKloos commented 4 years ago

I made a start at supporting omnipay 3 with this pull request. There are no big changes. I've only tested it with the omnipay/mollie: ^5.2 gateway.

The only issues I've found right now are the following:

In your plugin you need to register the gateway manually. For example:

public function boot()
{
    $factory = Omnipay::getFactory();
    $factory->register('Mollie');
}
kharanenka commented 4 years ago

Thanks for your contribution. I'll check with example of PayPal plugin later

vdomah commented 3 years ago

Checked with my Stripe for Shopaholic plugin. Works perfect!

kharanenka commented 3 years ago

Thanks for your contribution

bcwaretx commented 1 year ago

Building from this we just implemented an extension that restores the prior function from 2.x that will register all gateways that are found installed. The caveat is that we had to hardcode the "Supported Gateways" list. This is still hot fresh code, use at your own risk, and I welcome any comments! In our custom Shopaholic class boot() method we have: Event::subscribe(ExtendPaymentMethods::class);

ExtendPaymentMethods class:

<?php namespace ournamespace\Shopaholic\Classes\Event;

use Event;
use Omnipay\Omnipay;
use Lovata\OrdersShopaholic\Models\PaymentMethod;
use Lovata\OmnipayShopaholic\Classes\Event\PaymentMethodModelHandler;

/**
 * Class ExtendPaymentMethods
 * Restores feature from Omnipay 2.x that will register all available gateways
 */
class ExtendPaymentMethods 
{
    /**
     * Add listeners
     * @param \Illuminate\Events\Dispatcher $obEvent
     */
    public function subscribe($obEvent)
    {
        PaymentMethod::extend(function ($obElement) {
            /** @var PaymentMethod $obElement */

            // Register the available gateways since Omnipay no longer supports this directly
            $this->registerOmnipayGateways();
        });
        // Call this again to register Payment Gateways now that Omnipay Gateways are registered
        Event::subscribe(PaymentMethodModelHandler::class);
    }

    public function registerOmnipayGateways()
    {
        $gateways = $this->gatewayFactoryfind();
    }

    /**
     * Automatically find and register all officially supported gateways
     * Based on 2.x GatewayFactory->find()
     * After calling this, GatewayFactory->all() in 3.x will function as expected from find() in 2.x
     * 
     * @return array An array of gateway names
     */
    public function gatewayFactoryfind()
    {
        $factory = Omnipay::getFactory();

        if(count($factory->all())) {
            // There are already gateways registered, so either this has run or some have been manually registered
            // For our application we do not run the auto-registration method
        } else {
            foreach ($this->getSupportedGateways() as $gateway) {
                $class = \Omnipay\Common\Helper::getGatewayClassName($gateway);
                if (class_exists($class)) {
                    $factory->register($gateway);
                }
            }
        }
        return $factory->all();
    }

    /**
     * Simulates getSupportedGateways() method from Omnipay 2.x
     * Added new AuthorizeNet methods
     */
    public function getSupportedGateways()
    {
        // This could be a default list with an event to allow extension
        return [
            "AuthorizeNet_AIM",
            "AuthorizeNet_CIM",
            "AuthorizeNet_SIM",
            "AuthorizeNet_DPM",
            "Dummy",
            "FirstData_Connect",
            "Manual",
            "Mollie",
            "MultiSafepay",
            "Payflow_Pro",
            "PayPal_Express",
            "PayPal_Pro",
            "Pin",
            "SagePay_Direct",
            "SagePay_Server",
            "SecurePay_DirectPost",
            "Stripe",
            "WorldPay",
        ];
    }
}