Module: X402::Rails::ControllerExtensions

Extended by:
ActiveSupport::Concern
Defined in:
lib/x402/rails/controller_extensions.rb

Instance Method Summary collapse

Instance Method Details

#x402_payment_attempted?(version: nil) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/x402/rails/controller_extensions.rb', line 89

def x402_payment_attempted?(version: nil)
  x402_payment_header(version: version).present?
end

#x402_payment_header(version: nil) ⇒ Object

The raw payment header, or nil when absent (or when the version is invalid). Pass version: on endpoints that override it via x402_paywall(version:).



82
83
84
85
86
87
# File 'lib/x402/rails/controller_extensions.rb', line 82

def x402_payment_header(version: nil)
  version_strategy = X402::Versions.for(version || X402.configuration.version)
  request.headers[version_strategy.payment_header_name].presence
rescue X402::ConfigurationError
  nil
end

#x402_paywall(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
77
# File 'lib/x402/rails/controller_extensions.rb', line 38

def x402_paywall(options = {})
  amount = options[:amount] or raise ArgumentError, "amount is required"
  chain = options[:chain]
  currency = options[:currency]
  protocol_version = options[:version] || X402.configuration.version
  wallet_address = options[:wallet_address]
  fee_payer = options[:fee_payer]
  accepts = options[:accepts]
  @x402_paywall_extensions = options[:extensions]

  begin
    version_strategy = X402::Versions.for(protocol_version)
  rescue X402::ConfigurationError => e
    return render_configuration_error(e.message)
  end

  payment_header = request.headers[version_strategy.payment_header_name]

  if payment_header.nil? || payment_header.empty?
    return render_payment_required(
      amount,
      chain: chain,
      currency: currency,
      version: protocol_version,
      wallet_address: wallet_address,
      fee_payer: fee_payer,
      accepts: accepts
    )
  end

  process_payment(
    payment_header, amount,
    chain: chain,
    currency: currency,
    version: protocol_version,
    wallet_address: wallet_address,
    fee_payer: fee_payer,
    accepts: accepts
  )
end