Class: NodexPay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nodex_pay/client.rb,
sig/nodex_pay.rbs

Constant Summary collapse

BASE_URLS =

Returns:

  • (Hash[Symbol, String])
{
  production: "https://app.nodexpay.com/api/v1",
  testnet: "https://testnet.app.nodexpay.com/api/v1"
}.freeze
DEFAULT_OPEN_TIMEOUT =

Returns:

  • (Integer)
5
DEFAULT_READ_TIMEOUT =

Returns:

  • (Integer)
30
DEFAULT_WRITE_TIMEOUT =

Returns:

  • (Integer)
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identification_token:, hash_token:, environment:, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT, transport: nil) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nodex_pay/client.rb', line 19

def initialize(
  identification_token:,
  hash_token:,
  environment:,
  open_timeout: DEFAULT_OPEN_TIMEOUT,
  read_timeout: DEFAULT_READ_TIMEOUT,
  write_timeout: DEFAULT_WRITE_TIMEOUT,
  transport: nil
)
  unless BASE_URLS.key?(environment)
    raise ConfigurationError, "environment must be :production or :testnet"
  end

  @identification_token = Validator.credential(identification_token, "identification_token")
  @hash_token = Validator.credential(hash_token, "hash_token")
  @environment = environment
  @open_timeout = Validator.timeout(open_timeout, "open_timeout")
  @read_timeout = Validator.timeout(read_timeout, "read_timeout")
  @write_timeout = Validator.timeout(write_timeout, "write_timeout")
  @transport = transport || Transport::NetHTTP.new

  unless @transport.respond_to?(:call)
    raise ConfigurationError, "transport must respond to #call"
  end

  freeze
end

Instance Attribute Details

#environment:production, :testnet (readonly)

Returns the value of attribute environment.

Returns:

  • (:production, :testnet)


17
18
19
# File 'lib/nodex_pay/client.rb', line 17

def environment
  @environment
end

#open_timeoutNumeric (readonly)

Returns the value of attribute open_timeout.

Returns:

  • (Numeric)


17
18
19
# File 'lib/nodex_pay/client.rb', line 17

def open_timeout
  @open_timeout
end

#read_timeoutNumeric (readonly)

Returns the value of attribute read_timeout.

Returns:

  • (Numeric)


17
18
19
# File 'lib/nodex_pay/client.rb', line 17

def read_timeout
  @read_timeout
end

#write_timeoutNumeric (readonly)

Returns the value of attribute write_timeout.

Returns:

  • (Numeric)


17
18
19
# File 'lib/nodex_pay/client.rb', line 17

def write_timeout
  @write_timeout
end

Instance Method Details

#cancel_payment(order_code:) ⇒ true

Parameters:

  • order_code: (String)

Returns:

  • (true)


73
74
75
76
77
78
79
# File 'lib/nodex_pay/client.rb', line 73

def cancel_payment(order_code:)
  order_code = Validator.order_code(order_code)
  fields = authenticated_fields(order_code:, amount: nil)

  perform(:cancel_payment, "/payment/cancel", fields)
  true
end

#create_payment(order_code:, amount: nil, amount_type: nil, color_theme: nil, ui_mode: nil, ext_description: nil, deadline: nil, description: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nodex_pay/client.rb', line 47

def create_payment(
  order_code:,
  amount: nil,
  amount_type: nil,
  color_theme: nil,
  ui_mode: nil,
  ext_description: nil,
  deadline: nil,
  description: nil
)
  order_code = Validator.order_code(order_code)
  amount = AmountSerializer.serialize(amount) unless amount.nil?

  fields = authenticated_fields(order_code:, amount:)
  fields["amount"] = amount unless amount.nil?
  add_optional_field(fields, "amount_type", Validator.amount_type(amount_type))
  add_optional_field(fields, "color_theme", Validator.color_theme(color_theme))
  add_optional_field(fields, "uimode", Validator.ui_mode(ui_mode))
  add_optional_field(fields, "ext_description", Validator.text(ext_description, "ext_description"))
  add_optional_field(fields, "deadline", Validator.deadline(deadline))
  add_optional_field(fields, "description", Validator.text(description, "description"))

  response = perform(:create_payment, "/payment/receive", fields)
  decode_payment(response.body)
end

#inspectObject



111
112
113
# File 'lib/nodex_pay/client.rb', line 111

def inspect
  "#<#{self.class} environment=#{environment.inspect}>"
end

#verify_payment_result(raw_body) ⇒ PaymentResult

Parameters:

  • raw_body (String)

Returns:



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
# File 'lib/nodex_pay/client.rb', line 81

def verify_payment_result(raw_body)
  unless raw_body.is_a?(String)
    raise InvalidResponseError, "Payment Result body must be a String"
  end

  payload = parse_json_object(raw_body, context: "Payment Result")
  order_code = Validator.webhook_string(payload, "order_code")
  transaction_code = Validator.transaction_code(payload)
  amount = Validator.webhook_string(payload, "amount")
  symbol = Validator.webhook_string(payload, "symbol")
  result = Validator.webhook_result(payload)
  verify_token = Validator.verify_token(payload)
  chain_id = Validator.webhook_string(payload, "chain_id")
  transaction_hash = Validator.webhook_string(payload, "transaction_hash")

  unless Signer.valid?(order_code:, amount:, hash_token: @hash_token, signature: verify_token)
    raise SignatureVerificationError, "Payment Result signature verification failed"
  end

  PaymentResult.new(
    order_code:,
    transaction_code:,
    amount:,
    symbol:,
    result:,
    chain_id:,
    transaction_hash:
  )
end