webpay_interswitch 1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +97 -0
- data/lib/generators/webpay_interswitch/install_generator.rb +16 -0
- data/lib/webpay_interswitch.rb +7 -0
- data/lib/webpay_interswitch/core.rb +10 -0
- data/lib/webpay_interswitch/form_builder.rb +81 -0
- data/lib/webpay_interswitch/gateway.rb +55 -0
- data/lib/webpay_interswitch/transaction_query.rb +91 -0
- data/lib/webpay_interswitch/version.rb +3 -0
- data/lib/webpay_interswitch/view_helper.rb +11 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3e59162aaf80b3b37d6feb352229568cb5a180f0
|
4
|
+
data.tar.gz: 256eb634543a590ca016bf67888b634cf2be8c18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f30180b731e9c48d3345b78556b683d1e7ed90e7c1f428ec1324cae79a28cea9b484464ae510605eb2e3d6495feb846ed3403ba906c0cade746d645c413e614c
|
7
|
+
data.tar.gz: 4034b8020a85b0a57bbdc74fd82076192cf81834d9584776a72d1d405df5a671fd20f1f4780d061d3d5bf05ffbafdea732e0be7dd88eef6e96e6cd5ac2067bdb
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
#WebpayInterswitch
|
2
|
+
|
3
|
+
|
4
|
+
[![Code Climate](https://codeclimate.com/github/vinsol/webpay_interswitch.png)](https://codeclimate.com/github/vinsol/webpay_interswitch)
|
5
|
+
|
6
|
+
|
7
|
+
A simple gem to integrate your Rails app with [Webpay Interswitch](http://www.interswitchng.com/), a nigerian payment gateway. It provides methods to add webpay as a payment option in your Rails application out of the box. It also provides a simple API that can be used to fetch transactions (JSON Only).
|
8
|
+
|
9
|
+
##Getting Started
|
10
|
+
|
11
|
+
|
12
|
+
Include the gem in your Rails app by adding it to Gemfile:
|
13
|
+
|
14
|
+
`gem 'webpay_interswitch'` and run
|
15
|
+
|
16
|
+
`bundle install`
|
17
|
+
|
18
|
+
And then:
|
19
|
+
|
20
|
+
`rails generate webpay_interswitch:install`
|
21
|
+
|
22
|
+
The generator will install an initializer which adds the configuration options for setting up the gateway from the webpay_interswitch.yml file.
|
23
|
+
|
24
|
+
It also adds a `webpay_interswitch.yml.example` file which should be considered as a reference while setting up the gateway. Create `webpay_interswitch.yml` file in `config` directory and specify your mac_key, product_id, pay_item_id etc.
|
25
|
+
|
26
|
+
|
27
|
+
##Helper
|
28
|
+
|
29
|
+
|
30
|
+
It comes with a helper method that seamlessly adds a webpay form to your Rails application.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
<%= form_for_webpay(txn_ref, amount) %>
|
34
|
+
```
|
35
|
+
|
36
|
+
This helper adds a button (Along with all the hidden elements required to make the transaction). You can also configure the text that appears on the button and add any valid html attributes to it like:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
<%= form_for_webpay(txn_ref, amount, { submit_button_text: 'Make Payment', class: 'btn-class' }) %>
|
40
|
+
```
|
41
|
+
|
42
|
+
txn_ref must be a globally unique number generated by your application. Each request that hits the gateway must have a unique value of txn_ref. To avoid polluting the global space, consider prefixing it with a specific string. E.g. my_app_1, my_app_2 and so on for each transaction.
|
43
|
+
|
44
|
+
The gateway accepts the amount only in Kobo. So, you must always pass the amount after converting the amount in Naira to Kobo.
|
45
|
+
|
46
|
+
Once you click the submit button, you'll be redirected to the Webpay to carry out the transaction. Upon completion(success or failure), Webpay will send a POST request to the redirect URL along with a few parameters. Remember that the helper generates an HTML form for itself. So, make sure it is not placed inside another HTML form.
|
47
|
+
|
48
|
+
|
49
|
+
##Querying Transactions
|
50
|
+
|
51
|
+
|
52
|
+
The gem provides an API that should be used to query the transaction.
|
53
|
+
|
54
|
+
Create an object of WebpayInterswitch::Transaction using:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
transaction = WebpayInterswitch::TransactionQuery.new(params, amount)
|
58
|
+
```
|
59
|
+
|
60
|
+
It accepts the params hash that is sent by the gateway to the redirect URL. The amount of the transaction is a required parameter and must be set to ensure the payment amount was not tampered.
|
61
|
+
|
62
|
+
You can then call methods on this object like:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
transaction.success?
|
66
|
+
|
67
|
+
transaction.fetch_response
|
68
|
+
|
69
|
+
transaction.full_error_message
|
70
|
+
```
|
71
|
+
|
72
|
+
##Configurations Parameters
|
73
|
+
|
74
|
+
|
75
|
+
Below parameters are needed to setup the gateway.
|
76
|
+
|
77
|
+
* product_id: It is provided when you sign up for interswitch webpay.
|
78
|
+
|
79
|
+
* pay_item_id: It is provided when you sign up for interswitch webpay.
|
80
|
+
|
81
|
+
* site_redirect_url: This is the URL where the interswitch redirects back with a POST request when the transaction is complete (Successful Or Failed). This is the controller action, where you want to process the transaction response using `WebpayInterswitch::TransactionQuery` class.
|
82
|
+
|
83
|
+
* mac_key: It is provided when you sign up for interswitch webpay.
|
84
|
+
|
85
|
+
* currency (Optional, defaults to Naira): Currently, we only support Naira. Currency Code for Naira is `566`. The payment amount is always specified in Kobo. While interfacing with the gem, you must always pass in the amount in Kobo. `Conversion: 1 Naira = 100 Kobo`
|
86
|
+
|
87
|
+
* test (Optional, defaults to true): This is used to specify Interswitch URL. If true, all the requests are made at the test gateway. For production environments (Making actual payments), you must set this explicitly to false. By default, it would always be set to true.
|
88
|
+
|
89
|
+
## Find a Demo Application
|
90
|
+
|
91
|
+
##### [Interswitch Demo](https://github.com/vinsol/webpay_interswitch_gem_demo_app)
|
92
|
+
|
93
|
+
##Credits
|
94
|
+
[![vinsol.com: Ruby on Rails, iOS and Android developers](http://vinsol.com/vin_logo.png "Ruby on Rails, iOS and Android developers")](http://vinsol.com)
|
95
|
+
|
96
|
+
Copyright (c) 2014 [vinsol.com](http://vinsol.com "Ruby on Rails, iOS and Android developers"), released under the New MIT License
|
97
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module WebpayInterswitch
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../../../templates", __FILE__)
|
5
|
+
|
6
|
+
def create_webpay_yml
|
7
|
+
copy_file 'webpay_interswitch.yml.example', 'config/webpay_interswitch.yml.example'
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_webpay_initializer
|
11
|
+
copy_file 'webpay.rb', 'config/initializers/webpay.rb'
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'webpay_interswitch/view_helper.rb'
|
2
|
+
require 'webpay_interswitch/core.rb'
|
3
|
+
require 'webpay_interswitch/form_builder.rb'
|
4
|
+
require 'webpay_interswitch/gateway.rb'
|
5
|
+
require 'webpay_interswitch/transaction_query.rb'
|
6
|
+
|
7
|
+
ActionView::Base.send(:include, WebpayInterswitch::ViewHelper) if defined?(::Rails)
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module WebpayInterswitch
|
2
|
+
class FormBuilder
|
3
|
+
|
4
|
+
include WebpayInterswitch::Core
|
5
|
+
|
6
|
+
def initialize(txn_ref, amount, html_options)
|
7
|
+
@txn_ref = txn_ref
|
8
|
+
@amount = amount
|
9
|
+
@html_options = html_options
|
10
|
+
sanitize_options
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate_webpay_form
|
14
|
+
form_html = "<form action=#{ WebpayInterswitch::Gateway.url } method=post>"
|
15
|
+
|
16
|
+
form_html += generate_webpay_elements
|
17
|
+
|
18
|
+
form_html += generate_transaction_data_elements
|
19
|
+
|
20
|
+
# This generates the submit button alongwith the @submit_button_text
|
21
|
+
# If submit_button_text is not provided, it defaults to 'Make Payment'
|
22
|
+
form_html += generate_input_field('commit', @submit_button_text, 'submit', @html_options)
|
23
|
+
|
24
|
+
form_html += '</form>'
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def valid?
|
29
|
+
@txn_ref.present? && Integer(@amount.to_s) > 0 rescue false
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def generate_webpay_elements
|
35
|
+
webpay_elem_html = ''
|
36
|
+
%w(product_id pay_item_id currency site_redirect_url).each do |field_name|
|
37
|
+
webpay_elem_html += generate_input_field(field_name, WebpayInterswitch::Gateway.public_send("#{ field_name }"))
|
38
|
+
end
|
39
|
+
webpay_elem_html
|
40
|
+
end
|
41
|
+
|
42
|
+
def generate_transaction_data_elements
|
43
|
+
txn_elem_html = generate_input_field('txn_ref', @txn_ref)
|
44
|
+
txn_elem_html += generate_input_field('amount', @amount)
|
45
|
+
txn_elem_html += generate_input_field('hash', sha_hash(string_for_hash_param))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generates the input tag alongwith the provided name, value, type and html_options if any.
|
49
|
+
def generate_input_field(name, value, type = 'hidden', html_options={})
|
50
|
+
html_string = string_for_html_options(html_options)
|
51
|
+
"<input type=#{ type } name=#{ name } value='#{ value }' #{html_string}>"
|
52
|
+
end
|
53
|
+
|
54
|
+
## Returns a string that is used to compute the sha hash for POST request on webpay.
|
55
|
+
def string_for_hash_param
|
56
|
+
@txn_ref.to_s +
|
57
|
+
WebpayInterswitch::Gateway.product_id +
|
58
|
+
WebpayInterswitch::Gateway.pay_item_id +
|
59
|
+
@amount.to_s +
|
60
|
+
WebpayInterswitch::Gateway.site_redirect_url +
|
61
|
+
WebpayInterswitch::Gateway.mac_key
|
62
|
+
end
|
63
|
+
|
64
|
+
## Returns a string from html_options that is embedded into the submit button.
|
65
|
+
## e.g: html_options = {id: 'hi', class: 'c1 c2'}
|
66
|
+
## output: "id='hi' class='c1 c2'"
|
67
|
+
def string_for_html_options(html_options)
|
68
|
+
html_string = ''
|
69
|
+
html_options.each do |attr, val|
|
70
|
+
html_string << " #{attr}='#{val}'"
|
71
|
+
end
|
72
|
+
html_string
|
73
|
+
end
|
74
|
+
|
75
|
+
def sanitize_options
|
76
|
+
@html_options.symbolize_keys!
|
77
|
+
@submit_button_text = @html_options.delete(:submit_button_text) || 'Make Payment'
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module WebpayInterswitch
|
2
|
+
|
3
|
+
class MissingParameterError < StandardError; end
|
4
|
+
class UnsupportedCurrencyError < StandardError; end
|
5
|
+
|
6
|
+
class Gateway
|
7
|
+
|
8
|
+
cattr_accessor :product_id, :pay_item_id, :currency, :site_redirect_url, :mac_key, :test
|
9
|
+
|
10
|
+
TEST_URL = 'https://stageserv.interswitchng.com/test_paydirect/pay'
|
11
|
+
|
12
|
+
LIVE_URL = 'https://webpay.interswitchng.com/paydirect/pay'
|
13
|
+
|
14
|
+
## ACCEPTED_CURRENCIES:
|
15
|
+
## * Naira (In Kobo)
|
16
|
+
|
17
|
+
ACCEPTED_CURRENCIES = ['566']
|
18
|
+
|
19
|
+
def self.url
|
20
|
+
WebpayInterswitch::Gateway.test ? TEST_URL : LIVE_URL
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.setup
|
24
|
+
yield self
|
25
|
+
set_defaults!
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate!
|
29
|
+
requires!(:product_id, :pay_item_id, :currency, :site_redirect_url, :mac_key)
|
30
|
+
validate_currency!
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def self.set_defaults!
|
36
|
+
# Default currency to Naira (Kobo)
|
37
|
+
@@currency ||= '566'
|
38
|
+
|
39
|
+
## Default test to true.
|
40
|
+
## Set this to false explicitly only in production environment.
|
41
|
+
@@test ||= false
|
42
|
+
end
|
43
|
+
|
44
|
+
def requires!(*required_parameters)
|
45
|
+
required_parameters.each do |parameter|
|
46
|
+
raise WebpayInterswitch::MissingParameterError if (self.public_send(parameter).blank?)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate_currency!
|
51
|
+
raise WebpayInterswitch::UnsupportedCurrencyError unless ACCEPTED_CURRENCIES.include?(@@currency.to_s)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
module WebpayInterswitch
|
3
|
+
|
4
|
+
#################################################################################################
|
5
|
+
## This class can be used to fetch transaction details
|
6
|
+
## Initialization Parameters:
|
7
|
+
##
|
8
|
+
## post_params: Parameters sent via POST by the gateway at the redirect_url
|
9
|
+
## amount: This is needed to detect the masquerading or parameter tampering to alter the amount.
|
10
|
+
## WebpayInterswitch::TransactionQuery.new(params, amount)
|
11
|
+
##################################################################################################
|
12
|
+
|
13
|
+
|
14
|
+
class TransactionQuery
|
15
|
+
|
16
|
+
include WebpayInterswitch::Core
|
17
|
+
|
18
|
+
# resp and desc are empty when transaction is successful
|
19
|
+
# txnref is the unique number sent in request.
|
20
|
+
# cardNum is always zero
|
21
|
+
# apprAmt is always zero
|
22
|
+
# response is populated when a transactions search query is sent.
|
23
|
+
attr_accessor :txnref, :resp, :desc, :payRef, :retRef, :cardNum, :amount, :response
|
24
|
+
|
25
|
+
TEST_URL = 'https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json'
|
26
|
+
|
27
|
+
LIVE_URL = 'https://webpay.interswitchng.com/paydirect/api/v1/gettransaction.json'
|
28
|
+
|
29
|
+
def initialize(post_params={}, amount)
|
30
|
+
post_params.to_hash.symbolize_keys!
|
31
|
+
post_params.each_pair do |key, val|
|
32
|
+
instance_variable_set("@#{key}", val)
|
33
|
+
end
|
34
|
+
@amount = amount
|
35
|
+
fetch_response
|
36
|
+
end
|
37
|
+
|
38
|
+
## fetches a returns a transaction as json
|
39
|
+
def fetch_response
|
40
|
+
uri = URI.parse(make_request)
|
41
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
42
|
+
http.use_ssl = true
|
43
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
44
|
+
|
45
|
+
@response = JSON.parse http.get(uri.request_uri, { 'Hash' => sha_hash(string_for_get_query) }).body
|
46
|
+
end
|
47
|
+
|
48
|
+
## Returns true or false
|
49
|
+
def success?
|
50
|
+
@response['ResponseCode'] == '00'
|
51
|
+
end
|
52
|
+
|
53
|
+
def full_error_message
|
54
|
+
"#{ error_message }. #{ query_error_message }"
|
55
|
+
end
|
56
|
+
|
57
|
+
## Returns error message if present. Else returns a blank string.
|
58
|
+
## This is generally due to incorrect parameters such as product_id, pay_item_id etc.
|
59
|
+
def error_message
|
60
|
+
@desc.strip.gsub(',', '')
|
61
|
+
end
|
62
|
+
|
63
|
+
## Returns error message recd from the gateway if the txn query was failed.
|
64
|
+
## else, returns nil
|
65
|
+
## This is due to: incorrect amount, invalid card number etc.
|
66
|
+
def query_error_message
|
67
|
+
@response['ResponseDescription'] unless success?
|
68
|
+
end
|
69
|
+
|
70
|
+
def transaction_url
|
71
|
+
WebpayInterswitch::Gateway.test ? TEST_URL : LIVE_URL
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def make_request
|
77
|
+
url = transaction_url
|
78
|
+
url += "?productid=#{WebpayInterswitch::Gateway.product_id}"
|
79
|
+
url += "&transactionreference=#{@txnref}"
|
80
|
+
url += "&amount=#{@amount}"
|
81
|
+
end
|
82
|
+
|
83
|
+
## Returns a string that is used to compute the sha hash for get transaction query on webpay.
|
84
|
+
def string_for_get_query
|
85
|
+
WebpayInterswitch::Gateway.product_id +
|
86
|
+
@txnref.to_s +
|
87
|
+
WebpayInterswitch::Gateway.mac_key
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module WebpayInterswitch
|
2
|
+
module ViewHelper
|
3
|
+
|
4
|
+
def form_for_webpay(txn_ref, amount, html_options={})
|
5
|
+
form_builder = WebpayInterswitch::FormBuilder.new(txn_ref, amount, html_options)
|
6
|
+
WebpayInterswitch::Gateway.new.validate!
|
7
|
+
form_builder.generate_webpay_form.html_safe if form_builder.valid?
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webpay_interswitch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shubham Gupta
|
8
|
+
- Jitender Rai
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-01-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
description: A simple gem to integrate your Rails app with Webpay Interswitch, a nigerian
|
29
|
+
payment gateway
|
30
|
+
email:
|
31
|
+
- info@vinsol.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- README.md
|
37
|
+
- lib/generators/webpay_interswitch/install_generator.rb
|
38
|
+
- lib/webpay_interswitch.rb
|
39
|
+
- lib/webpay_interswitch/core.rb
|
40
|
+
- lib/webpay_interswitch/form_builder.rb
|
41
|
+
- lib/webpay_interswitch/gateway.rb
|
42
|
+
- lib/webpay_interswitch/transaction_query.rb
|
43
|
+
- lib/webpay_interswitch/version.rb
|
44
|
+
- lib/webpay_interswitch/view_helper.rb
|
45
|
+
homepage: http://vinsol.com
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.2.2
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: A simple gem to integrate your Rails app with Webpay Interswitch, a nigerian
|
69
|
+
payment gateway
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|