-
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(payment_providers): Split customer creation by providers (#2896)
## Context The `PaymentProviderCustomers::CreateService` is now too complex and the logic to handle specific logic for the payment providers is splitted between this service and the `Customers::CreateService` and `Customers::UpdateService` making it hard to understand, maintain and extand (see the Cashfree provider PR as an example) ## Description This PR propose a new approach to handle provider related logic, that will be extended in the future to all interactions with the providers: - Move all the logic into `/app/services/payment_providers/_PROVIDER_NAME_/_LAGO_RESOURCE_/__ACTION_service.rb` (example: `/app/services/payment_providers/stripe/customers/create_service.rb`) - Add a dedicated factory responsible for initializing the instance of the right payment provider service
- Loading branch information
1 parent
41cdc73
commit 35d4b3c
Showing
13 changed files
with
741 additions
and
505 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
101 changes: 0 additions & 101 deletions
101
app/services/payment_provider_customers/create_service.rb
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
app/services/payment_providers/adyen/customers/create_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,79 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
module Adyen | ||
module Customers | ||
class CreateService < BaseService | ||
def initialize(customer:, payment_provider_id:, params:, async: true) | ||
@customer = customer | ||
@payment_provider_id = payment_provider_id | ||
@params = params || {} | ||
@async = async | ||
|
||
super | ||
end | ||
|
||
def call | ||
provider_customer = PaymentProviderCustomers::AdyenCustomer.find_by(customer_id: customer.id) | ||
provider_customer ||= PaymentProviderCustomers::AdyenCustomer.new(customer_id: customer.id, payment_provider_id:) | ||
|
||
if params.key?(:provider_customer_id) | ||
provider_customer.provider_customer_id = params[:provider_customer_id].presence | ||
end | ||
|
||
if params.key?(:sync_with_provider) | ||
provider_customer.sync_with_provider = params[:sync_with_provider].presence | ||
end | ||
|
||
provider_customer.save! | ||
|
||
result.provider_customer = provider_customer | ||
|
||
if should_create_provider_customer? | ||
create_customer_on_provider_service(async) | ||
elsif should_generate_checkout_url? | ||
generate_checkout_url(async) | ||
end | ||
|
||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
end | ||
|
||
private | ||
|
||
attr_accessor :customer, :payment_provider_id, :params, :async | ||
|
||
delegate :organization, to: :customer | ||
|
||
def create_customer_on_provider_service(async) | ||
return PaymentProviderCustomers::AdyenCreateJob.perform_later(result.provider_customer) if async | ||
|
||
PaymentProviderCustomers::AdyenCreateJob.perform_now(result.provider_customer) | ||
end | ||
|
||
def generate_checkout_url(async) | ||
return PaymentProviderCustomers::AdyenCheckoutUrlJob.perform_later(result.provider_customer) if async | ||
|
||
PaymentProviderCustomers::AdyenCheckoutUrlJob.perform_now(result.provider_customer) | ||
end | ||
|
||
def should_create_provider_customer? | ||
# NOTE: the customer does not exists on the service provider | ||
# and the customer id was not removed from the customer | ||
# customer sync with provider setting is set to true | ||
!result.provider_customer.provider_customer_id? && | ||
!result.provider_customer.provider_customer_id_previously_changed? && | ||
result.provider_customer.sync_with_provider.present? | ||
end | ||
|
||
def should_generate_checkout_url? | ||
!result.provider_customer.id_previously_changed?(from: nil) && # it was not created but updated | ||
result.provider_customer.provider_customer_id_previously_changed? && | ||
result.provider_customer.provider_customer_id? && | ||
result.provider_customer.sync_with_provider.blank? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
class CreateCustomerFactory | ||
def self.new_instance(provider:, customer:, payment_provider_id:, params:, async: true) | ||
service_class(provider:).new(customer:, payment_provider_id:, params:, async:) | ||
end | ||
|
||
def self.service_class(provider:) | ||
case provider | ||
when "adyen" | ||
PaymentProviders::Adyen::Customers::CreateService | ||
when "gocardless" | ||
PaymentProviders::Gocardless::Customers::CreateService | ||
when "stripe" | ||
PaymentProviders::Stripe::Customers::CreateService | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.