project-a/spryker-zero-payment

spryker zero payment provider


License
Other

Documentation

spryker-zero-payment

Spryker Zero Payment Provider

Example state machine sub process:

<?xml version="1.0" encoding="utf-8"?>
<statemachine
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../process.xsd">

    <process name="ZeroPaymentAuthorization01">

            <states>
                <state name="init zero authorization"/>
                <state name="zero authorized"/>
                <state name="zero authorization failed"/>
            </states>

            <transitions>

                <transition happy="true" condition="ZeroPayment/IsZeroAuthorizationSuccess">
                    <source>init zero authorization</source>
                    <target>zero authorized</target>
                    <event>execute authorization</event>
                </transition>

                <transition>
                    <source>init zero authorization</source>
                    <target>zero authorization failed</target>
                    <event>execute authorization</event>
                </transition>

            </transitions>

            <events>
                <event name="execute authorization" onEnter="true" command="ZeroPayment/AuthorizeZeroPayment"/>
            </events>

        </process>

</statemachine>
namespace Pyz\Zed\Checkout;

use Pav\Zed\ZeroPayment\Communication\Plugin\Checkout\DetectAndSwitchToZeroPaymentPreCheckPlugin;
use Spryker\Zed\Checkout\CheckoutDependencyProvider as SprykerCheckoutDependencyProvider;
use Spryker\Zed\Kernel\Container;
use Spryker\Zed\Payment\Communication\Plugin\Checkout\PaymentPostCheckPlugin;
use Spryker\Zed\Payment\Communication\Plugin\Checkout\PaymentPreCheckPlugin;
use Spryker\Zed\Payment\Communication\Plugin\Checkout\PaymentSaverPlugin;

class CheckoutDependencyProvider extends SprykerCheckoutDependencyProvider
{

    /**
     * @param \Spryker\Zed\Kernel\Container $container ’
     * @return \Spryker\Zed\Checkout\Dependency\Plugin\CheckoutPreConditionInterface[]
     */
    protected function getCheckoutPreConditions(Container $container)
    {
        return [
            ...
            /** zero payment switcher must be before payment pre check plugin **/
            new DetectAndSwitchToZeroPaymentPreCheckPlugin(), //SWITCHES PAYMENT METHOD IF GRAND TOTAL EQUALS 0
            new PaymentPreCheckPlugin(),
            /** --------------- **/
        ];
    }

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Checkout\Dependency\Plugin\CheckoutSaveOrderInterface[]
     */
    protected function getCheckoutOrderSavers(Container $container)
    {
        return [
            ...
            new PaymentSaverPlugin(),
        ];
    }

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Checkout\Dependency\Plugin\CheckoutPostSaveHookInterface[]
     */
    protected function getCheckoutPostHooks(Container $container)
    {
        return [
            ...
            new PaymentPostCheckPlugin()
        ];
    }

}

INTEGRATION spryker/payment 2.x.x, spryker/checkout 2.x.x AND spryker/oms 3.0.4

spryker/oms after v 3

place Zed injector in config/Shared/.... of your needs (for example config_default.php)

$config[KernelConstants::DEPENDENCY_INJECTOR_ZED] = [
    'Payment' => [
        '...',
        'ZeroPayment'
    ],
    'Oms' => [
        '...',
        'ZeroPayment'
    ]
];

spryker/oms before v 3

class OmsDependencyProvider extends SprykerOmsDependencyProvider
{

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Oms\Communication\Plugin\Oms\Condition\ConditionInterface[]
     */
    protected function getConditionPlugins(Container $container)
    {
        $conditionPluginsCollection = parent::getConditionPlugins($container);
        $conditionPluginsCollection->add(new IsZeroAuthorizationSuccess(), 'ZeroPayment/IsZeroAuthorizationSuccess');
        ...

        return $conditionPluginsCollection;
    }

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Oms\Communication\Plugin\Oms\Command\CommandInterface[]
     */
    protected function getCommandPlugins(Container $container)
    {
        $commandPluginsCollection = parent::getCommandPlugins($container);
        $commandPluginsCollection->add(new AuthorizeZeroPayment(), 'ZeroPayment/AuthorizeZeroPayment');
        ...

        return $commandPluginsCollection;
    }

OLD SPRYKER VERSIONS

INTEGRATION spryker/payment 1.x.x, spryker/checkout 1.x.x AND spryker/oms 2.x.x

Register Command/Condition

class OmsDependencyProvider extends SprykerOmsDependencyProvider
{

