Skip to content

Commit

Permalink
misc(cashfree): Apply payment refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jan 13, 2025
1 parent 4f40878 commit 36d9b8a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
4 changes: 4 additions & 0 deletions app/models/payment_providers/cashfree_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class CashfreeProvider < BaseProvider
API_VERSION = "2023-08-01"
BASE_URL = (Rails.env.production? ? "https://api.cashfree.com/pg/links" : "https://sandbox.cashfree.com/pg/links")

PROCESSING_STATUSES = %w[PARTIALLY_PAID].freeze
SUCCESS_STATUSES = %w[PAID].freeze
FAILED_STATUSES = %w[EXPIRED CANCELLED].freeze

validates :client_id, presence: true
validates :client_secret, presence: true
validates :success_redirect_url, url: true, allow_nil: true, length: {maximum: 1024}
Expand Down
15 changes: 2 additions & 13 deletions app/services/invoices/payments/cashfree_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ module Payments
class CashfreeService < BaseService
include Customers::PaymentProviderFinder

PENDING_STATUSES = %w[PARTIALLY_PAID].freeze
SUCCESS_STATUSES = %w[PAID].freeze
FAILED_STATUSES = %w[EXPIRED CANCELLED].freeze

def initialize(invoice = nil)
@invoice = invoice

Expand All @@ -28,7 +24,8 @@ def update_payment_status(organization_id:, status:, cashfree_payment:)
return result if payment.payable.payment_succeeded?

payment.update!(status:)
update_invoice_payment_status(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
rescue BaseService::FailedResult => e
Expand Down Expand Up @@ -132,14 +129,6 @@ def payment_url_params
}
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)
@invoice = result.invoice
result = Invoices::UpdateService.call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ module Payments
class CreateService < BaseService
include ::Customers::PaymentProviderFinder

PENDING_STATUSES = %w[PARTIALLY_PAID].freeze
SUCCESS_STATUSES = %w[PAID].freeze
FAILED_STATUSES = %w[EXPIRED CANCELLED].freeze

def initialize(payment:)
@payment = payment
@invoice = payment.payable
Expand Down
3 changes: 2 additions & 1 deletion spec/services/invoices/payments/cashfree_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
:payment,
payable: invoice,
provider_payment_id: invoice.id,
status: "pending"
status: "pending",
payment_provider: cashfree_payment_provider
)
end

Expand Down

0 comments on commit 36d9b8a

Please sign in to comment.