Implement bitcoin donations via BTCPay
This commit is contained in:
21
app/services/btcpay_manager/create_invoice.rb
Normal file
21
app/services/btcpay_manager/create_invoice.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module BtcpayManager
|
||||
class CreateInvoice < BtcpayManagerService
|
||||
def initialize(amount:, currency:, redirect_url:)
|
||||
@amount = amount
|
||||
@currency = currency
|
||||
@redirect_url = redirect_url
|
||||
end
|
||||
|
||||
def call
|
||||
post "/invoices", {
|
||||
amount: @amount.to_s,
|
||||
currency: @currency,
|
||||
checkout: {
|
||||
redirectURL: @redirect_url,
|
||||
redirectAutomatically: true,
|
||||
requiresRefundEmail: false
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
14
app/services/btcpay_manager/fetch_exchange_rate.rb
Normal file
14
app/services/btcpay_manager/fetch_exchange_rate.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module BtcpayManager
|
||||
class FetchExchangeRate < BtcpayManagerService
|
||||
def initialize(fiat_currency:)
|
||||
@fiat_currency = fiat_currency
|
||||
end
|
||||
|
||||
def call
|
||||
pair_str = "BTC_#{@fiat_currency}"
|
||||
res = get "rates", { currencyPair: pair_str }
|
||||
pair = res.find{|p| p["currencyPair"] == pair_str }
|
||||
rate = pair["rate"].to_f
|
||||
end
|
||||
end
|
||||
end
|
||||
14
app/services/btcpay_manager/fetch_invoice.rb
Normal file
14
app/services/btcpay_manager/fetch_invoice.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module BtcpayManager
|
||||
class FetchInvoice < BtcpayManagerService
|
||||
def initialize(invoice_id:)
|
||||
@invoice_id = invoice_id
|
||||
end
|
||||
|
||||
def call
|
||||
invoice = get "/invoices/#{@invoice_id}"
|
||||
payment_methods = get "/invoices/#{@invoice_id}/payment-methods"
|
||||
invoice["paymentMethods"] = payment_methods
|
||||
invoice
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -24,8 +24,8 @@ class BtcpayManagerService < ApplicationService
|
||||
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||
end
|
||||
|
||||
def get(path)
|
||||
res = Faraday.get endpoint_url(path), {}, headers
|
||||
def get(path, params = {})
|
||||
res = Faraday.get endpoint_url(path), params, headers
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user