    /**
     * Overwrite in project
     *
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Oms\Communication\Plugin\Oms\Condition\ConditionInterface[]
     */
    protected function getConditionPlugins(Container $container)
    {
        return [
            ...
            'ZeroPayment/IsZeroAuthorizationSuccess' => new IsZeroAuthorizationSuccess(),
            ...
        ];
    }

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Oms\Communication\Plugin\Oms\Command\CommandInterface[]
     */
    protected function getCommandPlugins(Container $container)
    {
        return [
            ...
            'ZeroPayment/AuthorizeZeroPayment' => new AuthorizeZeroPayment(),
            ...
        ];
    }

}

Apply PreCheck, SaveOrder and PostCheck Plugins to PaymentDependencyProvider on project level.

(ZeroPaymentPreCheckPlugin and ZeroPaymentSaveOrderPlugin are empty classes but needed because the payment bundle requests all hooks once a payment provider is set)

namespace Pyz\Zed\Payment;

use Pav\Shared\ZeroPayment\ZeroPaymentConstants;
use Pav\Zed\ZeroPayment\Communication\Plugin\Checkout\ZeroPaymentPostCheckPlugin;
use Pav\Zed\ZeroPayment\Communication\Plugin\Checkout\ZeroPaymentPreCheckPlugin;
use Pav\Zed\ZeroPayment\Communication\Plugin\Checkout\ZeroPaymentSaveOrderPlugin;
use Spryker\Zed\Kernel\Container;
use Spryker\Zed\Payment\PaymentDependencyProvider as SprykerPaymentDependencyProvider;

class PaymentDependencyProvider extends SprykerPaymentDependencyProvider
{

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return array
     */
    protected function getCheckoutPlugins(Container $container)
    {
        return [
            self::CHECKOUT_PRE_CHECK_PLUGINS => [
                ZeroPaymentConstants::ZERO_PAYMENT => new ZeroPaymentPreCheckPlugin()
            ],
            self::CHECKOUT_ORDER_SAVER_PLUGINS => [
                ZeroPaymentConstants::ZERO_PAYMENT => new ZeroPaymentSaveOrderPlugin()
            ],
            self::CHECKOUT_POST_SAVE_PLUGINS => [
                ZeroPaymentConstants::ZERO_PAYMENT => new ZeroPaymentPostCheckPlugin()
            ],
        ];
    }

}

Apply DetectAndSwitchToZeroPaymentPreCheckPlugin to checkout pre condition plugin stack.

The plugin checks if order grand total is 0 and switches payment method to zero payment

namespace Pyz\Zed\Checkout;

use Pav\Zed\ZeroPayment\Communication\Plugin\Checkout\DetectAndSwitchToZeroPaymentPreCheckPlugin;
use Spryker\Zed\Checkout\CheckoutDependencyProvider as SprykerCheckoutDependencyProvider;
use Spryker\Zed\Kernel\Container;
use Spryker\Zed\Payment\Communication\Plugin\Checkout\PaymentPostCheckPlugin;
use Spryker\Zed\Payment\Communication\Plugin\Checkout\PaymentPreCheckPlugin;
use Spryker\Zed\Payment\Communication\Plugin\Checkout\PaymentSaverPlugin;

class CheckoutDependencyProvider extends SprykerCheckoutDependencyProvider
{

    /**
     * @param \Spryker\Zed\Kernel\Container $container ’
     * @return \Spryker\Zed\Checkout\Dependency\Plugin\CheckoutPreConditionInterface[]
     */
    protected function getCheckoutPreConditions(Container $container)
    {
        return [
            ...
            /** zero payment switcher must be before payment pre check plugin **/
            new DetectAndSwitchToZeroPaymentPreCheckPlugin(),
            new PaymentPreCheckPlugin(),
            /** --------------- **/
        ];
    }

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Checkout\Dependency\Plugin\CheckoutSaveOrderInterface[]
     */
    protected function getCheckoutOrderSavers(Container $container)
    {
        return [
            ...
            new PaymentSaverPlugin(),
        ];
    }

    /**
     * @param \Spryker\Zed\Kernel\Container $container
     * @return \Spryker\Zed\Checkout\Dependency\Plugin\CheckoutPostSaveHookInterface[]
     */
    protected function getCheckoutPostHooks(Container $container)
    {
        return [
            ...
            new PaymentPostCheckPlugin()
        ];
    }

}