PxFusion
A Rubygem for talking to DPS's PxFusion payment product via their SOAP API. Includes the ablity to start a transaction, and then query for the status of that transaction once the payment is complete, following the pictured flow:
Installation
Add this line to your application's Gemfile:
gem 'pxfusion'
And then execute:
$ bundle
Or install it yourself as:
$ gem install pxfusion
Some configuration settings are available:
# Your PxFusion username
PxFusion.username = 'sample'
# Your PxFusion password
PxFusion.password = 'sample'
# A global return URL
# You can also override the return URL by passing
# it into a Transaction constructor
PxFusion.default_return_url = 'https://test.site/purchase'
# Override the default currency to charge in
# (Default is NZD)
PxFusion.default_currency = 'USD'
In a Rails project, you should put this configuration in an initializer or your Rails
environment configuration. Remember, DO NOT PLACE PASSWORDS IN YOUR CODE. Instead, you
might want to load your configuration into environment variables, and refer to them
using notation like ENV['PXFUSION_USERNAME']
.
Usage
The gem wraps around PxFusion's SOAP API. Given correct credentials, it will happily build a PxFusion
transaction for you, but then it's up to you to build the form required to submit to DPS. Any PxFusion::Transaction
object can be passed straight to a form_for
method though, so it oughtn't be too difficult:
# app/controllers/payments_controller.rb
def new
@order = Order.find(1)
@transaction = PxFusion::Transaction.new(amount: @order.total, reference: @order.to_param, return_url: payments_path)
render
end
# This is the POST-back destination for PxFusion
def create
@order = Order.find(1)
@transaction = PxFusion::Transaction.fetch(params[:sessionid])
if @transaction.status == PxFusion.statuses[:approved]
@order.paid!
respond_to root_path and return
else
render :new
end
end
And then your view:
# app/views/payments/new.html.erb
<%= form_for @transaction do |f| %>
<%= f.hidden_field :session_id, name: 'SessionId' %>
<%= text_field_tag 'CardNumber' %>
<%= date_select_tag 'ExpiryMonth', include_days: false %>
<%= text_field_tag 'CardHolderName' %>
<%= text_field_tag 'Cvc2', maxlength: 4 %>
<%= f.submit 'Make Payment' %>
<% end %>
Please read the PxFusion docs while implementing your payment flow to ensure you understand the process - it'll help you avoid confusing problems, which is important, as this gateway is not well documented.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Run the specs:
rspec spec
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Think something's missing?
If you can think of a way that this gem could be easier to use, or if you've found a bug (reasonably) likely, then please lodge an issue on Github, so that we can help out. Thanks!
If you need to regenerate the HTTP fixtures
For portability and testing speed, we use fixtures to test API calls against, rather than calling the actual API. This works usually, but if you're seeing strange results or have changed any API methods, you can rengenerate the fixtures:
- Remove the current fixtures:
rm -r spec/fixtures
- Add your credentials to
spec/spec_helper.rb
BE SURE NOT TO COMMIT THIS FILE - Generate a Transaction and add the transaction ID into:
spec/pxfusion/transaction_spec.rb
- Run the specs
- Commit and pull request the fixture changes as above if necessary