vnpay 0.0.1

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: 305b30b9a2b6b8c2c0cd6ed08ce2f926c48531131db2da26119c77b4e2ac88bf
4
+ data.tar.gz: d7e8e3abfd2aaab69f0d1d7b4082d54dfa5c52a98053544cf2452e7d7c4a2743
5
+ SHA512:
6
+ metadata.gz: 01eaa52fabc8099305e7195b413b3d25414aff4b22c4f2f414b2eaa690f5c9730e9b79304d0b2c7f52b1da352c818139dc7d2a3237c4d2ac48e443a8e55f82b1
7
+ data.tar.gz: 292033e4dd1227ff117b31cb12cf2451428e2937eacef0b39bff6f2e81eebcfcd0619933f324a25adf3540f6f4127cc150534670d06a304cdea69a561b3fe3b8
@@ -0,0 +1,2 @@
1
+ # vnpay
2
+ Ruby gem for VNPay integration
File without changes
@@ -0,0 +1,7 @@
1
+ class OrderInfo
2
+ attr_accessor :payment_ref, :amount, :description, :created_at
3
+
4
+ def created_at=(value)
5
+ @created_at = value.strftime('%Y%m%d%H%M%S')
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ require 'order_info'
2
+ require 'vnpay/url'
3
+ require 'vnpay/verify'
4
+
5
+ class Vnpay
6
+ def self.configuration
7
+ @configuration ||= OpenStruct.new
8
+ end
9
+
10
+ def self.configure
11
+ yield(configuration)
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module Vnpay
2
+ class Url
3
+ attr_accessor :order_info, :ip_address, :callback_url
4
+
5
+ def initialize(order_info, ip_address, callback)
6
+ @order_info = order_info
7
+ @ip_address = ip_address
8
+ @callback_url = callback_url
9
+ end
10
+
11
+ def generate
12
+ config.payment_url + query_string + "&vnp_SecureHashType=MD5&vnp_SecureHash=#{secure_hash}"
13
+ end
14
+
15
+ private
16
+
17
+ def query_string
18
+ initiate_request.map { |k, v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
19
+ end
20
+
21
+ def secure_hash
22
+ data = initiate_request.sort.map { |k, v| "#{k}=#{v}" }.join('&')
23
+ Digest::MD5.hexdigest(config.secret_key + data)
24
+ end
25
+
26
+ def initiate_request
27
+ {
28
+ vnp_Amount: order_info.amount.to_i * 100,
29
+ vnp_Command: 'pay',
30
+ vnp_CreateDate: order_info.created_at.to_i,
31
+ vnp_CurrCode: 'VND',
32
+ vnp_IpAddr: ip_address,
33
+ vnp_Locale: 'vn',
34
+ vnp_OrderInfo: order_info.description.to_s,
35
+ vnp_OrderType: "250000",
36
+ vnp_ReturnUrl: callback_url,
37
+ vnp_TmnCode: config.website_code,
38
+ vnp_TxnRef: order_info.payment_ref.to_s,
39
+ vnp_Version: "2.0.0"
40
+ }
41
+ end
42
+
43
+ def config
44
+ @config ||= Vnpay.configuration
45
+ end
46
+
47
+ def url_encode(string)
48
+ Rack::Utils.escape(string)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ module Vnpay
2
+ class Verify
3
+
4
+ REJECTED_PARAMS = %w[vnp_SecureHashType vnp_SecureHash controller action].freeze
5
+
6
+ attr_reader :params, :normalized_params
7
+
8
+ def initialize(params)
9
+ @params = params.permit!.to_h
10
+ end
11
+
12
+ def verify
13
+ normalize_params
14
+
15
+ return false unless success_code?
16
+ return false unless valid_hash?
17
+
18
+ true
19
+ end
20
+
21
+ private
22
+
23
+ def success_code?
24
+ params['vnp_ResponseCode'] == '00'
25
+ end
26
+
27
+ def valid_hash?
28
+ params['vnp_SecureHash'] == calculated_secure_hash
29
+ end
30
+
31
+ def calculated_secure_hash
32
+ REJECTED_PARAMS.each { |key| params.delete(key) }
33
+ data = params.sort.map { |k, v| "#{k}=#{v}" }.join('&')
34
+ Digest::MD5.hexdigest(Vnpay.configuration.secret_key + data)
35
+ end
36
+
37
+ def normalize_params
38
+ @normalized_params ||= {}
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vnpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nhannvu19
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ description: Ruby library for VNPay integration
28
+ email:
29
+ - nhannvu.19@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/order_info.rb
37
+ - lib/vnpay.rb
38
+ - lib/vnpay/url.rb
39
+ - lib/vnpay/verify.rb
40
+ homepage: https://github.com/nhannvu19/vnpay
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.0.8
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Ruby library for VNPay integration
63
+ test_files: []