Payment

At this time, several payment methods are handled in Sonata e-commerce. Whether you’d like to use credit card payment providers such as Scellius or Ogone ; simply Paypal ; or even want the ability to handle check payments, you should find what you need in what’s already provided. Otherwise, you may of course add your own payment methods (and feel free to submit them to the community).

You may get more details about the architecture here: Payment.

Add a payment method

A payment method is basically a service that implements the PaymentInterface and that is tagged sonata.payment.method. Thoses are the pre-requisites. An abstract BasePayment class is available, we advise you to use it to implement your own payment method.

Once you’ve declared your service (for the example, let’s say I’ve named it app.payment.mymethod), add it under the sonata_payment configuration as follows:

1
2
3
4
sonata_payment:
    services:
        # ...
        app.payment.mymethod: ~

Please keep in mind that we won’t process your service configuration. You’ll need to call the following methods on your service (if you wish to) manually:

  • setName
  • setCode
  • setEnabled
  • setOptions
  • addTransformer

Your service must return a unique, non-null key when the getCode method is called, or it might be overridden in the payment methods pool. To do that, either set the $this->code parameter in the constructor, or override the getCode method to return a constant string as follows:

namespace App\Sonata\PaymentBundle\Method;

use Sonata\Component\Payment\BasePayment;

class MyMethod extends BasePayment
{
    public function __construct(/* ... */)
    {
        $this->setCode('mymethod');

        // ...
    }

    // or...

    public function getCode()
    {
        return 'mymethod';
    }
}

Configuration

Here’s the full default configuration for SonataPaymentBundle:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
sonata_payment:
    selector:             sonata.payment.selector.simple
    generator:            sonata.payment.generator.mysql
    transformers:
        order:                sonata.payment.transformer.order      # The service to transform an order into a basket
        basket:               sonata.payment.transformer.basket     # The service to transform a basket into an order
    services:
        # Which payment methods are enabled?
        paypal:
            name:                 Paypal
            code:                 paypal
            transformers:
                basket:               sonata.payment.transformer.basket
                order:                sonata.payment.transformer.order
            options:
                shop_secret_key:      ~
                web_connector_name:   curl
                account:              [email protected]
                cert_id:              fake
                debug:                false
                paypal_cert_file:     '%kernel.root_dir%/paypal_cert_pem_sandbox.txt'
                url_action:           https://www.sandbox.paypal.com/cgi-bin/webscr
                class_order:          App\Sonata\OrderBundle\Entity\Order
                url_callback:         sonata_payment_callback
                url_return_ko:        sonata_payment_error
                url_return_ok:        sonata_payment_confirmation
                method:               encryptViaBuffer
                key_file:             '%kernel.root_dir%/my-prvkey.pem'
                cert_file:            '%kernel.root_dir%/my-pubcert.pem'
                openssl:              /opt/local/bin/openssl
        pass:
            name:                 Pass
            code:                 pass
            transformers:
                basket:               sonata.payment.transformer.basket
                order:                sonata.payment.transformer.order
            browser:              sonata.payment.browser.curl
            options:
                shop_secret_key:      ~
                url_callback:         sonata_payment_callback
                url_return_ko:        sonata_payment_error
                url_return_ok:        sonata_payment_confirmation
        check:
            name:                 Check
            code:                 check
            transformers:
                basket:               sonata.payment.transformer.basket
                order:                sonata.payment.transformer.order
            browser:              sonata.payment.browser.curl
            options:
                shop_secret_key:      ~
                url_callback:         sonata_payment_callback
                url_return_ko:        sonata_payment_error
                url_return_ok:        sonata_payment_confirmation
        scellius:
            name:                 Scellius
            code:                 scellius
            generator:            sonata.payment.provider.scellius.none_generator
            transformers:
                basket:               sonata.payment.transformer.basket
                order:                sonata.payment.transformer.order
            options:
                url_callback:         sonata_payment_callback
                url_return_ko:        sonata_payment_error
                url_return_ok:        sonata_payment_confirmation
                template:             SonataPaymentBundle:Payment:scellius.html.twig
                shop_secret_key:      ~
                request_command:      ~
                response_command:     ~
                merchant_id:          ~
                merchant_country:     ~
                pathfile:             ~
                language:             ~
                payment_means:        ~
                base_folder:          ~
                data:
                header_flag:          no
                capture_day:
                capture_mode:
                bgcolor:
                block_align:
                block_order:
                textcolor:
                normal_return_logo:
                cancel_return_logo:
                submit_logo:
                logo_id:
                logo_id2:
                advert:
                background_id:
                templatefile:
        ogone:
            name:                 Ogone
            code:                 ogone
            transformers:
                basket:               sonata.payment.transformer.basket
                order:                sonata.payment.transformer.order
            options:
                url_callback:         sonata_payment_callback
                url_return_ko:        sonata_payment_error
                url_return_ok:        sonata_payment_confirmation
                form_url:             ~ # Required
                catalog_url:          ~ # Required
                home_url:             ~ # Required
                pspid:                ~ # Required
                sha_key:              ~ # Required
                sha-out_key:          ~ # Required
                template:             SonataPaymentBundle:Payment:ogone.html.twig
    class:
        order:                App\Sonata\OrderBundle\Entity\Order
        transaction:          App\Sonata\PaymentBundle\Entity\Transaction

    # Here you will enable the payment methods you wish to provide
    # and add your custom ones
    methods:
        pass: ~     # This is a provided method, we don't need to specify its service id
        bitcoin: app.payment.bitcoin    # Custom payment method, we specify the service id

If you want to use the DebugPayment method, you need to add its configuration in the dev config file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
sonata_payment:
    services:
        debug:
            name:    Debug Payment
            enabled: true
            code:    debug
            browser: sonata.payment.browser.curl

            transformers:
                basket: sonata.payment.transformer.basket
                order:  sonata.payment.transformer.order

            options:
                url_callback:  sonata_payment_callback
                url_return_ko: sonata_payment_error
                url_return_ok: sonata_payment_confirmation

Add a custom payment method

In order to add a custom payment methods, here are the steps to follow:

  1. Create your own payment method class:

    namespace App\Payment;
    
    use Sonata\Component\Payment\BasePayment;
    
    // ...
    
    class Bitcoin extends BasePayment
    {
        // ...
    
        public function getCode()
        {
            return 'bitcoin';
        }
    
    }
    
  2. Declare the service associated (don’t forget the tag):

1
2
3
<service id="app.payment.bitcoin" class="App\Payment\BitcoinPayment">
    <tag name="sonata.payment.method"/>
</service>
  1. Add it to your configuration:
1
2
3
4
5
6
sonata_payment:
    # ...

    methods:
        # ...
        bitcoin: app.payment.bitcoin
  1. That’s it!