Skip to content

Commit e3df201

Browse files
committed
feat(webhooks): Sends invoice.payment_failed when retry to pay without payment provider
1 parent 4eabb6e commit e3df201

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

app/services/invoices/payments/retry_service.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ def call
2020
return result.not_allowed_failure!(code: "payment_processor_is_currently_handling_payment")
2121
end
2222

23+
if invoice.customer.payment_provider.nil?
24+
Utils::ActivityLog.produce(invoice, "invoice.payment_failure")
25+
SendWebhookJob.perform_later("invoice.payment_failure", invoice, { provider_error: { error_message: "no linked_payment provider", error_code: "no_linked_payment_provider" } })
26+
return result.single_validation_failure!(error_code: "no_linked_payment_provider")
27+
end
28+
2329
Invoices::Payments::CreateService.call_async(invoice:)
2430

2531
result.invoice = invoice

spec/services/invoices/payments/retry_service_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,32 @@
9090
expect(result.error.code).to eq("payment_processor_is_currently_handling_payment")
9191
end
9292
end
93+
94+
context "when customer does not have payment method" do
95+
before do
96+
customer.update(payment_provider: nil, payment_provider_code: nil)
97+
end
98+
99+
it "returns an error" do
100+
result = retry_service.call
101+
102+
expect(result).not_to be_success
103+
expect(result.error).to be_a(BaseService::ValidationFailure)
104+
expect(result.error.messages).to eq({base: ["no_linked_payment_provider"]})
105+
end
106+
107+
it "sends invoice.payment_failure webhook" do
108+
expect { retry_service.call }
109+
.to enqueue_job(SendWebhookJob)
110+
.with(
111+
"invoice.payment_failure",
112+
invoice,
113+
provider_error: {
114+
error_message: "no linked_payment provider",
115+
error_code: "no_linked_payment_provider"
116+
}
117+
).on_queue(webhook_queue)
118+
end
119+
end
93120
end
94121
end

0 commit comments

Comments
 (0)