mirror of
https://github.com/bumi/faraday_ln_paywall
synced 2025-06-16 17:25:35 +00:00
Add exception handling
A PaymentError is raised if something goes wrong. if: the invoice amount is too high the invoice is expired, the payment takes too long (default 5 seconds)
This commit is contained in:
parent
c38acdec53
commit
304b361b3f
@ -4,12 +4,15 @@ require 'lightning/invoice'
|
|||||||
ENV['GRPC_SSL_CIPHER_SUITES'] = "HIGH+ECDSA"
|
ENV['GRPC_SSL_CIPHER_SUITES'] = "HIGH+ECDSA"
|
||||||
|
|
||||||
module FaradayLnPaywall
|
module FaradayLnPaywall
|
||||||
|
class PaymentError < StandardError; end
|
||||||
|
|
||||||
class Middleware < Faraday::Middleware
|
class Middleware < Faraday::Middleware
|
||||||
|
|
||||||
def initialize(app, options = {})
|
def initialize(app, options = {})
|
||||||
super(app)
|
super(app)
|
||||||
@options = options
|
@options = options
|
||||||
@options[:address] ||= 'localhost:10009'
|
@options[:address] ||= 'localhost:10009'
|
||||||
|
@options[:timeout] ||= 5
|
||||||
@options[:credentials] ||= File.read(File.expand_path(@options[:credentials_path] || "~/.lnd/tls.cert"))
|
@options[:credentials] ||= File.read(File.expand_path(@options[:credentials_path] || "~/.lnd/tls.cert"))
|
||||||
macaroon_binary ||= File.read(File.expand_path(@options[:macaroon_path] || "~/.lnd/data/chain/bitcoin/testnet/admin.macaroon"))
|
macaroon_binary ||= File.read(File.expand_path(@options[:macaroon_path] || "~/.lnd/data/chain/bitcoin/testnet/admin.macaroon"))
|
||||||
@options[:macaroon] = macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
|
@options[:macaroon] = macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
|
||||||
@ -20,18 +23,25 @@ module FaradayLnPaywall
|
|||||||
env[:status] == 402 && env[:response_headers]['Content-Type'] == "application/vnd.lightning.bolt11"
|
env[:status] == 402 && env[:response_headers]['Content-Type'] == "application/vnd.lightning.bolt11"
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoice_valid?(invoice)
|
def validate_invoice!(invoice)
|
||||||
(@options[:max_amount].nil? || @options[:max_amount] > invoice.amount) &&
|
if !@options[:max_amount].nil? && @options[:max_amount] < invoice.amount
|
||||||
invoice.expiry.nil? || Time.now.to_i < invoice.timestamp + invoice.expiry.to_i
|
raise PaymentError, "invoice amount greater than expected maximum of #{@options[:max_amount]}"
|
||||||
|
end
|
||||||
|
if !invoice.expiry.nil? && Time.now.to_i > invoice.timestamp + invoice.expiry.to_i
|
||||||
|
raise PaymentError, "invoice expired"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pay(env)
|
def pay(env)
|
||||||
invoice = Lightning::Invoice.parse(env.body)
|
invoice = Lightning::Invoice.parse(env.body)
|
||||||
return false unless invoice_valid?(invoice)
|
validate_invoice!(invoice)
|
||||||
@lnd_client.send_payment_sync(
|
|
||||||
Lnrpc::SendRequest.new(payment_request: env.body),
|
Timeout::timeout(@options[:timeout], PaymentError, "payment execution expired") do
|
||||||
{ metadata: { macaroon: @options[:macaroon] }}
|
@lnd_client.send_payment_sync(
|
||||||
)
|
Lnrpc::SendRequest.new(payment_request: env.body),
|
||||||
|
{ metadata: { macaroon: @options[:macaroon] }}
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(request_env)
|
def call(request_env)
|
||||||
@ -43,7 +53,7 @@ module FaradayLnPaywall
|
|||||||
request_env[:request_headers].merge!({'X-Preimage' => preimage})
|
request_env[:request_headers].merge!({'X-Preimage' => preimage})
|
||||||
@app.call(request_env)
|
@app.call(request_env)
|
||||||
else
|
else
|
||||||
# payment failed
|
raise PaymentError, payment.payment_error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user