Class: Mpp::Server::ComposeOffer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options)
  @handler = T.let(handler, T.untyped)
  @method = T.let(method, T.untyped)
  @options = T.let(symbolize(options), 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

#methodObject (readonly)

Returns the value of attribute method.



16
17
18
# File 'lib/mpp/server/compose.rb', line 16

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/mpp/server/compose.rb', line 19

def options
  @options
end

Instance Method Details

#canonical_requestObject



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_nameObject



31
32
33
# File 'lib/mpp/server/compose.rb', line 31

def intent_name
  "charge"
end

#keyObject



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,
    authorization,
    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

Returns:

  • (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