Spree Commerce Resources

Spree Commerce

 

https://spreecommerce.org/docs/developer/core-concepts/payments

 

 

https://github.com/activemerchant/active_merchant#supported-payment-gateways

 

 

Adding your custom Payment Method

In order to make your own custom Payment Method show up on the backend list of available payment methods, you need to add it to the spree config list of payment methods first.

Firstly create a new model inheriting from Spree::PaymentMethod in your app/models directory:

class FancyPaymentMethod < Spree::PaymentMethod
end

Next, add your custom gateway to the list of available payment methods in config/initializers/spree.rb:

Rails.application.config.after_initialize do
  Rails.application.config.spree.payment_methods << FancyPaymentMethod
end

Spree Braintree Vzero is a good example of a standalone custom gateway.

Payment Method visibility

We’ve mentioned before that a PaymentMethod can have a display_on attribute. This attribute can have the following values: frontback, or both. For more granular control which Payment Methods should be available in which Store, you can override the available_for_store? method in your PaymentMethod subclass.

class FancyPaymentMethod < Spree::PaymentMethod
  def available_for_store?(store)
    store.supported_currencies.include?('EUR')
  end
end

Above code will make the payment method available only for stores that support the EUR currency.

If you want more control you can specify available_for_order? method to control Payment Method visibility for specific Order, eg.

class FancyPaymentMethod < Spree::PaymentMethod
  def available_for_order?(order)
    order.total > 100 && order.currency == 'USD'
  end
end

This code will make the payment method available only for orders with a total greater than 100 and currency USD.