From 5d66f5df2ce25771eaf3c2af834fd6a8566a68fa Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Mar 2023 17:45:33 +1100 Subject: [PATCH] Refactor for Gocardless --- app/Jobs/Mail/PaymentFailedMailer.php | 4 +- .../Subscription/CleanStaleInvoiceOrder.php | 2 + app/Mail/Admin/ClientPaymentFailureObject.php | 4 ++ app/Mail/Admin/PaymentFailureObject.php | 4 ++ app/PaymentDrivers/GoCardless/DirectDebit.php | 3 +- app/PaymentDrivers/GoCardless/SEPA.php | 2 +- .../GoCardlessPaymentDriver.php | 45 ++++++++++++++----- .../views/email/client/generic.blade.php | 28 ++++++++---- 8 files changed, 68 insertions(+), 24 deletions(-) diff --git a/app/Jobs/Mail/PaymentFailedMailer.php b/app/Jobs/Mail/PaymentFailedMailer.php index c5fa3c14db..4408637ba2 100644 --- a/app/Jobs/Mail/PaymentFailedMailer.php +++ b/app/Jobs/Mail/PaymentFailedMailer.php @@ -65,8 +65,8 @@ class PaymentFailedMailer implements ShouldQueue */ public function handle() { - if (!is_string($this->error)) { - $this->error = "Payment failed, no reason given."; + if (!is_string($this->error) || strlen($this->error) <=1) { + $this->error = ""; } //Set DB diff --git a/app/Jobs/Subscription/CleanStaleInvoiceOrder.php b/app/Jobs/Subscription/CleanStaleInvoiceOrder.php index 6d042ccd82..f0912aefbc 100644 --- a/app/Jobs/Subscription/CleanStaleInvoiceOrder.php +++ b/app/Jobs/Subscription/CleanStaleInvoiceOrder.php @@ -37,6 +37,8 @@ class CleanStaleInvoiceOrder implements ShouldQueue */ public function handle(InvoiceRepository $repo) : void { + nlog("Cleaning Stale Invoices:"); + if (! config('ninja.db.multi_db_enabled')) { Invoice::query() ->withTrashed() diff --git a/app/Mail/Admin/ClientPaymentFailureObject.php b/app/Mail/Admin/ClientPaymentFailureObject.php index ee62778f6b..d8516e02f6 100644 --- a/app/Mail/Admin/ClientPaymentFailureObject.php +++ b/app/Mail/Admin/ClientPaymentFailureObject.php @@ -124,6 +124,10 @@ class ClientPaymentFailureObject 'company' => $this->company, ]; + if (strlen($this->error > 1)) { + $data['content'] .= "\n\n".$this->error; + } + return $data; } } diff --git a/app/Mail/Admin/PaymentFailureObject.php b/app/Mail/Admin/PaymentFailureObject.php index 8b484be26e..3d0297cd21 100644 --- a/app/Mail/Admin/PaymentFailureObject.php +++ b/app/Mail/Admin/PaymentFailureObject.php @@ -120,6 +120,10 @@ class PaymentFailureObject 'additional_info' => $this->error, ]; + if (strlen($this->error > 1)) { + $data['content'] .= "\n\n".$this->error; + } + return $data; } diff --git a/app/PaymentDrivers/GoCardless/DirectDebit.php b/app/PaymentDrivers/GoCardless/DirectDebit.php index a6b55c113c..81bb8c105b 100644 --- a/app/PaymentDrivers/GoCardless/DirectDebit.php +++ b/app/PaymentDrivers/GoCardless/DirectDebit.php @@ -176,7 +176,7 @@ class DirectDebit implements MethodInterface $payment_meta = new \stdClass; $payment_meta->brand = $billing_request->resources->customer_bank_account->bank_name; - $payment_meta->type = GatewayType::DIRECT_DEBIT; + $payment_meta->type = $this->resolveScheme($billing_request->mandate_request->scheme); $payment_meta->state = 'pending'; $payment_meta->last4 = $billing_request->resources->customer_bank_account->account_number_ending; @@ -230,6 +230,7 @@ class DirectDebit implements MethodInterface { match ($scheme) { 'sepa_core' => $type = GatewayType::SEPA, + 'ach' => $type = GatewayType::BANK_TRANSFER, default => $type = GatewayType::DIRECT_DEBIT, }; diff --git a/app/PaymentDrivers/GoCardless/SEPA.php b/app/PaymentDrivers/GoCardless/SEPA.php index fbcc43e7ca..1a96bcc9fc 100644 --- a/app/PaymentDrivers/GoCardless/SEPA.php +++ b/app/PaymentDrivers/GoCardless/SEPA.php @@ -177,7 +177,7 @@ class SEPA implements MethodInterface try { $payment = $this->go_cardless->gateway->payments()->create([ 'params' => [ - 'amount' => $amount), + 'amount' => $amount, 'currency' => $request->currency, 'description' => $description, 'metadata' => [ diff --git a/app/PaymentDrivers/GoCardlessPaymentDriver.php b/app/PaymentDrivers/GoCardlessPaymentDriver.php index 7e6410582e..d1c499f4f1 100644 --- a/app/PaymentDrivers/GoCardlessPaymentDriver.php +++ b/app/PaymentDrivers/GoCardlessPaymentDriver.php @@ -11,22 +11,23 @@ namespace App\PaymentDrivers; -use App\Factory\ClientContactFactory; -use App\Factory\ClientFactory; -use App\Http\Requests\Payments\PaymentWebhookRequest; -use App\Jobs\Util\SystemLogger; use App\Models\Client; -use App\Models\ClientGatewayToken; use App\Models\Country; -use App\Models\GatewayType; 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\Models\SystemLog; -use App\Utils\Traits\GeneratesCounter; +use App\Factory\ClientFactory; +use App\Jobs\Util\SystemLogger; use App\Utils\Traits\MakesHash; +use App\Models\ClientGatewayToken; +use App\Factory\ClientContactFactory; +use App\Jobs\Mail\PaymentFailedMailer; +use App\Utils\Traits\GeneratesCounter; use Illuminate\Database\QueryException; +use App\Http\Requests\Payments\PaymentWebhookRequest; class GoCardlessPaymentDriver extends BaseDriver { @@ -46,7 +47,7 @@ class GoCardlessPaymentDriver extends BaseDriver private bool $completed = true; public static $methods = [ - GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\ACH::class, + GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\DirectDebit::class, GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class, GatewayType::SEPA => \App\PaymentDrivers\GoCardless\SEPA::class, GatewayType::INSTANT_BANK_PAY => \App\PaymentDrivers\GoCardless\InstantBankPay::class, @@ -79,7 +80,7 @@ class GoCardlessPaymentDriver extends BaseDriver $this->client && isset($this->client->country) // && in_array($this->client->country->iso_3166_3, ['GBR']) - && in_array($this->client->currency()->code, ['EUR', 'GBP','DKK','SEK','AUD','NZD','USD']) + && in_array($this->client->currency()->code, ['EUR', 'GBP','DKK','SEK','AUD','NZD']) ) { $types[] = GatewayType::DIRECT_DEBIT; } @@ -131,7 +132,8 @@ class GoCardlessPaymentDriver extends BaseDriver } public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) - {nlog("here"); + { + $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; $converted_amount = $this->convertToGoCardlessAmount($amount, $this->client->currency()->precision); @@ -278,9 +280,28 @@ class GoCardlessPaymentDriver extends BaseDriver ->first(); if ($payment) { + + if ($payment->status_id == Payment::STATUS_PENDING) { + $payment->service()->deletePayment(); + } + $payment->status_id = Payment::STATUS_FAILED; $payment->save(); - nlog('GoCardless completed'); + + $payment_hash = PaymentHash::where('payment_id', $payment->id)->first(); + $error = ''; + + if (isset($event['details']['description'])) { + $error = $event['details']['description']; + } + + PaymentFailedMailer::dispatch( + $payment_hash, + $payment->client->company, + $payment->client, + $error + ); + } } diff --git a/resources/views/email/client/generic.blade.php b/resources/views/email/client/generic.blade.php index b54c9cbea7..187630d27f 100644 --- a/resources/views/email/client/generic.blade.php +++ b/resources/views/email/client/generic.blade.php @@ -21,17 +21,29 @@ @endisset @isset($url) - - - - +
- - {{ ctrans($button) }} +
+ + + + + -
+ + {{ ctrans($button) }} -
+
+ + @endisset