-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc(stripe): Refact payment method update webhooks
- Loading branch information
1 parent
f564be8
commit d12a15b
Showing
32 changed files
with
615 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/services/payment_provider_customers/stripe/update_payment_method_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_String_literal: true | ||
|
||
module PaymentProviderCustomers | ||
module Stripe | ||
class UpdatePaymentMethodService < BaseService | ||
def initialize(stripe_customer:, payment_method_id:) | ||
@stripe_customer = stripe_customer | ||
@payment_method_id = payment_method_id | ||
|
||
super | ||
end | ||
|
||
def call | ||
return result.not_found_failure!(resource: 'stripe_customer') unless stripe_customer | ||
|
||
stripe_customer.payment_method_id = payment_method_id | ||
stripe_customer.save! | ||
|
||
reprocess_pending_invoices | ||
|
||
result.stripe_customer = stripe_customer | ||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :stripe_customer, :payment_method_id | ||
|
||
delegate :customer, to: :stripe_customer | ||
|
||
def reprocess_pending_invoices | ||
invoices = customer.invoices | ||
.payment_pending | ||
.where(ready_for_payment_processing: true) | ||
.where(status: 'finalized') | ||
|
||
invoices.find_each do |invoice| | ||
Invoices::Payments::StripeCreateJob.perform_later(invoice) | ||
rescue ActiveJob::Uniqueness::JobNotUnique | ||
# NOTE: Payment is already enqueued for processing | ||
Rails.logger.warn("Duplicated payment attempt for invoice #{invoice.id}") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.