Class: X402::PaymentPayload
- Inherits:
-
Object
- Object
- X402::PaymentPayload
- Defined in:
- lib/x402/payment_payload.rb
Instance Attribute Summary collapse
-
#accepted ⇒ Object
Returns the value of attribute accepted.
-
#extensions ⇒ Object
Returns the value of attribute extensions.
-
#network ⇒ Object
Returns the value of attribute network.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#resource_info ⇒ Object
Returns the value of attribute resource_info.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#x402_version ⇒ Object
Returns the value of attribute x402_version.
Class Method Summary collapse
Instance Method Summary collapse
- #authorization ⇒ Object
- #evm_chain? ⇒ Boolean
- #from_address ⇒ Object
-
#initialize(attributes = {}) ⇒ PaymentPayload
constructor
A new instance of PaymentPayload.
- #nonce ⇒ Object
- #signature ⇒ Object
- #solana_chain? ⇒ Boolean
- #to_address ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #transaction ⇒ Object
- #valid_after ⇒ Object
- #valid_before ⇒ Object
- #value ⇒ Object
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
#accepted ⇒ Object
Returns the value of attribute accepted.
5 6 7 |
# File 'lib/x402/payment_payload.rb', line 5 def accepted @accepted end |
#extensions ⇒ Object
Returns the value of attribute extensions.
5 6 7 |
# File 'lib/x402/payment_payload.rb', line 5 def extensions @extensions end |
#network ⇒ Object
Returns the value of attribute network.
5 6 7 |
# File 'lib/x402/payment_payload.rb', line 5 def network @network end |
#payload ⇒ Object
Returns the value of attribute payload.
5 6 7 |
# File 'lib/x402/payment_payload.rb', line 5 def payload @payload end |
#resource_info ⇒ Object
Returns the value of attribute resource_info.
5 6 7 |
# File 'lib/x402/payment_payload.rb', line 5 def resource_info @resource_info end |
#scheme ⇒ Object
Returns the value of attribute scheme.
5 6 7 |
# File 'lib/x402/payment_payload.rb', line 5 def scheme @scheme end |
#x402_version ⇒ Object
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.}" end end |
Instance Method Details
#authorization ⇒ Object
51 52 53 54 |
# File 'lib/x402/payment_payload.rb', line 51 def return nil if solana_chain? @authorization ||= payload&.with_indifferent_access&.[](:authorization) end |
#evm_chain? ⇒ Boolean
42 43 44 |
# File 'lib/x402/payment_payload.rb', line 42 def evm_chain? !solana_chain? end |
#from_address ⇒ Object
61 62 63 64 |
# File 'lib/x402/payment_payload.rb', line 61 def from_address return nil if solana_chain? &.with_indifferent_access&.[](:from) end |
#nonce ⇒ Object
86 87 88 89 |
# File 'lib/x402/payment_payload.rb', line 86 def nonce return nil if solana_chain? &.with_indifferent_access&.[](:nonce) end |
#signature ⇒ Object
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
38 39 40 |
# File 'lib/x402/payment_payload.rb', line 38 def solana_chain? X402.solana_chain?(network) end |
#to_address ⇒ Object
66 67 68 69 |
# File 'lib/x402/payment_payload.rb', line 66 def to_address return nil if solana_chain? &.with_indifferent_access&.[](:to) end |
#to_h ⇒ Object
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 |
#transaction ⇒ Object
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_after ⇒ Object
76 77 78 79 |
# File 'lib/x402/payment_payload.rb', line 76 def valid_after return nil if solana_chain? &.with_indifferent_access&.[](:validAfter) || &.with_indifferent_access&.[](:valid_after) end |
#valid_before ⇒ Object
81 82 83 84 |
# File 'lib/x402/payment_payload.rb', line 81 def valid_before return nil if solana_chain? &.with_indifferent_access&.[](:validBefore) || &.with_indifferent_access&.[](:valid_before) end |
#value ⇒ Object
71 72 73 74 |
# File 'lib/x402/payment_payload.rb', line 71 def value return nil if solana_chain? &.with_indifferent_access&.[](:value) end |