Attempt to add a credit card before processing payment

This commit is contained in:
Rob Harrington
2017-11-10 11:36:19 +11:00
parent c20c9e2e78
commit dfe874cf3f
2 changed files with 123 additions and 11 deletions

View File

@@ -1,12 +1,16 @@
module OpenFoodNetwork
class StandingOrderPaymentUpdater
def initialize(order)
@order = order
end
def update!
return unless payment
return if payment.blank?
if card_required? && !card_set?
return unless ensure_credit_card
end
payment.update_attributes(amount: @order.outstanding_balance)
end
@@ -15,5 +19,22 @@ module OpenFoodNetwork
def payment
@payment ||= @order.pending_payments.last
end
def card_required?
payment.payment_method.is_a? Spree::Gateway::StripeConnect
end
def card_set?
payment.source is_a? Spree::CreditCard
end
def ensure_credit_card
return false unless saved_credit_card.present?
payment.update_attributes(source: saved_credit_card)
end
def saved_credit_card
@order.standing_order.credit_card
end
end
end