vesse
vesse

Reputation: 5078

Using Omnipay gateway in Sylius with Payum + OmnipayBridge

I have implemented an Omnipay gateway that I now would like to use in Sylius which uses payum 1.3, payum-bundle 1.0, and omnipay-bridge 1.0.

I have configured the gateway (it's a redirecting one that shows a separate payment page and, once payment is completed, will call the returnUrl, and no credit cards involved) in app/config/config.yml:

payum:
    gateways:
        my_gateway:
            omnipay_offsite:
                type: MyOmnipayGateway
                options:
                    gatewayOption: 1
                    anotherOption: 2
              actions:
                  - sylius.payum.my_gateway.action.convert_payment

and I have added the gateway to sylius_payment section as well

sylius_payment:
    gateways:
        my_gateway: My Payment Service

I have added an action to convert the payment to vendor/sylius/sylius/src/Sylius/Bundle/PayumBundle/Resources/config/services.xml:

<service id="sylius.payum.my_gateway.action.convert_payment" class="Sylius\Bundle\PayumBundle\Action\ConvertPaymentToMyGatewayAction">
    <tag name="payum.action" context="my_gateway" />
</service>

and implemented the ConvertPaymentToMyGatewayAction class that now converts the request payload to expected format (used ConvertPaymentToPaypalExpressAction as reference).

I have also added MyOmnipayGateway to the list of gateways in vendor/omnipay/common/composer.json to get past error that says the gateway is not supported.

Now when I complete the order I am succesfully redirected to my actual payment site, and once payment is completed returned to the provided returnUrl with expected parameters in query string. However here the execution goes back to OffsiteCaptureAction and calls purchase with same parameters as the call was originally made and I get redirected to the payment site again and again.

Current questions:

  1. How to avoid adding configuration options under vendor folder - namely the mentioned services.xml and Omnipay's composer.json?

  2. Where to handle the payment response? I need to check the returnUrl query string parameters (implemented in my gateway's completePurchase).

Thanks!

Edit: I missed initializing $details in my convert action with $details = $payment->getDetails(); which caused the _completeCaptureRequired be false every time and thus purchase was executed in a loop. I can now handle the payment properly, ie. question 2 is pretty much solved with the config above and this handler

<?php
namespace Sylius\Bundle\PayumBundle\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\Convert;
use Sylius\Component\Core\Model\PaymentInterface;

class ConvertPaymentToMyGatewayAction implements ActionInterface
{
    public function execute($request)
    {
        RequestNotSupportedException::assertSupports($this, $request);

        /** @var PaymentInterface $payment */
        $payment = $request->getSource();
        $order = $payment->getOrder();

        $details = $payment->getDetails();

        // Fill the correct parameters here

        $request->setResult($details);
    }

    public function supports($request)
    {
        return
            $request instanceof Convert &&
            $request->getSource() instanceof PaymentInterface &&
            $request->getTo() === 'array'
        ;
    }
}

Upvotes: 3

Views: 867

Answers (1)

Brett
Brett

Reputation: 2010

To answer Question 1. You can add your own service to your app config or your own bundle. I like to put it in my own bundle, then when updating sylius it won't get touched.

<service id="sylius.payum.my_gateway.action.convert_payment" class="Sylius\Bundle\PayumBundle\Action\ConvertPaymentToMyGatewayAction">
    <tag name="payum.action" context="my_gateway" />
</service>

Just convert that to yml

sylius.payum.my_gateway.action.convert_payment:
    class: Sylius\Bundle\PayumBundle\Action\ConvertPaymentToMyGatewayAction
    tags:
        -  { name: payum.action, context: my_gateway }

Upvotes: 0

Related Questions