momo_pay 2.0 → 2.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 643839942d3b6571ca400f5039952dfc3918d7013585c33e16bdadf09d1109a1
4
- data.tar.gz: a2b1534be8afd77c8d435e9a95826711195133d70e0e5c6dd4493b18613eca18
3
+ metadata.gz: 5fb9540eb6689ec86f35720577a724d00af3d778582bfdab5760e0c66cf4c290
4
+ data.tar.gz: 65d356ef22b59cb92b24be5e35bde13d1d51979841e06ba6b48367c10fc2a6ba
5
5
  SHA512:
6
- metadata.gz: bf0332013009c0ea9697e86940935375386b24569ef81759100f2a12ecccedd814dbdef54c7cb97b2b14c0dd7c878e945e0ef44b3f7d8a64b45ff2c8bfafd630
7
- data.tar.gz: 645384cf12afc022848982110418ae316d2277123b6f8f77753e326525b3e020a9028f87beb748674c8592b534f88e7cb3e88cebfb10d16bf037d8e4af6f921c
6
+ metadata.gz: dd643be78c06462a2937d5854698d5cc1525ffaa5a57de41e1f49908a6e9b6c2c038d1be3e45d555b291b7fcce70c907e2daa897efd545aa00882ec71834c100
7
+ data.tar.gz: 3a7fe520788c9431ded03273b7d93b394cfb7974dd0a8b03c285bfeb6983ada02fa238ee62028f97715428149ae50f81b1c1179464ed1b8d9d37083e097442c2
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
- # MomoPay
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/momo_pay`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
1
+ # API Caller for https://momo.vn
2
+ - API documentation: https://developers.momo.vn
6
3
 
7
4
  ## Installation
8
5
 
@@ -21,23 +18,50 @@ Or install it yourself as:
21
18
  $ gem install momo_pay
22
19
 
23
20
  ## Usage
21
+ #### Setup:
22
+ Add `config/initializers/momo.rb` file with content
23
+ ```ruby
24
+ MomoPay.setup do |config|
25
+ config.domain = Rails.application.secrets.momo_domain
26
+ config.public_key = Rails.application.secrets.momo_public_key
27
+ config.secret_key = Rails.application.secrets.momo_secret_key
28
+ config.partner_code = Rails.application.secrets.momo_partner_code
29
+ config.partner_name = Rails.application.secrets.momo_partner_name
30
+ end
31
+ ```
32
+ #### 1. App-In-App:
33
+ **Request to process transaction:** [detail from Momo](https://developers.momo.vn/#/docs/app_in_app?id=x%e1%bb%ad-l%c3%bd-thanh-to%c3%a1n)
34
+ ```ruby
35
+ response = MomoPay::Mobile.process(
36
+ partnerRefId: order_number,
37
+ amount: order_value,
38
+ appData: momo_token_from_app,
39
+ customerNumber: momo_phone_from_app
40
+ )
41
+ ```
42
+ => Example response data
43
+ ```ruby
44
+ {
45
+ "status"=>0,
46
+ "message"=>"Thành công",
47
+ "amount"=>100000,
48
+ "transid"=>"230.....289",
49
+ "feeMoMo"=>0,
50
+ "signature"=>"2ab844bc9ae52.....0d04d85aef836e9"
51
+ }
52
+ ```
53
+ - How to verify response by HMAC-SHA256
54
+ ```ruby
55
+ MomoPay::Signature.verify!(response)
56
+ ```
57
+ It will raise `MomoPay::SignatureError` exception when response data is not verified!
24
58
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/momo_pay. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
59
+ **Request to confirm transaction:** [detail from Momo](https://developers.momo.vn/#/docs/app_in_app?id=x%c3%a1c-nh%e1%ba%adn-giao-d%e1%bb%8bch)
60
+ ```ruby
61
+ response = MomoPay::Transaction.confirm(
62
+ partnerRefId: order_number,
63
+ momoTransId: transid_from_process_output,
64
+ )
65
+ ```
42
66
 
43
- Everyone interacting in the MomoPay project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/momo_pay/blob/master/CODE_OF_CONDUCT.md).
67
+ ### To be continued...
@@ -3,12 +3,15 @@ module MomoPay
3
3
 
4
4
  attr_accessor :domain
5
5
  attr_accessor :public_key
6
+ attr_accessor :access_key
6
7
  attr_accessor :secret_key
7
8
  attr_accessor :partner_code
8
9
  attr_accessor :partner_name
9
10
 
10
- attr_accessor :signature_verify_keys
11
+ attr_accessor :verify_keys
11
12
  attr_accessor :signature_confirm_keys
13
+ attr_accessor :signature_qr_code_keys
14
+ attr_accessor :signature_one_time_payment_keys
12
15
 
13
16
  def public_key_pem
14
17
  @public_key_pem ||= [
@@ -18,9 +21,21 @@ module MomoPay
18
21
  ].join("\n")
19
22
  end
20
23
 
24
+ def get_verify_keys!(type)
25
+ keys = self.verify_keys[type]
26
+ return keys if keys.is_a?(Array)
27
+ raise MomoPay::SignatureError, "Verify type `#{type}` is not in #{self.verify_keys.keys.join(', ')}"
28
+ end
29
+
21
30
  def initialize
