Class: X402::PaymentPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/x402/payment_payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ PaymentPayload

Returns a new instance of PaymentPayload.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/x402/payment_payload.rb', line 7

def initialize(attributes = {})
  attrs = attributes.with_indifferent_access

  @x402_version = (attrs[:x402Version] || attrs[:x402_version] || 1).to_i
  @accepted = attrs[:accepted]
  @resource_info = attrs[:resource]
  @extensions = attrs[:extensions]

  if @accepted
    @scheme = @accepted.with_indifferent_access[:scheme]
    @network = normalize_network(@accepted.with_indifferent_access[:network])
  else
    @scheme = attrs[:scheme]
    @network = normalize_network(attrs[:network])
  end

  @payload = attrs[:payload]
end

Instance Attribute Details

#acceptedObject

Returns the value of attribute accepted.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def accepted
  @accepted
end

#extensionsObject

Returns the value of attribute extensions.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def extensions
  @extensions
end

#networkObject

Returns the value of attribute network.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def network
  @network
end

#payloadObject

Returns the value of attribute payload.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def payload
  @payload
end

#resource_infoObject

Returns the value of attribute resource_info.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def resource_info
  @resource_info
end

#schemeObject

Returns the value of attribute scheme.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def scheme
  @scheme
end

#x402_versionObject

Returns the value of attribute x402_version.



5
6
7
# File 'lib/x402/payment_payload.rb', line 5

def x402_version
  @x402_version
end

Class Method Details

.from_header(header_value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/x402/payment_payload.rb', line 26

def self.from_header(header_value)
  return nil if header_value.nil? || header_value.empty?

  begin
    decoded = Base64.strict_decode64(header_value)
    json = JSON.parse(decoded)
    new(json)
  rescue StandardError => e
    raise InvalidPaymentError, "Failed to decode payment payload: #{e.message}"
  end
end

Instance Method Details

#authorizationObject



51
52
53
54
# File 'lib/x402/payment_payload.rb', line 51

def authorization
  return nil if solana_chain?
  @authorization ||= payload&.with_indifferent_access&.[](:authorization)
end

#evm_chain?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/x402/payment_payload.rb', line 42

def evm_chain?
  !solana_chain?
end

#from_addressObject



61
62
63
64
# File 'lib/x402/payment_payload.rb', line 61

def from_address
  return nil if solana_chain?
  authorization&.with_indifferent_access&.[](:from)
end

#nonceObject



86
87
88
89
# File 'lib/x402/payment_payload.rb', line 86

def nonce
  return nil if solana_chain?
  authorization&.with_indifferent_access&.[](:nonce)
end

#signatureObject



56
57
58
59
# File 'lib/x402/payment_payload.rb', line 56

def signature
  return nil if solana_chain?
  payload&.with_indifferent_access&.[](:signature)
end

#solana_chain?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/x402/payment_payload.rb', line 38

def solana_chain?
  X402.solana_chain?(network)
end

#to_addressObject



66
67
68
69
# File 'lib/x402/payment_payload.rb', line 66

def to_address
  return nil if solana_chain?
  authorization&.with_indifferent_access&.[](:to)
end

#to_hObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/x402/payment_payload.rb', line 91

def to_h
  version_strategy = X402::Versions.for(x402_version)

  if x402_version >= 2
    base = {
      x402Version: x402_version,
      accepted: format_accepted_for_version(version_strategy),
      payload: payload,
      # Client-echoed extensions (e.g. bazaar discovery) must survive to verify/settle
      extensions: extensions.presence || {}
    }
    base[:resource] = resource_info if resource_info
    base
  else
    {
      x402Version: x402_version,
      scheme: scheme,
      network: version_strategy.format_network(network),
      payload: payload
    }
  end
end

#to_json(*args) ⇒ Object



114
115
116
# File 'lib/x402/payment_payload.rb', line 114

def to_json(*args)
  to_h.to_json(*args)
end

#transactionObject



46
47
48
49
# File 'lib/x402/payment_payload.rb', line 46

def transaction
  return nil unless solana_chain?
  payload&.with_indifferent_access&.[](:transaction)
end

#valid_afterObject



76
77
78
79
# File 'lib/x402/payment_payload.rb', line 76

def valid_after
  return nil if solana_chain?
  authorization&.with_indifferent_access&.[](:validAfter) || authorization&.with_indifferent_access&.[](:valid_after)
end

#valid_beforeObject



81
82
83
84
# File 'lib/x402/payment_payload.rb', line 81

def valid_before
  return nil if solana_chain?
  authorization&.with_indifferent_access&.[](:validBefore) || authorization&.with_indifferent_access&.[](:valid_before)
end

#valueObject



71
72
73
74
# File 'lib/x402/payment_payload.rb', line 71

def value
  return nil if solana_chain?
  authorization&.with_indifferent_access&.[](:value)
end