From 2474507790d44b0009e6226ee1fc87a9cd543873 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 2 Feb 2021 12:04:52 +1100 Subject: [PATCH] Working on generic payment failure notification --- app/Jobs/Mail/PaymentFailureMailer.php | 8 +-- app/Mail/Admin/AutoBillingFailureObject.php | 9 ++- app/Mail/Admin/PaymentFailureObject.php | 68 +++++++++++++++---- app/PaymentDrivers/Stripe/Charge.php | 20 +++--- resources/views/email/admin/generic.blade.php | 6 ++ 5 files changed, 78 insertions(+), 33 deletions(-) diff --git a/app/Jobs/Mail/PaymentFailureMailer.php b/app/Jobs/Mail/PaymentFailureMailer.php index 879d0184c9cd..2d78364df648 100644 --- a/app/Jobs/Mail/PaymentFailureMailer.php +++ b/app/Jobs/Mail/PaymentFailureMailer.php @@ -31,7 +31,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue public $client; - public $message; + public $error; public $company; @@ -47,11 +47,11 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue * @param $company * @param $amount */ - public function __construct($client, $message, $company, $payment_hash) + public function __construct($client, $error, $company, $payment_hash) { $this->company = $company; - $this->message = $message; + $this->error = $error; $this->client = $client; @@ -92,7 +92,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); - $mail_obj = (new PaymentFailureObject($this->client, $this->message, $this->amount, $this->company))->build(); + $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build(); $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; //send email diff --git a/app/Mail/Admin/AutoBillingFailureObject.php b/app/Mail/Admin/AutoBillingFailureObject.php index e6cfd14103b5..bd30395f67de 100644 --- a/app/Mail/Admin/AutoBillingFailureObject.php +++ b/app/Mail/Admin/AutoBillingFailureObject.php @@ -28,7 +28,7 @@ class AutoBillingFailureObject public $payment_hash; - private $invoice; + private $invoices; /** * Create a new job instance. @@ -54,8 +54,7 @@ class AutoBillingFailureObject public function build() { - - $this->invoice = Invoice::where('id', $this->decodePrimarykey($this->payment_hash->invoices()[0]->invoice_id))->first(); + $this->$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get(); $mail_obj = new stdClass; $mail_obj->amount = $this->getAmount(); @@ -78,7 +77,7 @@ class AutoBillingFailureObject return ctrans( 'texts.auto_bill_failed', - ['invoice_number' => $this->invoice->number] + ['invoice_number' => $this->invoices->first()->number] ); } @@ -89,7 +88,7 @@ class AutoBillingFailureObject $data = [ 'title' => ctrans( 'texts.auto_bill_failed', - ['invoice_number' => $this->invoice->number] + ['invoice_number' => $this->invoices->first()->number] ), 'message' => $this->error, 'signature' => $signature, diff --git a/app/Mail/Admin/PaymentFailureObject.php b/app/Mail/Admin/PaymentFailureObject.php index b7e693d00bfe..107437d069ec 100644 --- a/app/Mail/Admin/PaymentFailureObject.php +++ b/app/Mail/Admin/PaymentFailureObject.php @@ -11,29 +11,52 @@ namespace App\Mail\Admin; +use App\Models\Invoice; use App\Utils\Number; +use App\Utils\Traits\MakesHash; use stdClass; class PaymentFailureObject { + use MakesHash; + public $client; - public $message; + public $error; public $company; - public $amount; + public $payment_hash; - public function __construct($client, $message, $amount, $company) + private $invoices; + + /** + * Create a new job instance. + * + * @param $client + * @param $message + * @param $company + * @param $amount + */ + public function __construct($client, $error, $company, $payment_hash) { $this->client = $client; - $this->message = $message; - $this->amount = $amount; + + $this->error = $error; + $this->company = $company; + + $this->payment_hash = $payment_hash; + + $this->company = $company; + } public function build() { + + $this->invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get(); + $mail_obj = new stdClass; $mail_obj->amount = $this->getAmount(); $mail_obj->subject = $this->getSubject(); @@ -46,16 +69,20 @@ class PaymentFailureObject private function getAmount() { - return Number::formatMoney($this->amount, $this->client); + + return array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total; + } private function getSubject() { + return ctrans( 'texts.payment_failed_subject', ['client' => $this->client->present()->name()] ); + } private function getData() @@ -65,23 +92,36 @@ class PaymentFailureObject $data = [ 'title' => ctrans( 'texts.payment_failed_subject', - ['client' => $this->client->present()->name()] - ), - 'message' => ctrans( - 'texts.notification_payment_paid', - ['amount' => $this->getAmount(), - 'client' => $this->client->present()->name(), - 'message' => $this->message, - ] + [ + 'client' => $this->client->present()->name() + ] ), + 'message' => $this->error, 'signature' => $signature, 'logo' => $this->company->present()->logo(), 'settings' => $this->client->getMergedSettings(), 'whitelabel' => $this->company->account->isPaid() ? true : false, 'url' => config('ninja.app_url'), 'button' => ctrans('texts.login'), + 'additional_info' => $this->buildFailedInvoices() ]; return $data; } + + private function buildFailedInvoices() + { + + $text = ''; + + foreach($this->invoices as $invoice) + { + + $text .= ctrans('texts.notification_invoice_payment_failed_subject', ['invoice' => $invoice->number]) . "\n"; + + } + + return $text; + + } } diff --git a/app/PaymentDrivers/Stripe/Charge.php b/app/PaymentDrivers/Stripe/Charge.php index e8ad3b5f006d..84900b720d38 100644 --- a/app/PaymentDrivers/Stripe/Charge.php +++ b/app/PaymentDrivers/Stripe/Charge.php @@ -91,27 +91,27 @@ class Charge switch ($e) { case ($e instanceof CardException): - $data['status'] => $e->getHttpStatus(); - $data['error_type'] => $e->getError()->type; - $data['error_code'] => $e->getError()->code; - $data['param'] => $e->getError()->param; - $data['message'] => $e->getError()->message; + $data['status'] = $e->getHttpStatus(); + $data['error_type'] = $e->getError()->type; + $data['error_code'] = $e->getError()->code; + $data['param'] = $e->getError()->param; + $data['message'] = $e->getError()->message; break; case ($e instanceof RateLimitException): - $data['message'] => 'Too many requests made to the API too quickly'; + $data['message'] = 'Too many requests made to the API too quickly'; break; case ($e instanceof InvalidRequestException): - $data['message'] => 'Invalid parameters were supplied to Stripe\'s API'; + $data['message'] = 'Invalid parameters were supplied to Stripe\'s API'; break; case ($e instanceof AuthenticationException): - $data['message'] => 'Authentication with Stripe\'s API failed'; + $data['message'] = 'Authentication with Stripe\'s API failed'; break; case ($e instanceof ApiErrorException): - $data['message'] => 'Network communication with Stripe failed'; + $data['message'] = 'Network communication with Stripe failed'; break; default: - $data['message'] => $e->getMessage(); + $data['message'] = $e->getMessage(); break; } diff --git a/resources/views/email/admin/generic.blade.php b/resources/views/email/admin/generic.blade.php index bf867eefc32e..99c527b46dc5 100644 --- a/resources/views/email/admin/generic.blade.php +++ b/resources/views/email/admin/generic.blade.php @@ -9,6 +9,12 @@

{{ $message }}

+ @if(isset($additional_info)) + +

{{ $additional_info }}

+ + @endif + @component('email.components.button', ['url' => $url]) @lang($button) @endcomponent