22
- self.signature_verify_keys = MomoPay::Default::SIGNATURE_VERIFY_KEYS
23
31
  self.signature_confirm_keys = MomoPay::Default::SIGNATURE_CONFIRM_KEYS
32
+ self.signature_qr_code_keys = MomoPay::Default::SIGNATURE_QR_CODE_KEYS
33
+ self.signature_one_time_payment_keys = MomoPay::Default::SIGNATURE_ONE_TIME_PAYMENT_KEYS
34
+ self.verify_keys = {
35
+ mobile: MomoPay::Default::SIGNATURE_MOBILE_VERIFY_KEYS,
36
+ ipn: MomoPay::Default::SIGNATURE_IPN_VERIFY_KEYS,
37
+ one_time_payment: MomoPay::Default::SIGNATURE_ONE_TIME_PAYMENT_VERIFY_KEYS,
38
+ }
24
39
  end
25
40
 
26
41
  end
@@ -1,8 +1,77 @@
1
1
  module MomoPay
2
2
  class Default
3
3
 
4
- SIGNATURE_VERIFY_KEYS = %w(status message amount transid)
5
- SIGNATURE_CONFIRM_KEYS = %w(partnerCode partnerRefId requestType requestId momoTransId)
4
+ SIGNATURE_MOBILE_VERIFY_KEYS = [
5
+ 'status',
6
+ 'message',
7
+ 'amount',
8
+ 'transid',
9
+ ]
6
10
 
11
+ SIGNATURE_IPN_VERIFY_KEYS = [
12
+ 'partnerCode',
13
+ 'accessKey',
14
+ 'requestId',
15
+ 'amount',
16
+ 'orderId',
17
+ 'orderInfo',
18
+ 'orderType',
19
+ 'transId',
20
+ 'message',
21
+ 'localMessage',
22
+ 'responseTime',
23
+ 'errorCode',
24
+ 'payType',
25
+ 'extraData',
26
+ ]
27
+
28
+ SIGNATURE_CONFIRM_KEYS = [
29
+ 'partnerCode',
30
+ 'partnerRefId',
31
+ 'requestType',
32
+ 'requestId',
33
+ 'momoTransId',
34
+ ]
35
+
36
+ SIGNATURE_QR_CODE_KEYS = [
37
+ 'partnerCode',
38
+ 'accessKey',
39
+ 'requestId',
40
+ 'amount',
41
+ 'orderId',
42
+ 'orderInfo',
43
+ 'returnUrl',
44
+ 'notifyUrl',
45
+ 'extraData',
46
+ ]
47
+
48
+ SIGNATURE_ONE_TIME_PAYMENT_VERIFY_KEYS = [
49
+ 'accessKey',
50
+ 'amount',
51
+ 'extraData',
52
+ 'message',
53
+ 'orderId',
54
+ 'orderInfo',
55
+ 'orderType',
56
+ 'partnerCode',
57
+ 'payType',
58
+ 'requestId',
59
+ 'responseTime',
60
+ 'resultCode',
61
+ 'transId'
62
+ ]
63
+
64
+ SIGNATURE_ONE_TIME_PAYMENT_KEYS = [
65
+ 'accessKey',
66
+ 'amount',
67
+ 'extraData',
68
+ 'ipnUrl',
69
+ 'orderId',
70
+ 'orderInfo',
71
+ 'partnerCode',
72
+ 'redirectUrl',
73
+ 'requestId',
74
+ 'requestType'
75
+ ]
7
76
  end
8
77
  end
