nequi 0.2.1 → 0.2.4

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: 045a325deecbb8f309b3420ebe908e192580a7f6fb353e43724d22635d425bb8
4
- data.tar.gz: adacb4e9d2f97edb22e903eed453359e6bb758a690e5dc568ec4ca8bf3de442e
3
+ metadata.gz: abbefb8ee00a9cf229ad5cf57d1df4e635d6f0b143f786a04c03e3e12455caa9
4
+ data.tar.gz: 8903c1e8f64d6610bf03b47292c04a7d6e13a5211da61bed2e406462210504ed
5
5
  SHA512:
6
- metadata.gz: b1caed0a1ca5baabe6be34edb39fbcbf51bda5db633e9fffbaee50b9fe8b91d85fa3422f36f2b310bc9c2fcfd977569f0a221aaa216794f5eff4a832d6c2f141
7
- data.tar.gz: 38bec8604a947dd3339ea1fd94285794e7cbeb8a953f5a92e5ba57704324847ddb88d995b84856bb157fd93d31f683459b0081e1c8f91191523d70acad1fcab8
6
+ metadata.gz: '0188ce3485e7cfb95178e42b6da80df2806b41607c6265cdbf704cd72effb1f953da8108e1ce80c8f41889a5ff059d387524ab2cd000e1603da75478b90d26b1'
7
+ data.tar.gz: 553ca437e995578386248307b4a9fb3f29d8ea58fb3a16fe5fdae4845e379ade828f91df08dbd676b6c585bf24ca0f085a887b579bdb9e8a33decf236a60ec4c
data/lib/nequi.rb CHANGED
@@ -4,9 +4,10 @@ require_relative "nequi/version"
4
4
 
5
5
  module Nequi
6
6
  class Error < StandardError; end
7
- require 'securerandom'
8
7
  require 'httparty'
9
8
  require 'base64'
9
+ require 'json'
10
+ require 'time'
10
11
  require 'active_support/core_ext/integer/time'
11
12
 
12
13
  include HTTParty
@@ -22,14 +23,12 @@ module Nequi
22
23
 
23
24
  class Configuration
24
25
  attr_accessor :auth_uri, :phone, :auth_grant_type, :unregisteredpayment_endpoint,
25
- :client_id, :client_secret, :api_base_path, :api_key
26
+ :client_id, :client_secret, :api_base_path, :api_key, :nequi_status_payment
26
27
  end
27
28
 
28
29
  NEQUI_STATUS_CODE_SUCCESS = '200'.freeze
29
30
 
30
31
  def self.get_token
31
- return @token if @token
32
-
33
32
  headers = {
34
33
  'Content-Type' => 'application/x-www-form-urlencoded',
35
34
  'Accept' => 'application/json',
@@ -43,18 +42,20 @@ module Nequi
43
42
  raise "Failed to authenticate with Nequi. HTTP status code: #{response.code}" unless (response.code.to_i == 200 && !response.body.empty?)
44
43
 
45
44
  response_body = JSON.parse(response.body)
46
- @token = { access_token: response_body['access_token'], token_type: response_body['token_type'], expires_at: Time.now + 2.hours }
45
+ @token = { access_token: response_body['access_token'], token_type: response_body['token_type'], expires_at: Time.now + 15.minutes }
47
46
  end
48
47
 
49
- def self.charge(amount, phone)
48
+ def self.payment_request(amount, phone, product_id)
50
49
  current_time = Time.now
51
- formatted_timestamp = current_time.strftime('%Y-%m-%d %H:%M:%S.%6N %z')
52
- message_id = SecureRandom.uuid
50
+ utc_time = current_time.utc
51
+ formatted_timestamp = utc_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
52
+
53
+ access_token = get_token[:access_token]
53
54
 
54
55
  headers = {
55
56
  'Content-Type' => 'application/json',
56
57
  'Accept' => 'application/json',
57
- 'Authorization' => "Bearer #{get_token[:access_token]}",
58
+ 'Authorization' => "Bearer #{access_token}",
58
59
  'x-api-key' => configuration.api_key
59
60
  }
60
61
 
@@ -63,11 +64,11 @@ module Nequi
63
64
  "RequestHeader" => {
64
65
  "Channel" => "PNP04-C001",
65
66
  "RequestDate" => formatted_timestamp,
66
- "MessageID" => message_id,
67
+ "MessageID" => product_id,
67
68
  "ClientID" => configuration.client_id,
68
69
  "Destination" => {
69
- "ServiceName" => "PaymentsService",
70
- "ServiceOperation" => "unregisteredPayment",
70
+ "ServiceName" => "PaymentsService",
71
+ "ServiceOperation" => "unregisteredPayment",
71
72
  "ServiceRegion" => "C001",
72
73
  "ServiceVersion" => "1.2.0"
73
74
  }
@@ -75,51 +76,33 @@ module Nequi
75
76
  "RequestBody" => {
76
77
  "any" => {
77
78
  "unregisteredPaymentRQ" => {
78
- "phoneNumber" => "#{phone}",
79
+ "phoneNumber" => phone,
79
80
  "code" => "NIT_1",
80
- "value" => "#{amount}"
81
+ "value" => amount
81
82
  }
82
83
  }
83
84
  }
84
85
  }
85
86
  }.to_json
86
87
 
