Skip to content

Commit

Permalink
misc(payment_request): Apply status refact
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Dec 23, 2024
1 parent a0c3d16 commit ec98ed4
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 91 deletions.
2 changes: 1 addition & 1 deletion app/jobs/invoices/payments/adyen_create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AdyenCreateJob < ApplicationJob
retry_on Faraday::ConnectionFailed, wait: :polynomially_longer, attempts: 6

def perform(invoice)
# NOTE: Legacy job, kept only to avoid faileure with existing jobs
# NOTE: Legacy job, kept only to avoid failure with existing jobs

Invoices::Payments::CreateService.call!(invoice:, payment_provider: :adyen)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/payment_providers/adyen_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PaymentProviders
class AdyenProvider < BaseProvider
SUCCESS_REDIRECT_URL = 'https://www.adyen.com/'

PENDING_STATUSES = %w[AuthorisedPending Received].freeze
PROCESSING_STATUSES = %w[AuthorisedPending Received].freeze
SUCCESS_STATUSES = %w[Authorised SentForSettle SettleScheduled Settled Refunded].freeze
FAILED_STATUSES = %w[Cancelled CaptureFailed Error Expired Refused].freeze

Expand Down
2 changes: 1 addition & 1 deletion app/models/payment_providers/base_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BaseProvider < ApplicationRecord
settings_accessors :webhook_secret, :success_redirect_url

def determine_payment_status(payment_status)
return :pending if self.class::PENDING_STATUSES.include?(payment_status)
return :processing if self.class::PROCESSING_STATUSES.include?(payment_status)
return :succeeded if self.class::SUCCESS_STATUSES.include?(payment_status)
return :failed if self.class::FAILED_STATUSES.include?(payment_status)

Expand Down
2 changes: 1 addition & 1 deletion app/models/payment_providers/gocardless_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PaymentProviders
class GocardlessProvider < BaseProvider
SUCCESS_REDIRECT_URL = 'https://gocardless.com/'

PENDING_STATUSES = %w[pending_customer_approval pending_submission submitted confirmed].freeze
PROCESSING_STATUSES = %w[pending_customer_approval pending_submission submitted confirmed].freeze
SUCCESS_STATUSES = %w[paid_out].freeze
FAILED_STATUSES = %w[cancelled customer_approval_denied failed charged_back].freeze

Expand Down
2 changes: 1 addition & 1 deletion app/models/payment_providers/stripe_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StripeProvider < BaseProvider
charge.dispute.closed
].freeze

PENDING_STATUSES = %w[
PROCESSING_STATUSES = %w[
processing
requires_capture
requires_action
Expand Down
14 changes: 1 addition & 13 deletions app/services/invoices/payments/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class AdyenService < BaseService
include Lago::Adyen::ErrorHandlable
include Customers::PaymentProviderFinder

PENDING_STATUSES = %w[AuthorisedPending Received].freeze
SUCCESS_STATUSES = %w[Authorised SentForSettle SettleScheduled Settled Refunded].freeze
FAILED_STATUSES = %w[Cancelled CaptureFailed Error Expired Refused].freeze

def initialize(invoice = nil)
@invoice = invoice

Expand All @@ -30,7 +26,7 @@ def update_payment_status(provider_payment_id:, status:, metadata: {})

payment.update!(status:)

invoice_payment_status = invoice_payment_status(status)
invoice_payment_status = payment.payment_provider&.determine_payment_status(status)
update_invoice_payment_status(payment_status: invoice_payment_status)

result
Expand Down Expand Up @@ -175,14 +171,6 @@ def payment_url_params
prms
end

def invoice_payment_status(payment_status)
return :pending if PENDING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)

payment_status
end

def update_invoice_payment_status(payment_status:, deliver_webhook: true)
result = Invoices::UpdateService.call(
invoice:,
Expand Down
15 changes: 1 addition & 14 deletions app/services/invoices/payments/gocardless_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ module Payments
class GocardlessService < BaseService
include Customers::PaymentProviderFinder

PENDING_STATUSES = %w[pending_customer_approval pending_submission submitted confirmed]
.freeze
SUCCESS_STATUSES = %w[paid_out].freeze
FAILED_STATUSES = %w[cancelled customer_approval_denied failed charged_back].freeze

def initialize(invoice = nil)
@invoice = invoice

Expand All @@ -26,7 +21,7 @@ def update_payment_status(provider_payment_id:, status:)

payment.update!(status:)

invoice_payment_status = invoice_payment_status(status)
invoice_payment_status = payment.payment_provider&.determine_payment_status(status)
update_invoice_payment_status(payment_status: invoice_payment_status)

result
Expand All @@ -40,14 +35,6 @@ def update_payment_status(provider_payment_id:, status:)

delegate :organization, :customer, to: :invoice

def invoice_payment_status(payment_status)
return :pending if PENDING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)

payment_status
end

def update_invoice_payment_status(payment_status:, deliver_webhook: true)
update_invoice_result = Invoices::UpdateService.call(
invoice: result.invoice,
Expand Down
17 changes: 2 additions & 15 deletions app/services/invoices/payments/stripe_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ module Payments
class StripeService < BaseService
include Customers::PaymentProviderFinder

PENDING_STATUSES = %w[processing requires_capture requires_action requires_confirmation requires_payment_method]
.freeze
SUCCESS_STATUSES = %w[succeeded].freeze
FAILED_STATUSES = %w[canceled].freeze

def initialize(invoice = nil)
@invoice = invoice

Expand Down Expand Up @@ -37,7 +32,7 @@ def update_payment_status(organization_id:, status:, stripe_payment:)
payment.update!(status:)

update_invoice_payment_status(
payment_status: invoice_payment_status(status),
payment_status: payment.payment_provider&.determine_payment_status(status),
processing: status == "processing"
)

Expand Down Expand Up @@ -89,7 +84,7 @@ def create_payment(stripe_payment, invoice: nil)
status: "pending"
)

status = invoice_payment_status(stripe_payment.status)
status = payment.payment_provider&.determine_payment_status(stripe_payment.status)
status = (status.to_sym == :pending) ? :processing : status

payment.provider_payment_id = stripe_payment.id
Expand Down Expand Up @@ -150,14 +145,6 @@ def description
"#{organization.name} - Invoice #{invoice.number}"
end

def invoice_payment_status(payment_status)
return :pending if PENDING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)

