Module: Nequi

Includes:
HTTParty
Defined in:
lib/nequi.rb,
lib/nequi/version.rb

Defined Under Namespace

Classes: Configuration, Error

Constant Summary collapse

ERRORS_MESSAGES =
{
    "20-07A": "Nequi resive the payload but got an error from them."
}
NEQUI_STATUS_CODE_SUCCESS =
'200'.freeze
VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



20
21
22
# File 'lib/nequi.rb', line 20

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



23
24
25
26
# File 'lib/nequi.rb', line 23

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.get_tokenObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nequi.rb', line 35

def self.get_token
  headers = {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept' => 'application/json',
    'Authorization' => "Basic #{Base64.strict_encode64("#{configuration.client_id}:#{configuration.client_secret}")}"
  }

  body = { 'grant_type' => configuration.auth_grant_type }

  response = HTTParty.post(configuration.auth_uri, body: body, headers: headers)

  raise "Failed to authenticate with Nequi. HTTP status code: #{response.code}" unless (response.code.to_i == 200 && !response.body.empty?)

  response_body = JSON.parse(response.body)
  @token = { access_token: response_body['access_token'], token_type: response_body['token_type'], expires_at: Time.now + 15.minutes }
end

.payment_request(amount, phone, product_id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/nequi.rb', line 52

def self.payment_request(amount, phone, product_id)
  current_time = Time.now
  utc_time = current_time.utc
  formatted_timestamp = utc_time.strftime('%Y-%m-%dT%H:%M:%S.%LZ')

  access_token = get_token[:access_token]

  headers = {
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'Authorization' => "Bearer #{access_token}",
    'x-api-key' => configuration.api_key
  }

  body = {
    "RequestMessage" => {
      "RequestHeader" => {
        "Channel" => "PNP04-C001",
        "RequestDate" => formatted_timestamp,
        "MessageID" => product_id,
        "ClientID" => configuration.client_id,
        "Destination" => {
        "ServiceName" => "PaymentsService",
        "ServiceOperation" => "unregisteredPayment",
          "ServiceRegion" => "C001",
          "ServiceVersion" => "1.2.0"
        }
      },
      "RequestBody" => {
        "any" => {
          "unregisteredPaymentRQ" => {
            "phoneNumber" => phone,
            "code" => "NIT_1",
            "value" => amount
          }
        }
      }
    }
  }.to_json

 unregisteredpayment = configuration.api_base_path + configuration.unregisteredpayment_endpoint

 response = HTTParty.post(unregisteredpayment, body: body, headers: headers)

 response_status = response["ResponseMessage"]["ResponseHeader"]["Status"]
 status_code = response_status["StatusCode"]
 status_description = response_status["StatusDesc"]


  return  {
    type: 'Error',
    status: status_code,
    message: ERRORS_MESSAGES[:"#{status_code}"] || "#{status_code} #{status_description}",
  } unless response_status == { "StatusCode" => "0", "StatusDesc" => "SUCCESS" }

  response_any = response["ResponseMessage"]["ResponseBody"]["any"]
  success_id = response_any["unregisteredPaymentRS"]["transactionId"]

  {token: access_token, success_id: success_id, type: 'success', status: response.code, api_status: status_code, message: 'Payment request send success fully'}
end