joshmcarthur / spree-hosted-gateway

Hosted Gateway extension for Spree, supporting redirect to external third-party payment gateways.
BSD 3-Clause "New" or "Revised" License
11 stars 12 forks source link

HostedGateway

There are a bunch of ActiveMerchant modules out there to provide a range of support for various payment gateways, not to mention the ones that Spree users themselves have created. Having reasonable support for external gateways though, is something that I've come to require in a couple of projects, so I've created this extension to help myself and others.

An external gateway (in my mind), is basically a scenario where the customer needs to go to another site to pay, rather than payment happening with some magic behind the scenes. It's very simple, and cheap for store owners, which is why a whole lot of them use something like this method. For us developers though, there are definite downsides - basically, you're losing control of the checkout at the most important stage - payment. There's not much we can do about all these redirects, except to keep track of the orders as best we can.

This extension works in the following way - on the payment page, the customer clicks the continue button, and (thanks to some fancy Javascript), is POST'd to the payment gateway with a couple of parameters that a) The gateway needs, and b) We need to keep track of things. Once payment is made, the gateway does another postback, to us this time, usually with whatever parameters we gave the gateway as well as a couple others (like a status field to tell us whether the payment went through. This postback is directed towards a custom action of the checkout controller, that does some checking to make sure the payment has gone through (As far as we can know), and then updates the order, advancing it to the next step.

Installation

Really, like any Spree 0.30+ extension - add the following to your Gemfile, and run bundle install gem 'hosted_gateway', :git => 'https://www.github.com/joshmcarthur/spree-hosted-gateway.git'

There aren't any migrations or public assets, so there is no need to run rake hosted_gateway:install (Though you can if you really want.

Configuration

From the admin interface, you can add an external gateway as a Payment Method (Look under the 'Configuration' tab). Once you've created an instance of the external gateway, you can enter the server (i.e. where to redirect to).

Something that is kinda necessary with this extension (Since no gateway will be the same, and I've had to make some assumptions to make this generic), is to both understand what the code is doing, and to know how to get it working the way you want. In particular, you should check out the important ExternalGateway class - this is the actual payment method class, and contains the bulk of the logic. I have used Spree's preferences system to hold configuration for a gateway, so you should also look at this - basically, this allows you to add preferences to send to the gateway, without having to touch my code. This extension handles preferences in an interesting way - basically, there are some 'reserved' preferences (i.e. server). These are preferences that ExternalGateway looks for by hardcoded name. All others, though, are automatically inserted into the form as hidden inputs to be POST'd to the gateway. This means you can use something like ExternalGateway.send(:preference, :my_preference, :string) in your own extension or initializer, etc. to add things to the form. I'm pretty sure most of the changes you will need to make will be to get the form data into the structure the gateway expects, so understanding how this works will make things much easier.

Consequences

Enjoy!

Copyright (c) 2011 Josh McArthur[https://www.github.com/joshmcarthur], for 3Months.com[http://www.3months.com], released under the MIT License.