Skip to content

Instantly share code, notes, and snippets.

@mmoreram
Last active December 24, 2015 08:39
Show Gist options
  • Save mmoreram/6771947 to your computer and use it in GitHub Desktop.
Save mmoreram/6771947 to your computer and use it in GitHub Desktop.
How to configure your Symfony2 project to work with Payfony platforms

Configure PayFony Environment

  1. About PaymentBridgeBundle
  2. PaymentBridge Service
  3. Payment Event Listener

About PaymentBridgeBundle

As Payment Suite should be compatible with all E-commerces projects, it's built without any kind of attachment with your model, so you must build (just once) a specific bridge bundle to tell Payment Suite where to find some data.

For this proposal, create a Bundle named PaymentBridgeBundle with following features.

PaymentBridge Service

Payment Service is a service that has to be necessarily implemented. This service must implements Mmoreram\PaymentCoreBundle\Services\Interfaces\PaymentBridgeInterface.

<?php

namespace YourProjectName\PaymentCoreBundle\Services\interfaces;

use Mmoreram\PaymentCoreBundle\Services\Interfaces\PaymentBridgeInterface;

class PaymentBridge implements PaymentBridgeInterface
{

    /**
     * @var Order
     *
     * Order object
     */
    private $order;


    /**
     * Set order to OrderWrapper
     *
     * @var Object $order Order element
     */
    public function setOrder($order)
    {
        $this->order = $order;
    }


    /**
     * Return order
     *
     * @return Object order
     */
    public function getOrder()
    {
        return $this->order;
    }


    /**
     * Return order identifier value
     *
     * @return integer
     */
    public function getOrderId()
    {
        return $this->order->getId();
    }


    /**
     * Given an id, find Order
     *
     * @return Object order
     */
    public function findOrder($orderId)
    {
        /*
        * Your code to get Order
        */

        return $this->order;
    }


    /**
     * Get currency
     *
     * @return string
     */
    public function getCurrency()
    {
        /*
        * Set your static or dynamic currency
        */

        return 'USD';
    }


    /**
     * Get amount
     *
     * @return integer
     */
    public function getAmount()
    {
        /*
        * Return payment amount (in cents)
        */

        return $amount;
    }


    /**
     * Get extra data
     *
     * @return array
     */
    public function getExtraData()
    {
        return array();
    }
} ?>

This service must be named payment.bridge adding next code to Resources\config\services.yml.

services:
    /* ... */

    payment.bridge:
        class: YourProjectName\PaymentBridgeBundle\Services\PaymentBridge

Payment Event Listener

You can create an Event Listener to subscribe to Payment process events.

In fact, this will be the way to manage your cart and your order in every payment stage.

<?php

namespace YourProjectName\PaymentBridgeBundle\EventListener;

use Mmoreram\PaymentCoreBundle\Event\PaymentOrderLoadEvent;
use Mmoreram\PaymentCoreBundle\Event\PaymentOrderCreatedEvent;
use Mmoreram\PaymentCoreBundle\Event\PaymentOrderDoneEvent;
use Mmoreram\PaymentCoreBundle\Event\PaymentOrderSuccessEvent;
use Mmoreram\PaymentCoreBundle\Event\PaymentOrderFailEvent;

/**
 * Payment event listener
 *
 * This listener is enabled whatever the payment method is.
 */
class Payment
{    

    /**
     * On payment order load event
     *
     * @param PaymentOrderLoadEvent $paymentOrderLoadEvent Payment Order Load event
     */
    public function onPaymentOrderLoad(PaymentOrderLoadEvent $paymentOrderLoadEvent)
    {
        /*
         * Your code for this event
         */
    }


    /**
     * On payment order created event
     *
     * @param PaymentOrderCreatedEvent $paymentOrderCreatedEvent Payment Order Created event
     */
    public function onPaymentOrderCreated(PaymentOrderCreatedEvent $paymentOrderCreatedEvent)
    {
        /*
         * Your code for this event
         */
    }


    /**
     * On payment done event
     *
     * @param PaymentOrderDoneEvent $paymentOrderDoneEvent Payment Order Done event
     */
    public function onPaymentDone(PaymentOrderDoneEvent $paymentOrderDoneEvent)
    {
        /*
         * Your code for this event
         */
    }


    /**
     * On payment success event
     *
     * @param PaymentOrderSuccessEvent $paymentOrderSuccessEvent Payment Order Success event
     */
    public function onPaymentSuccess(PaymentOrderSuccessEvent $paymentOrderSuccessEvent)
    {
        /*
         * Your code for this event
         */
    }


    /**
     * On payment fail event
     *
     * @param PaymentOrderFailEvent $paymentOrderFailEvent Payment Order Fail event
     */
    public function onPaymentFail(PaymentOrderFailEvent $paymentOrderFailEvent)
    {
        /*
         * Your code for this event
         */
    }
}

Also you need to register listener for the events in Resources\config\services.yml adding next code.

services:
    /* ... */

    payment.event.listener:
        class: YourProjectName\PaymentBridgeBundle\EventListener\Payment
        arguments:
            entity.manager: "@doctrine.orm.entity_manager"
            mailer: @mailer
        tags:
            - { name: kernel.event_listener, event: payment.order.done, method: onPaymentOrderDone }
            - { name: kernel.event_listener, event: payment.order.created, method: onPaymentOrderCreated }
            - { name: kernel.event_listener, event: payment.order.load, method: onPaymentLoad }
            - { name: kernel.event_listener, event: payment.order.success, method: onPaymentSuccess }
            - { name: kernel.event_listener, event: payment.order.fail, method: onPaymentFail }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment