Skip to content

Commit

Permalink
Fix handling of provider_customer_id
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Dec 20, 2024
1 parent 606ad89 commit b5578bd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 103 deletions.
6 changes: 5 additions & 1 deletion app/services/invoices/payments/cashfree_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def should_process_payment?
!!customer&.cashfree_customer&.id
end

def increment_payment_attempts
invoice.update!(payment_attempts: invoice.payment_attempts + 1)
end

def client
@client ||= LagoHttpClient::Client.new(::PaymentProviders::CashfreeProvider::BASE_URL)
end
Expand Down Expand Up @@ -150,7 +154,7 @@ def update_invoice_payment_status(payment_status:, deliver_webhook: true)

def deliver_error_webhook(cashfree_error)
DeliverErrorWebhookService.call_async(invoice, {
provider_customer_id: customer.cashfree_customer.provider_customer_id,
provider_customer_id: customer.cashfree_customer.id,
provider_error: {
message: cashfree_error.error_body,
error_code: cashfree_error.error_code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PaymentProviders
module Cashfree
module Payments
class CreateService < BaseService
include Customers::PaymentProviderFinder
include ::Customers::PaymentProviderFinder

PENDING_STATUSES = %w[PARTIALLY_PAID].freeze
SUCCESS_STATUSES = %w[PAID].freeze
Expand All @@ -20,6 +20,10 @@ def initialize(payment:)

def call
result.payment = payment

# NOTE: No need to register the payment with Cashfree Payments for the Payment Link feature.
# Simply create a single `Payment` record and update it upon receiving the webhook, which works perfectly fine.

result
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/payment_requests/payments/cashfree_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def create_payment(cashfree_payment)

def deliver_error_webhook(cashfree_error)
DeliverErrorWebhookService.call_async(payable, {
provider_customer_id: customer.cashfree_customer.provider_customer_id,
provider_customer_id: customer.cashfree_customer.id,
provider_error: {
message: cashfree_error.error_body,
error_code: cashfree_error.error_code
Expand Down
32 changes: 8 additions & 24 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 0 additions & 76 deletions spec/services/invoices/payments/cashfree_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,82 +24,6 @@
)
end

describe ".call" do
before do
cashfree_payment_provider
cashfree_customer

allow(Invoices::PrepaidCreditJob).to receive(:perform_later)
end

it "creates a cashfree payment", aggregate_failure: true do
result = cashfree_service.call

expect(result).to be_success

expect(result.invoice).to be_payment_pending
expect(result.invoice.payment_attempts).to eq(1)
expect(result.invoice.reload.ready_for_payment_processing).to eq(true)

expect(result.payment.id).to be_present
expect(result.payment.payable).to eq(invoice)
expect(result.payment.payment_provider).to eq(cashfree_payment_provider)
expect(result.payment.payment_provider_customer).to eq(cashfree_customer)
expect(result.payment.amount_cents).to eq(invoice.total_amount_cents)
expect(result.payment.amount_currency).to eq(invoice.currency)
expect(result.payment.status).to eq("pending")
end

it_behaves_like "syncs payment" do
let(:service_call) { cashfree_service.call }
end

context "with no payment provider" do
let(:cashfree_payment_provider) { nil }

it "does not creates a payment", aggregate_failure: true do
result = cashfree_service.call

expect(result).to be_success
expect(result.invoice).to eq(invoice)
expect(result.payment).to be_nil
end
end

context "with 0 amount" do
let(:invoice) do
create(
:invoice,
organization:,
customer:,
total_amount_cents: 0,
currency: "EUR"
)
end

it "does not creates a payment", aggregate_failure: true do
result = cashfree_service.call

expect(result).to be_success
expect(result.invoice).to eq(invoice)
expect(result.payment).to be_nil
expect(result.invoice).to be_payment_succeeded
end
end

context "when customer does not exists" do
let(:cashfree_customer) { nil }

it "does not creates a adyen payment", aggregate_failure: true do
result = cashfree_service.call

expect(result).to be_success
expect(result.invoice).to eq(invoice)
expect(result.payment).to be_nil
end
end
end

describe ".update_payment_status" do
let(:payment) do
create(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe PaymentProviders::Cashfree::Payments::CreateService, type: :service do
subject(:create_service) { described_class.new(payment:) }

let(:customer) { create(:customer, payment_provider_code: code) }
let(:organization) { customer.organization }
let(:chasfree_payment_provider) { create(:cashfree_provider, organization:, code:) }
let(:cashfree_customer) { create(:cashfree_customer, customer:, payment_provider: chasfree_payment_provider) }
let(:code) { "stripe_1" }

let(:invoice) do
create(
:invoice,
organization:,
customer:,
total_amount_cents: 200,
currency: "EUR",
ready_for_payment_processing: true
)
end

let(:payment) do
create(
:payment,
payable: invoice,
status: "pending",
payable_payment_status: "pending",
payment_provider: chasfree_payment_provider,
payment_provider_customer: cashfree_customer,
amount_cents: invoice.total_amount_cents,
amount_currency: invoice.currency
)
end

describe ".call" do
before do
chasfree_payment_provider
cashfree_customer
end

it "returns the payment and keeps it pending" do
result = create_service.call

expect(result).to be_success

expect(result.payment.id).to be_present
expect(result.payment.payable).to eq(invoice)
expect(result.payment.payment_provider).to eq(chasfree_payment_provider)
expect(result.payment.payment_provider_customer).to eq(cashfree_customer)
expect(result.payment.amount_cents).to eq(invoice.total_amount_cents)
expect(result.payment.amount_currency).to eq(invoice.currency)
expect(result.payment.status).to eq("pending")
expect(result.payment.payable_payment_status).to eq("pending")
end
end
end

0 comments on commit b5578bd

Please sign in to comment.