Simple library for accepting payments via PayU.
- No dependencies on ActiveSupport or any similar libraries, so it should work in any Ruby application or framework
- Simple configuration (multiple points of sale)
- Automatic signature generation and verification
- View helpers for generating payment form and verifying signatures
In your Gemfile:
Or, from the command line:
gem install payu
This gem implements only core functionality for integrating with PayU gateway. It is designed to work with any ruby framework or plain ruby application. To integrate it in your app you need to do some work, but it is really simple.
Your app will interact with PayU via point of sale (Pos). You can create it on PayU website and get its credentials. With these credentials you can create Pos instance:
pos = ::. :pos_id => '12345', :pos_auth_key => 'abcdefghijk', :key1 => 'xxxxxxxx', :key2 => 'xxxxxxxx'
You can also load Pos configuration from yaml file. For example config/payu.yml:
bank: pos_id: 12345 pos_auth_key: XXX key1: XXX key2: XXX type: default sms: pos_id: 56789 pos_auth_key: XXX key1: XXX key2: XXX type: sms_premium
Then add new initializer config/initializers/payu.rb:
Now you can access them by name or pos_id:
['bank'] [:bank]  ['56789']
Create new payment
To create new payment:
@transaction = pos.new_transaction(:first_name => 'John', :last_name => 'Doe', :email => '[email protected]', :client_ip => '188.8.131.52', :amount => 10000)
Now you need to build form with this transaction object:
<%= form_tag(@transaction.new_url) do %> <%= payu_hidden_fields(@transaction) %> <%= submit_tag 'Pay' %> <% end %>
Read payment status
You can check status of any payment:
response = pos.get(123456789) if response.status == 'OK' if response.completed? # payment completed end end
123456789 is a payment session id. It is automatically generated when you create new transaction.
By default when somebody sends a payment it is automatically accepted. You can turn it off and confirm every payment:
response = pos.confirm(123456789)
You can cancel payment:
response = pos.cancel(123456789)
Handle Payu callbacks
When payment status changes, Payu will send report to your application. You need controller to handle these reports:
class PayuController < ApplicationController include Payu::Helpers skip_before_filter :verify_authenticity_token def ok # successful redirect end def error # failed redirect end def report payu_verify_params(params) response = Payu['main'].get params[:session_id] if response.status == 'OK' order = Order.find(response.trans_order_id) if response.completed? && order.present? # mark order as paid else # payment not completed end end render :text => 'OK' end
match '/payu/ok' => 'payu#ok' match '/payu/error' => 'payu#error' match '/payu/report' => 'payu#report'
Actions ok and error are pages where user is redirected after payment. You need to enter these url on Payu website.
Copyright (c) 2013 Michał Młoźniak. See LICENSE for details.