payment_status&.to_sym
end

def update_invoice_payment_status(payment_status:, deliver_webhook: true, processing: false)
result = Invoices::UpdateService.call(
invoice: invoice.presence || @result.invoice,
Expand Down
14 changes: 1 addition & 13 deletions app/services/payment_providers/adyen/payments/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module PaymentProviders
module Adyen
module Payments
class CreateService < BaseService
PROCESSING_STATUSES = %w[AuthorisedPending Received].freeze
SUCCESS_STATUSES = %w[Authorised SentForSettle SettleScheduled Settled Refunded].freeze
FAILED_STATUSES = %w[Cancelled CaptureFailed Error Expired Refused].freeze

def initialize(payment:, reference:, metadata:)
@payment = payment
@reference = reference
Expand All @@ -31,7 +27,7 @@ def call

payment.provider_payment_id = adyen_result.response["pspReference"]
payment.status = adyen_result.response["resultCode"]
payment.payable_payment_status = payment_status_mapping(payment.status)
payment.payable_payment_status = payment.payment_provider&.determine_payment_status(payment.status)
payment.save!

result.payment = payment
Expand Down Expand Up @@ -108,14 +104,6 @@ def payment_params
prms
end

def payment_status_mapping(payment_status)
return :processing if PROCESSING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)

payment_status
end

def prepare_failed_result(error, reraise: false)
result.error_message = error.msg
result.error_code = error.code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,14 @@ def initialize(payment:, reference:, metadata:)
super
end

PROCESSING_STATUSES = %w[pending_customer_approval pending_submission submitted confirmed]
.freeze
SUCCESS_STATUSES = %w[paid_out].freeze
FAILED_STATUSES = %w[cancelled customer_approval_denied failed charged_back].freeze

def call
result.payment = payment

gocardless_result = create_gocardless_payment

payment.provider_payment_id = gocardless_result.id
payment.status = gocardless_result.status
payment.payable_payment_status = payment_status_mapping(payment.status)
payment.payable_payment_status = payment.payment_provider&.determine_payment_status(payment.status)
payment.save!

result.payment = payment
Expand Down Expand Up @@ -96,14 +91,6 @@ def create_gocardless_payment
)
end

def payment_status_mapping(payment_status)
return :processing if PROCESSING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)

payment_status
end

def prepare_failed_result(error, reraise: false)
result.error_message = error.message
result.error_code = error.code
Expand Down
15 changes: 1 addition & 14 deletions app/services/payment_providers/stripe/payments/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ module PaymentProviders
module Stripe
module Payments
class CreateService < BaseService
PROCESSING_STATUSES = %w[processing requires_capture requires_action requires_confirmation requires_payment_method]
.freeze
SUCCESS_STATUSES = %w[succeeded].freeze
FAILED_STATUSES = %w[canceled].freeze

def initialize(payment:, reference:, metadata:)
@payment = payment
@reference = reference
Expand All @@ -26,7 +21,7 @@ def call

payment.provider_payment_id = stripe_result.id
payment.status = stripe_result.status
payment.payable_payment_status = payment_status_mapping(payment.status)
payment.payable_payment_status = payment.payment_provider&.determine_payment_status(payment.status)
payment.provider_payment_data = stripe_result.next_action if stripe_result.status == "requires_action"
payment.save!

Expand Down Expand Up @@ -62,14 +57,6 @@ def call

delegate :payment_provider, to: :provider_customer

def payment_status_mapping(payment_status)
return :processing if PROCESSING_STATUSES.include?(payment_status)
return :succeeded if SUCCESS_STATUSES.include?(payment_status)
return :failed if FAILED_STATUSES.include?(payment_status)

payment_status
end

def handle_requires_action(payment)
SendWebhookJob.perform_later("payment.requires_action", payment, {
provider_customer_id: provider_customer.provider_customer_id
Expand Down
5 changes: 3 additions & 2 deletions spec/services/invoices/payments/adyen_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let(:customer) { create(:customer, payment_provider_code: code) }
let(:organization) { customer.organization }
let(:adyen_payment_provider) { create(:adyen_provider, organization:, code:) }
let(:adyen_customer) { create(:adyen_customer, customer:) }
let(:adyen_customer) { create(:adyen_customer, customer:, payment_provider: adyen_payment_provider) }
let(:adyen_client) { instance_double(Adyen::Client) }
let(:payments_api) { Adyen::PaymentsApi.new(adyen_client, 70) }
let(:payment_links_api) { Adyen::PaymentLinksApi.new(adyen_client, 70) }
Expand Down Expand Up @@ -55,7 +55,8 @@
:payment,
payable: invoice,
provider_payment_id: "ch_123456",
status: "Pending"
status: "Pending",
payment_provider: adyen_payment_provider
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/services/invoices/payments/gocardless_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
:payment,
payable: invoice,
provider_payment_id: "ch_123456",
status: "pending_submission"
status: "pending_submission",
payment_provider: gocardless_payment_provider
)
end

Expand Down

0 comments on commit ec98ed4

Please sign in to comment.