diff --git a/app/jobs/invoices/payments/adyen_create_job.rb b/app/jobs/invoices/payments/adyen_create_job.rb index 7ef4199eb419..575aaf8b5d54 100644 --- a/app/jobs/invoices/payments/adyen_create_job.rb +++ b/app/jobs/invoices/payments/adyen_create_job.rb @@ -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 diff --git a/app/models/payment_providers/adyen_provider.rb b/app/models/payment_providers/adyen_provider.rb index d45af2703ebd..057c44143465 100644 --- a/app/models/payment_providers/adyen_provider.rb +++ b/app/models/payment_providers/adyen_provider.rb @@ -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 diff --git a/app/models/payment_providers/base_provider.rb b/app/models/payment_providers/base_provider.rb index 813bb6435f07..6970ba9b2d3f 100644 --- a/app/models/payment_providers/base_provider.rb +++ b/app/models/payment_providers/base_provider.rb @@ -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) diff --git a/app/models/payment_providers/gocardless_provider.rb b/app/models/payment_providers/gocardless_provider.rb index 8ee648424c3d..69e725e5c33b 100644 --- a/app/models/payment_providers/gocardless_provider.rb +++ b/app/models/payment_providers/gocardless_provider.rb @@ -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 diff --git a/app/models/payment_providers/stripe_provider.rb b/app/models/payment_providers/stripe_provider.rb index 82a9b83778a4..bd004568eca4 100644 --- a/app/models/payment_providers/stripe_provider.rb +++ b/app/models/payment_providers/stripe_provider.rb @@ -18,7 +18,7 @@ class StripeProvider < BaseProvider charge.dispute.closed ].freeze - PENDING_STATUSES = %w[ + PROCESSING_STATUSES = %w[ processing requires_capture requires_action diff --git a/app/services/invoices/payments/adyen_service.rb b/app/services/invoices/payments/adyen_service.rb index 7444f33639d1..84bccc04485e 100644 --- a/app/services/invoices/payments/adyen_service.rb +++ b/app/services/invoices/payments/adyen_service.rb @@ -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 @@ -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 @@ -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:, diff --git a/app/services/invoices/payments/gocardless_service.rb b/app/services/invoices/payments/gocardless_service.rb index 1304533b87c0..cc825caa347a 100644 --- a/app/services/invoices/payments/gocardless_service.rb +++ b/app/services/invoices/payments/gocardless_service.rb @@ -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 @@ -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 @@ -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, diff --git a/app/services/invoices/payments/stripe_service.rb b/app/services/invoices/payments/stripe_service.rb index 22a0ea4dfaa6..38f00ed43039 100644 --- a/app/services/invoices/payments/stripe_service.rb +++ b/app/services/invoices/payments/stripe_service.rb @@ -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 @@ -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" ) @@ -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 @@ -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, diff --git a/app/services/payment_providers/adyen/payments/create_service.rb b/app/services/payment_providers/adyen/payments/create_service.rb index cbf0da20e1f7..9b1ad515a4f1 100644 --- a/app/services/payment_providers/adyen/payments/create_service.rb +++ b/app/services/payment_providers/adyen/payments/create_service.rb @@ -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 @@ -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 @@ -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 diff --git a/app/services/payment_providers/gocardless/payments/create_service.rb b/app/services/payment_providers/gocardless/payments/create_service.rb index a4e313b0b816..b9f073853678 100644 --- a/app/services/payment_providers/gocardless/payments/create_service.rb +++ b/app/services/payment_providers/gocardless/payments/create_service.rb @@ -27,11 +27,6 @@ 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 @@ -39,7 +34,7 @@ def call 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 @@ -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 diff --git a/app/services/payment_providers/stripe/payments/create_service.rb b/app/services/payment_providers/stripe/payments/create_service.rb index 88374b31a30a..8b3e12ae645b 100644 --- a/app/services/payment_providers/stripe/payments/create_service.rb +++ b/app/services/payment_providers/stripe/payments/create_service.rb @@ -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 @@ -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! @@ -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 diff --git a/spec/services/invoices/payments/adyen_service_spec.rb b/spec/services/invoices/payments/adyen_service_spec.rb index 3c9baea8c20f..829e0f23c58d 100644 --- a/spec/services/invoices/payments/adyen_service_spec.rb +++ b/spec/services/invoices/payments/adyen_service_spec.rb @@ -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) } @@ -55,7 +55,8 @@ :payment, payable: invoice, provider_payment_id: "ch_123456", - status: "Pending" + status: "Pending", + payment_provider: adyen_payment_provider ) end diff --git a/spec/services/invoices/payments/gocardless_service_spec.rb b/spec/services/invoices/payments/gocardless_service_spec.rb index 2fee837f1f56..ea98cc61560d 100644 --- a/spec/services/invoices/payments/gocardless_service_spec.rb +++ b/spec/services/invoices/payments/gocardless_service_spec.rb @@ -33,7 +33,8 @@ :payment, payable: invoice, provider_payment_id: "ch_123456", - status: "pending_submission" + status: "pending_submission", + payment_provider: gocardless_payment_provider ) end