87
- logs = [{ 'type' => 'information', 'message' => "Ready to send Petitions" }]
88
-
89
- unregisteredpayment = configuration.api_base_path + configuration.unregisteredpayment_endpoint
90
-
91
- response = HTTParty.post(unregisteredpayment, body: body, headers: headers)
92
-
93
- response_body = JSON.parse(response.body)
94
-
95
- if response.code.to_i == 200 && !response_body['ResponseMessage']['ResponseBody'].nil?
96
- logs << { 'type' => 'information', 'message' => "Petition returned HTTP 200" }
97
-
98
- begin
99
- any_data = response_body['ResponseMessage']['ResponseBody']['any']
100
-
101
- status = response_body['ResponseMessage']['ResponseHeader']['Status']
102
- status_code = status ? status['StatusCode'] : ''
103
- status_desc = status ? status['StatusDesc'] : ''
104
-
105
- if status_code == '200'
106
- logs << { 'type' => 'success', 'message' => 'Payment request send success fully' }
88
+ unregisteredpayment = configuration.api_base_path + configuration.unregisteredpayment_endpoint
107
89
 
108
- payment = any_data['unregisteredPaymentRS']
109
- trn_id = payment ? payment['transactionId'].to_s.strip : ''
90
+ response = HTTParty.post(unregisteredpayment, body: body, headers: headers)
110
91
 
111
- logs << { 'type' => 'success', 'message' => 'Transaction Id: ' + trn_id }
112
- else
113
- raise 'Error ' + status_code + ' = ' + status_desc
114
- end
92
+ response_status = response["ResponseMessage"]["ResponseHeader"]["Status"]
93
+ status_code = response_status["StatusCode"]
94
+ status_description = response_status["StatusDesc"]
115
95
 
116
- rescue StandardError => e
117
- raise e
118
- end
119
- else
120
- raise 'Unable to connect to Nequi, please check the information sent.'
121
- end
96
+ return {
97
+ type: 'Error',
98
+ status: status_code,
99
+ api_status: status_code,
100
+ message: status_description
101
+ } unless response_status.include?({ "StatusCode"=>"0", "StatusDesc"=>"SUCCESS" })
122
102
 
123
- logs
103
+ response_any = response["ResponseMessage"]["ResponseBody"]["any"]
104
+ success_id = response_any["unregisteredPaymentRS"]["transactionId"]
105
+ # Nequi::StatusCheckJob.set(wait: 2.minutes).perform_later(product_id, configuration, access_token, success_id)
106
+ { type: 'success', status: response.code, api_status: status_code, message: 'Payment request send success fully'}
124
107
  end
125
108
  end
@@ -0,0 +1,68 @@
1
+ # lib/nequi_status_check_job.rb
2
+
3
+ module Nequi
4
+ class StatusCheckJob < Struct.new(:product_id, :configuration, :token, :success_id)
5
+ require 'httparty'
6
+ require 'base64'
7
+ require 'json'
8
+ require 'time'
9
+ require 'active_support/core_ext/integer/time'
10
+
11
+
12
+ def perform
13
+
14
+ current_time = Time.now
15
+ utc_time = current_time.utc
16
+ formatted_timestamp = utc_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
17
+
18
+ headers = {
19
+ 'Content-Type' => 'application/json',
20
+ 'Accept' => 'application/json',
21
+ 'Authorization' => "Bearer #{token}",
22
+ 'x-api-key' => configuration.api_key
23
+ }
24
+
25
+ body = {
26
+ "RequestMessage" => {
27
+ "RequestHeader" => {
28
+ "Channel" => "PNP04-C001",
29
+ "RequestDate" => formatted_timestamp,
30
+ "MessageID" => product_id,
31
+ "ClientID" => configuration.client_id,
32
+ "Destination" => {
33
+ "ServiceName" => "PaymentsService",
34
+ "ServiceOperation" => "getStatusPayment",
35
+ "ServiceRegion" => "C001",
36
+ "ServiceVersion" => "1.0.0"
37
+ }
38
+ },
39
+ "RequestBody" => {
40
+ "any" => {
41
+ "getStatusPaymentRQ" => {
42
+ "codeQR": success_id
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }.to_json
48
+
49
+ nequi_status_payment_url = configuration.api_base_path + configuration.nequi_status_payment
50
+
51
+ response = HTTParty.post(nequi_status_payment_url, body: body, headers: headers)
52
+
53
+
54
+ response_status = response["ResponseMessage"]["ResponseHeader"]["Status"]
55
+ status_code = response_status["StatusCode"]
56
+ status_description = response_status["StatusDesc"]
57
+
58
+
59
+ if status_description = response_status["StatusDesc"] == "SUCCESS"
60
+ logs = { type: 'success', status: response.code, api_status: status_code, message: 'Payment request send success fully'}
61
+
62
+ Rails.logger.info(logs)
63
+ else
64
+ { type: 'success', status: response.code, api_status: status_code, message: ":wClient don't accept charge"}
65
+ end
66
+ end
67
+ end
68
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nequi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - geocodinglife
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-22 00:00:00.000000000 Z
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -78,6 +78,26 @@ dependencies:
78
78
  - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: 3.18.1
81
+ - !ruby/object:Gem::Dependency
82
+ name: sidekiq
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '7.1'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 7.1.2
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '7.1'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 7.1.2
81
101
  description: Nequi gem provides a convenient way to integrate with Nequi payments
82
102
  systems for processing payments and other related operations.
83
103
  email:
@@ -88,6 +108,7 @@ extra_rdoc_files: []
88
108
  files:
89
109
  - lib/nequi.rb
90
110
  - lib/nequi/version.rb
111
+ - lib/nequi_status_check_job.rb
91
112
  homepage: https://github.com/geocodinglife/nequi
92
113
  licenses:
93
114
  - MIT