Set member status to sustainer upon payment
Introduces a state machine for the payment status as well. refs #213
This commit is contained in:
@@ -11,7 +11,7 @@ class Contributions::DonationsController < ApplicationController
|
||||
def index
|
||||
@current_section = :contributions
|
||||
@donations_completed = current_user.donations.completed.order('paid_at desc')
|
||||
@donations_pending = current_user.donations.processing.order('created_at desc')
|
||||
@donations_processing = current_user.donations.processing.order('created_at desc')
|
||||
|
||||
if Setting.lndhub_enabled?
|
||||
begin
|
||||
@@ -81,14 +81,11 @@ class Contributions::DonationsController < ApplicationController
|
||||
|
||||
case invoice["status"]
|
||||
when "Settled"
|
||||
@donation.paid_at = DateTime.now
|
||||
@donation.payment_status = "settled"
|
||||
@donation.save!
|
||||
@donation.complete!
|
||||
flash_message = { success: "Thank you!" }
|
||||
when "Processing"
|
||||
unless @donation.processing?
|
||||
@donation.payment_status = "processing"
|
||||
@donation.save!
|
||||
@donation.start_processing!
|
||||
flash_message = { success: "Thank you! We will send you an email when the payment is confirmed." }
|
||||
BtcpayCheckDonationJob.set(wait: 20.seconds).perform_later(@donation)
|
||||
end
|
||||
|
||||
@@ -10,9 +10,7 @@ class BtcpayCheckDonationJob < ApplicationJob
|
||||
|
||||
case invoice["status"]
|
||||
when "Settled"
|
||||
donation.paid_at = DateTime.now
|
||||
donation.payment_status = "settled"
|
||||
donation.save!
|
||||
donation.complete!
|
||||
|
||||
NotificationMailer.with(user: donation.user)
|
||||
.bitcoin_donation_confirmed
|
||||
|
||||
@@ -1,22 +1,41 @@
|
||||
class Donation < ApplicationRecord
|
||||
# Relations
|
||||
include AASM
|
||||
|
||||
belongs_to :user
|
||||
|
||||
# Validations
|
||||
validates_presence_of :user
|
||||
validates_presence_of :donation_method,
|
||||
inclusion: { in: %w[ custom btcpay lndhub ] }
|
||||
validates_presence_of :payment_status, allow_nil: true,
|
||||
inclusion: { in: %w[ processing settled ] }
|
||||
inclusion: { in: %w[ pending processing settled ] }
|
||||
validates_presence_of :paid_at, allow_nil: true
|
||||
validates_presence_of :amount_sats, allow_nil: true
|
||||
validates_presence_of :fiat_amount, allow_nil: true
|
||||
validates_presence_of :fiat_currency, allow_nil: true,
|
||||
inclusion: { in: %w[ EUR USD ] }
|
||||
|
||||
#Scopes
|
||||
scope :pending, -> { where(payment_status: "pending") }
|
||||
scope :processing, -> { where(payment_status: "processing") }
|
||||
scope :completed, -> { where(payment_status: "settled") }
|
||||
scope :completed, -> { where(payment_status: "settled") }
|
||||
|
||||
aasm column: :payment_status do
|
||||
state :pending, initial: true
|
||||
state :processing
|
||||
state :settled
|
||||
|
||||
event :start_processing do
|
||||
transitions from: :pending, to: :processing
|
||||
end
|
||||
|
||||
event :complete do
|
||||
transitions from: :processing, to: :settled, after: [:set_paid_at, :set_sustainer_status]
|
||||
transitions from: :pending, to: :settled, after: [:set_paid_at, :set_sustainer_status]
|
||||
end
|
||||
end
|
||||
|
||||
def pending?
|
||||
payment_status == "pending"
|
||||
end
|
||||
|
||||
def processing?
|
||||
payment_status == "processing"
|
||||
@@ -25,4 +44,17 @@ class Donation < ApplicationRecord
|
||||
def completed?
|
||||
payment_status == "settled"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_paid_at
|
||||
update paid_at: DateTime.now if paid_at.nil?
|
||||
end
|
||||
|
||||
def set_sustainer_status
|
||||
user.add_member_status :sustainer
|
||||
rescue => e
|
||||
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
||||
Rails.logger.error("Failed to set memberStatus: #{e.message}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,11 +22,11 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<% if @donations_pending.any? %>
|
||||
<% if @donations_processing.any? %>
|
||||
<section class="donation-list">
|
||||
<h2>Pending</h2>
|
||||
<%= render partial: "contributions/donations/list",
|
||||
locals: { donations: @donations_pending } %>
|
||||
locals: { donations: @donations_processing } %>
|
||||
</section>
|
||||
<% end %>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user