-
Notifications
You must be signed in to change notification settings - Fork 103
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 in
- Loading branch information
1 parent
f575884
commit 0e3071f
Showing
14 changed files
with
824 additions
and
430 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
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 |
41 changes: 41 additions & 0 deletions
41
app/services/payment_providers/cashfree/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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
module Cashfree | ||
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::CashfreeCustomer.find_by(customer_id: customer.id) | ||
provider_customer ||= PaymentProviderCustomers::CashfreeCustomer.new(customer_id: customer.id, payment_provider_id:) | ||
|
||
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 | ||
|
||
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 | ||
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,22 @@ | ||
# 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 "cashfree" | ||
PaymentProviders::Cashfree::Customers::CreateService | ||
when "gocardless" | ||
PaymentProviders::Gocardless::Customers::CreateService | ||
when "stripe" | ||
PaymentProviders::Stripe::Customers::CreateService | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.