From e1ff5c479e2dff70105a0de1f91225bf39a56104 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Mon, 23 May 2022 18:49:55 +0200 Subject: [PATCH] Initial BTCPay integration --- .env.example | 1 + .env.production | 1 + app/services/btc_pay.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 app/services/btc_pay.rb diff --git a/.env.example b/.env.example index ed6e513..99cc4ad 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ EJABBERD_API_URL='https://xmpp.kosmos.org/api' +BTCPAY_API_URL='http://localhost:23001' LNDHUB_API_URL='http://localhost:3023' LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org' diff --git a/.env.production b/.env.production index e9bd695..8b61513 100644 --- a/.env.production +++ b/.env.production @@ -1,3 +1,4 @@ EJABBERD_API_URL='https://xmpp.kosmos.org:5443/api' +BTCPAY_API_URL='http://10.1.1.163:23001' LNDHUB_API_URL='http://10.1.1.163:3023' LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org' diff --git a/app/services/btc_pay.rb b/app/services/btc_pay.rb new file mode 100644 index 0000000..05c8d82 --- /dev/null +++ b/app/services/btc_pay.rb @@ -0,0 +1,32 @@ +# +# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/ +# +class BtcPay + def initialize + @base_url = "#{ENV["BTCPAY_API_URL"]}/api/v1" + @store_id = Rails.application.credentials.btcpay[:store_id] + @auth_token = Rails.application.credentials.btcpay[:auth_token] + end + + def onchain_wallet_balance + wallet_info = get "stores/#{@store_id}/payment-methods/onchain/BTC/wallet" + + { + balance: wallet_info["balance"].to_f, + unconfirmed_balance: wallet_info["unconfirmedBalance"].to_f, + confirmed_balance: wallet_info["confirmedBalance"].to_f + } + end + + private + + def get(endpoint) + res = Faraday.get("#{@base_url}/#{endpoint}", {}, { + "Content-Type" => "application/json", + "Accept" => "application/json", + "Authorization" => "token #{@auth_token}" + }) + + JSON.parse(res.body) + end +end