Let users donate sats via BTCPay Server #176

Merged
raucao merged 9 commits from feature/donations_btcpay into master 2024-03-13 16:31:54 +00:00
4 changed files with 45 additions and 7 deletions
Showing only changes of commit 54220019bb - Show all commits

View File

@@ -4,15 +4,19 @@ class BtcpayCheckDonationJob < ApplicationJob
def perform(donation)
return if donation.completed?
invoice = BtcpayManager::FetchInvoice.call(invoice_id: donation.btcpay_invoice_id)
invoice = BtcpayManager::FetchInvoice.call(
invoice_id: donation.btcpay_invoice_id
)
case invoice["status"]
when "Settled"
# TODO use time from actual payment confirmation
donation.paid_at = DateTime.now
donation.payment_status = "settled"
donation.save!
# TODO send email
NotificationMailer.with(user: donation.user)
.bitcoin_donation_confirmed
.deliver_later
when "Processing"
re_enqueue_job(donation)
end

View File

@@ -23,4 +23,11 @@ class NotificationMailer < ApplicationMailer
@subject = "New invitations added to your account"
mail to: @user.email, subject: @subject
end
def bitcoin_donation_confirmed
@user = params[:user]
@donation = params[:donation]
@subject = "Donation confirmed"
mail to: @user.email, subject: @subject
end
end

View File

@@ -0,0 +1,11 @@
Hi <%= @user.display_name.presence || @user.cn %>,
Your bitcoin donation has been confirmed successfully. <3
Thank you so much for helping us with keeping the lights on, as well as with continually improving our services for you!
You can find all of your past financial contributions on this page:
<%= contributions_donations_url %>
Have a nice day!

View File

@@ -6,12 +6,20 @@ RSpec.describe BtcpayCheckDonationJob, type: :job do
let(:donation) do
user.donations.create!(
donation_method: "btcpay", btcpay_invoice_id: "K4e31MhbLKmr3D7qoNYRd3",
donation_method: "btcpay",
btcpay_invoice_id: "K4e31MhbLKmr3D7qoNYRd3",
paid_at: nil, payment_status: "processing",
fiat_amount: 120, fiat_currency: "USD"
)
end
before do
allow_any_instance_of(User).to receive(:ldap_entry).and_return({
uid: user.cn, ou: user.ou, mail: user.email, admin: nil,
display_name: nil
})
end
after(:each) do
clear_enqueued_jobs
clear_performed_jobs
@@ -48,16 +56,24 @@ RSpec.describe BtcpayCheckDonationJob, type: :job do
end
it "updates the donation record" do
perform_enqueued_jobs { job }
perform_enqueued_jobs(only: described_class) { job }
donation.reload
expect(donation.paid_at).not_to be_nil
expect(donation.payment_status).to eq("settled")
end
it "notifies the user via email" do
perform_enqueued_jobs(only: described_class) { job }
expect(enqueued_jobs.size).to eq(1)
job = enqueued_jobs.select{|j| j['job_class'] == "ActionMailer::MailDeliveryJob"}.first
expect(job['arguments'][0]).to eq('NotificationMailer')
expect(job['arguments'][1]).to eq('bitcoin_donation_confirmed')
expect(job['arguments'][3]['params']['user']['_aj_globalid']).to eq('gid://akkounts/User/1')
end
it "does not enqueue itself again" do
expect_any_instance_of(described_class).not_to receive(:re_enqueue_job)
perform_enqueued_jobs { job }
perform_enqueued_jobs(only: described_class) { job }
end
end
end