payu-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b1732d292b7b39f5caa2029546ff3a61ecb5fda4b031e4fa10889c8b6dca91d8
4
+ data.tar.gz: 3013a7bd787eaeab76408c23c8732a22b84750cb40bfff2849de505569ec03ae
5
+ SHA512:
6
+ metadata.gz: 7f8dadf7909d6a59f189127fbafcb34befe5b6a5926f7fb27a818796030310d575db281450e482fc70d8dbf4c512c8f9b76f9f2b5660b78067c56153cc43072f
7
+ data.tar.gz: 6b4e6decc7e64d9e343d16e01bd0c4339c6b5c4219efce523d0ab3c46389a6e83e3047caed4c4035c8da8d24889740421da933c896f2e9a0e389d5417b2d8f4a
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'minitest
@@ -0,0 +1,9 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << "test"
5
+ t.libs << "lib"
6
+ t.test_files = FileList["test/**/*.spec.rb"]
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,39 @@
1
+ # Payu ruby SDK
2
+
3
+ ## Installation
4
+
5
+ Add this line to your app's Gemfile:
6
+
7
+ ```rb
8
+ gem 'payu-sdk'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install payu-sdk
18
+
19
+ ## Usage
20
+
21
+ Remember to `require 'payu-sdk'` before anything else.
22
+
23
+ Next, you need to bootstrap using key and salt by following:
24
+
25
+ ```rb
26
+ PayU.init('<payu_key>', '<payu_salt>')
27
+ ```
28
+ If you are using rails, the right place to do this might be config/initializers/payu.rb.
29
+
30
+ ### Hash API
31
+
32
+ ```rb
33
+ hash_str = PayU::Hasher.generate_hash txn_id: "5512244", amount: "1000", product_info: 'mobile', first_name: 'Jacob', email: 'test@payu.in'
34
+
35
+ # Validate reverse hash received from payu after checkout
36
+
37
+ reverse_hash = '<payu_hash>' # hash received after payment from payu
38
+ is_verified = PayU::Hasher.validate_hash reverse_hash, txn_id: "5512244", amount: "1000", product_info: 'mobile', first_name: 'Jacob', email: 'test@payu.in', status: 'success'
39
+ ```
@@ -0,0 +1,17 @@
1
+ require 'payu/constants'
2
+ require 'payu/payu_error';
3
+ require 'payu/validator'
4
+ require 'payu/hasher'
5
+
6
+ # Base module
7
+ module PayU
8
+ class << self
9
+ attr_accessor :key, :salt
10
+ end
11
+
12
+ def self.init(key, salt)
13
+ self.key = key
14
+ self.salt = salt
15
+ end
16
+
17
+ end
@@ -0,0 +1,5 @@
1
+ module PayU
2
+ VERSION = '1.0.0'.freeze
3
+ EMAIL_REGEX = /^(?=.{6,254}$)[A-Za-z0-9_\-\.]{1,64}\@[A-Za-z0-9_\-\.]+\.[A-Za-z]{2,}$/.freeze
4
+ AMOUNT_REGEX = /^\d+(\.\d{1,2})?$/.freeze
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'digest'
2
+
3
+ module PayU
4
+ class Hasher
5
+ class << self
6
+ def generate_hash(
7
+ txn_id:,
8
+ amount:,
9
+ product_info:,
10
+ first_name:,
11
+ email:,
12
+ udf1: '',
13
+ udf2: '',
14
+ udf3: '',
15
+ udf4: '',
16
+ udf5: '')
17
+ PayU::Validator.validate_hash_params({
18
+ txn_id: txn_id,
19
+ amount: amount,
20
+ product_info: product_info,
21
+ first_name: first_name,
22
+ email: email,
23
+ udf1: udf1,
24
+ udf2: udf2,
25
+ udf3: udf3,
26
+ udf4: udf4,
27
+ udf5: udf5
28
+ })
29
+ data = "#{PayU.key}|#{txn_id}|#{amount}|#{product_info}|#{first_name}|#{email}|#{udf1}|#{udf2}|#{udf3}|#{udf4}|#{udf5}||||||#{PayU.salt}"
30
+ Digest::SHA512.hexdigest data
31
+ end
32
+
33
+ def validate_hash(hash_str,
34
+ txn_id:,
35
+ amount:,
36
+ product_info:,
37
+ first_name:,
38
+ email:,
39
+ udf1: '',
40
+ udf2: '',
41
+ udf3: '',
42
+ udf4: '',
43
+ udf5: '',
44
+ status:,
45
+ additional_charges: ''
46
+ )
47
+ PayU::Validator.validate_hash_params({
48
+ txn_id: txn_id,
49
+ amount: amount,
50
+ product_info: product_info,
51
+ first_name: first_name,
52
+ email: email,
53
+ udf1: udf1,
54
+ udf2: udf2,
55
+ udf3: udf3,
56
+ udf4: udf4,
57
+ udf5: udf5
58
+ })
59
+ data = "|||||#{udf5}|#{udf4}|#{udf3}|#{udf2}|#{udf1}|#{email}|#{first_name}|#{product_info}|#{amount}|#{txn_id}|#{PayU.key}"
60
+ data = additional_charges + data if additional_charges
61
+ hash_str == Digest::SHA512.hexdigest("#{PayU.salt}|#{status}|#{data}")
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,8 @@
1
+ module PayU
2
+ class PayuError < StandardError
3
+ def initialize(msg="PayuError", exception_type="Standard")
4
+ @exception_type = exception_type
5
+ super(msg)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,43 @@
1
+ module PayU
2
+ class Validator
3
+ class << self
4
+ def validate_hash_params(**kwargs)
5
+ kwargs.each do |k, v|
6
+ if not v&.is_a? String
7
+ raise PayU::PayuError.new "#{k} should be of type String"
8
+ end
9
+
10
+ if kwargs[:txn_id].length > 25
11
+ raise PayU::PayuError.new "txn_id length should be less than equal to 25"
12
+ end
13
+
14
+ if kwargs[:product_info].length > 100
15
+ raise PayU::PayuError.new "product_info length should be less than equal to 100"
16
+ end
17
+
18
+ if kwargs[:first_name].length > 60
19
+ raise PayU::PayuError.new "first_name length should be less than equal to 60"
20
+ end
21
+
22
+ if kwargs[:email].length > 50
23
+ raise PayU::PayuError.new "email length should be less than equal to 50"
24
+ end
25
+
26
+ if !PayU::EMAIL_REGEX.match(kwargs[:email])
27
+ raise PayU::PayuError.new "Invalid Email"
28
+ end
29
+
30
+ if !PayU::AMOUNT_REGEX.match(kwargs[:amount])
31
+ raise PayU::PayuError.new "amount should contain digits with upto 2 decimal places"
32
+ end
33
+
34
+ [kwargs[:udf1], kwargs[:udf2], kwargs[:udf3], kwargs[:udf4], kwargs[:udf5]].each do |udf|
35
+ if udf.length > 255
36
+ raise PayuError.new "udf length should be less than equal to 255"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+
2
+ lib = File.expand_path('lib', Dir.pwd)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'payu/constants'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'payu-sdk'
8
+ s.version = PayU::VERSION
9
+ s.summary = "Payu's Ruby API"
10
+ s.authors = ['']
11
+ s.email = ['dx@payu.in']
12
+ s.files = `git ls-files`.split("\n")
13
+ s.homepage = 'https://payu.in'
14
+ s.license = 'MIT'
15
+ s.description = 'Official Ruby SDK for PayU'
16
+ s.require_paths = ['lib']
17
+ end
@@ -0,0 +1,26 @@
1
+ require "minitest/autorun"
2
+ require "payu"
3
+
4
+ describe PayU do
5
+ before do
6
+ PayU.init('bjg$121', 'vhgvs75')
7
+ end
8
+
9
+ describe "when paramters are valid for generate_hash" do
10
+ it "must return hash string" do
11
+ _(PayU::Hasher.generate_hash txn_id: "5512244", amount: "1000", product_info: 'mobile', first_name: 'Jacob', email: 'test@payu.in').must_equal 'b035620f2f75e99b74e8064203519daea6d8fb5ac81d4c4c599e1b50b63f2d610195145e9769e527682aff06555e511e9c3ec58ea51291cccdbcc3427b35630d'
12
+ end
13
+ end
14
+
15
+ describe "when paramters are valid for validate_hash" do
16
+ it "must return false when hash is not valid" do
17
+ _(PayU::Hasher.validate_hash 'reverse_hash', txn_id: "5512244", amount: "1000", product_info: 'mobile', first_name: 'Jacob', email: 'test@payu.in', status: 'success').must_equal false
18
+ end
19
+
20
+ it "must return true when hash is valid" do
21
+ _(PayU::Hasher.validate_hash '10da174f9789c00c3290dbda12947deae227cd7f039d14db587c2be3663b4bde9d405edd4dad0a4458cbc36ee10e92389a81213b54a5d83ce68aad32fa61eef4', txn_id: "5512244", amount: "1000", product_info: 'mobile', first_name: 'Jacob', email: 'test@payu.in', status: 'success').must_equal true
22
+ end
23
+
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payu-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ''
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Official Ruby SDK for PayU
14
+ email:
15
+ - dx@payu.in
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Rakefile
22
+ - Readme.md
23
+ - lib/payu-sdk.rb
24
+ - lib/payu/constants.rb
25
+ - lib/payu/hasher.rb
26
+ - lib/payu/payu_error.rb
27
+ - lib/payu/validator.rb
28
+ - payu-sdk.gemspec
29
+ - test/payu/hasher.spec.rb
30
+ homepage: https://payu.in
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.0.6
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Payu's Ruby API
53
+ test_files: []