Class: Mpp::Server::ComposeOffer
- Inherits:
-
Object
- Object
- Mpp::Server::ComposeOffer
- Extended by:
- T::Sig
- Defined in:
- lib/mpp/server/compose.rb
Overview
A configured payment offer: a method plus per-request charge options.
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #canonical_request ⇒ Object
- #challenge(body: nil, url: nil, http_method: nil) ⇒ Object
- #decorate_challenge(headers, challenge, url: nil, http_method: nil) ⇒ Object
- #decorate_receipt(headers, credential, receipt, payment_signature: nil) ⇒ Object
-
#initialize(handler, method, options) ⇒ ComposeOffer
constructor
A new instance of ComposeOffer.
- #intent_name ⇒ Object
- #key ⇒ Object
- #verify(authorization: nil, payment_signature: nil, body: nil, url: nil, http_method: nil) ⇒ Object
- #with_scope(scope) ⇒ Object
- #x402_matches?(payload) ⇒ Boolean
Constructor Details
#initialize(handler, method, options) ⇒ ComposeOffer
Returns a new instance of ComposeOffer.
22 23 24 25 26 27 28 |
# File 'lib/mpp/server/compose.rb', line 22 def initialize(handler, method, ) @handler = T.let(handler, T.untyped) @method = T.let(method, T.untyped) @options = T.let(symbolize(), T::Hash[Symbol, T.untyped]) amount = @options[:amount] Kernel.raise ArgumentError, "compose entry requires amount:" if amount.nil? || amount.to_s.empty? end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
16 17 18 |
# File 'lib/mpp/server/compose.rb', line 16 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/mpp/server/compose.rb', line 19 def @options end |
Instance Method Details
#canonical_request ⇒ Object
41 42 43 |
# File 'lib/mpp/server/compose.rb', line 41 def canonical_request @canonical_request ||= @handler.build_charge_request(@method, amount, **charge_kwargs) end |
#challenge(body: nil, url: nil, http_method: nil) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/mpp/server/compose.rb', line 60 def challenge(body: nil, url: nil, http_method: nil) result = verify(body: body, url: url, http_method: http_method) return result if result.is_a?(Mpp::Challenge) Kernel.raise "expected a challenge from unpaid offer #{key}" end |
#decorate_challenge(headers, challenge, url: nil, http_method: nil) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/mpp/server/compose.rb', line 68 def decorate_challenge(headers, challenge, url: nil, http_method: nil) return headers unless @method.respond_to?(:decorate_challenge) @method.decorate_challenge(headers, challenge, url: url, http_method: http_method, request: canonical_request) headers end |
#decorate_receipt(headers, credential, receipt, payment_signature: nil) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/mpp/server/compose.rb', line 76 def decorate_receipt(headers, credential, receipt, payment_signature: nil) return headers unless payment_signature && @method.respond_to?(:decorate_receipt) @method.decorate_receipt(headers, receipt, credential, payment_signature: payment_signature) headers end |
#intent_name ⇒ Object
31 32 33 |
# File 'lib/mpp/server/compose.rb', line 31 def intent_name "charge" end |
#key ⇒ Object
36 37 38 |
# File 'lib/mpp/server/compose.rb', line 36 def key "#{@method.name}/#{intent_name}" end |
#verify(authorization: nil, payment_signature: nil, body: nil, url: nil, http_method: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mpp/server/compose.rb', line 46 def verify(authorization: nil, payment_signature: nil, body: nil, url: nil, http_method: nil) @handler.charge_one( @method, , amount, payment_signature: payment_signature, url: url, body: body, http_method: http_method, **charge_kwargs ) end |
#with_scope(scope) ⇒ Object
92 93 94 |
# File 'lib/mpp/server/compose.rb', line 92 def with_scope(scope) ComposeOffer.new(@handler, @method, @options.merge(mppx_scope: @options[:mppx_scope] || scope.dup)) end |
#x402_matches?(payload) ⇒ Boolean
84 85 86 87 88 |
# File 'lib/mpp/server/compose.rb', line 84 def x402_matches?(payload) return false unless @method.respond_to?(:x402_matches?) @method.x402_matches?(payload, canonical_request) end |