From adf93942b274f7a67ab2796f86c26755fcbf0bc0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 30 Aug 2024 11:10:18 +1000 Subject: [PATCH] Fixes for BTC Pay --- app/PaymentDrivers/BTCPayPaymentDriver.php | 51 +++++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/app/PaymentDrivers/BTCPayPaymentDriver.php b/app/PaymentDrivers/BTCPayPaymentDriver.php index 4c93c9842c..4d1c02b273 100644 --- a/app/PaymentDrivers/BTCPayPaymentDriver.php +++ b/app/PaymentDrivers/BTCPayPaymentDriver.php @@ -12,18 +12,19 @@ namespace App\PaymentDrivers; -use App\Utils\Traits\MakesHash; -use App\Models\PaymentHash; -use App\Models\GatewayType; -use App\PaymentDrivers\BTCPay\BTCPay; -use App\Models\SystemLog; -use App\Models\Payment; use App\Models\Client; -use App\Exceptions\PaymentFailed; -use App\Models\PaymentType; -use BTCPayServer\Client\Webhook; -use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Models\Invoice; +use App\Models\Payment; +use App\Models\SystemLog; +use App\Models\GatewayType; +use App\Models\PaymentHash; +use App\Models\PaymentType; +use App\Utils\Traits\MakesHash; +use BTCPayServer\Client\Webhook; +use App\Exceptions\PaymentFailed; +use App\PaymentDrivers\BTCPay\BTCPay; +use App\Jobs\Mail\PaymentFailedMailer; +use App\Http\Requests\Payments\PaymentWebhookRequest; class BTCPayPaymentDriver extends BaseDriver { @@ -138,8 +139,6 @@ class BTCPayPaymentDriver extends BaseDriver $_invoice = $this->payment_hash->fee_invoice; - // Invoice::with('client')->withTrashed()->find($this->payment_hash->fee_invoice_id); - $this->client = $_invoice->client; $dataPayment = [ @@ -158,21 +157,49 @@ class BTCPayPaymentDriver extends BaseDriver } switch ($btcpayRep->type) { case "InvoiceExpired": + + if ($payment->status_id == Payment::STATUS_PENDING) { + $payment->service()->deletePayment(); + $this->failedPaymentNotification($payment); + } + $StatusId = Payment::STATUS_CANCELLED; break; case "InvoiceInvalid": + + if ($payment->status_id == Payment::STATUS_PENDING) { + $payment->service()->deletePayment(); + $this->failedPaymentNotification($payment); + } + $StatusId = Payment::STATUS_FAILED; break; case "InvoiceSettled": $StatusId = Payment::STATUS_COMPLETED; break; } + if ($payment->status_id != $StatusId) { $payment->status_id = $StatusId; $payment->save(); } } + private function failedPaymentNotification(Payment $payment): void + { + + $error = ctrans('texts.client_payment_failure_body', [ + 'invoice' => implode(',', $payment->invoices->pluck('number')->toArray()), + 'amount' => array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total, ]); + + PaymentFailedMailer::dispatch( + $this->payment_hash, + $payment->client->company, + $payment->client, + $error + ); + + } public function refund(Payment $payment, $amount, $return_client_response = false) {