@@ -0,0 +1,18 @@
1
+ module MomoPay
2
+ class OneTimePayment
3
+
4
+ def self.request(data)
5
+ data.merge!({
6
+ requestType: 'captureWallet',
7
+ accessKey: MomoPay.setup.access_key,
8
+ partnerCode: MomoPay.setup.partner_code,
9
+ partnerName: MomoPay.setup.partner_name,
10
+ })
11
+ signature = MomoPay::Signature.new(data, MomoPay.setup.signature_one_time_payment_keys).to_s
12
+ data.merge!(signature: signature)
13
+
14
+ MomoPay::Request.post('/v2/gateway/api/create', data)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MomoPay
4
+ class OneTimePaymentSignature < Signature
5
+
6
+ def initialize(data, keys)
7
+ data.merge!({
8
+ accessKey: MomoPay.setup.access_key,
9
+ })
10
+ @query_string = MomoPay::QueryString.new(data).to_s(keys)
11
+ end
12
+
13
+ def to_s
14
+ OpenSSL::HMAC.hexdigest("SHA256", MomoPay.setup.secret_key, query_string)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :query_string
20
+
21
+ end
22
+ end
23
+
@@ -0,0 +1,17 @@
1
+ module MomoPay
2
+ class QrCode
3
+
4
+ def self.request(data)
5
+ data.merge!({
6
+ requestType: 'captureMoMoWallet',
7
+ accessKey: MomoPay.setup.access_key,
8
+ partnerCode: MomoPay.setup.partner_code,
9
+ partnerName: MomoPay.setup.partner_name,
10
+ })
11
+ data.merge!(signature: MomoPay::Signature.new(data, MomoPay.setup.signature_qr_code_keys).to_s)
12
+
13
+ MomoPay::Request.post('/gw_payment/transactionProcessor', data)
14
+ end
15
+
16
+ end
17
+ end
@@ -10,5 +10,9 @@ module MomoPay
10
10
  response.parse
11
11
  end
12
12
 
13
+ def self.random_id
14
+ "#{Time.now.strftime("%Y%m%d%H%M%S")}#{SecureRandom.hex(2)}"
15
+ end
16
+
13
17
  end
14
18
  end
@@ -1,12 +1,11 @@
1
1
  module MomoPay
2
2
  class Signature
3
3
 
4
- def self.verify!(data)
5
- momo_signature = self.new(data, MomoPay.setup.signature_verify_keys).to_s
4
+ def self.verify!(data, type)
5
+ momo_signature = self.new(data, MomoPay.setup.get_verify_keys!(type)).to_s
6
6
  data_signature = data['signature'] || data[:signature]
7
- if momo_signature != data_signature
8
- raise MomoPay::SignatureError.new("#{momo_signature} vs #{data_signature}")
9
- end
7
+ return true if momo_signature == data_signature
8
+ raise MomoPay::SignatureError, "Gem:#{momo_signature || 'nil'} vs Data:#{data_signature || 'nil'}"
10
9
  end
11
10
 
12
11
  def initialize(data, keys)
@@ -1,3 +1,3 @@
1
1
  module MomoPay
2
- VERSION = "2.0"
2
+ VERSION = "2.0.3"
3
3
  end
data/lib/momo_pay.rb CHANGED
@@ -2,6 +2,7 @@ require "http"
2
2
  require 'openssl'
3
3
  require 'base64'
4
4
  require 'json'
5
+ require 'securerandom'
5
6
 
6
7
  require "momo_pay/version"
7
8
  require "momo_pay/config"
@@ -9,12 +10,15 @@ require "momo_pay/default"
9
10
  require "momo_pay/error"
10
11
  require "momo_pay/http"
11
12
  require "momo_pay/mobile"
13
+ require "momo_pay/qr_code"
12
14
  require "momo_pay/query_status"
13
15
  require "momo_pay/query_string"
14
16
  require "momo_pay/request"
15
17
  require "momo_pay/rsa"
16
18
  require "momo_pay/signature"
17
19
  require "momo_pay/transaction"
20
+ require "momo_pay/one_time_payment"
21
+ require "momo_pay/one_time_payment_signature"
18
22
 
19
23
  module MomoPay
20
24
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: momo_pay
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.0'
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - KhoaRB
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-01 00:00:00.000000000 Z
11
+ date: 2024-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,9 @@ files:
73
73
  - lib/momo_pay/error.rb
74
74
  - lib/momo_pay/http.rb
75
75
  - lib/momo_pay/mobile.rb
76
+ - lib/momo_pay/one_time_payment.rb
77
+ - lib/momo_pay/one_time_payment_signature.rb
78
+ - lib/momo_pay/qr_code.rb
76
79
  - lib/momo_pay/query_status.rb
77
80
  - lib/momo_pay/query_string.rb
78
81
  - lib/momo_pay/request.rb
@@ -88,7 +91,7 @@ metadata:
88
91
  homepage_uri: https://github.com/ThanhKhoaIT/momo
89
92
  source_code_uri: https://github.com/ThanhKhoaIT/momo
90
93
  changelog_uri: https://github.com/ThanhKhoaIT/momo
91
- post_install_message:
94
+ post_install_message:
92
95
  rdoc_options: []
93
96
  require_paths:
94
97
  - lib
@@ -103,8 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
106
  - !ruby/object:Gem::Version
104
107
  version: '0'
105
108
  requirements: []
106
- rubygems_version: 3.0.6
107
- signing_key:
109
+ rubygems_version: 3.2.3
110
+ signing_key:
108
111
  specification_version: 4
109
112
  summary: Momo wallet SDK for RUBY
110
113
  test_files: []