Working on generic payment failure notification

This commit is contained in:
David Bomba 2021-02-02 12:04:52 +11:00
parent e9d0ac30e2
commit 2474507790
5 changed files with 78 additions and 33 deletions

View File

@ -31,7 +31,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
public $client; public $client;
public $message; public $error;
public $company; public $company;
@ -47,11 +47,11 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
* @param $company * @param $company
* @param $amount * @param $amount
*/ */
public function __construct($client, $message, $company, $payment_hash) public function __construct($client, $error, $company, $payment_hash)
{ {
$this->company = $company; $this->company = $company;
$this->message = $message; $this->error = $error;
$this->client = $client; $this->client = $client;
@ -92,7 +92,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) { if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]); 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')]; $mail_obj->from = [config('mail.from.address'), config('mail.from.name')];
//send email //send email

View File

@ -28,7 +28,7 @@ class AutoBillingFailureObject
public $payment_hash; public $payment_hash;
private $invoice; private $invoices;
/** /**
* Create a new job instance. * Create a new job instance.
@ -54,8 +54,7 @@ class AutoBillingFailureObject
public function build() public function build()
{ {
$this->$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get();
$this->invoice = Invoice::where('id', $this->decodePrimarykey($this->payment_hash->invoices()[0]->invoice_id))->first();
$mail_obj = new stdClass; $mail_obj = new stdClass;
$mail_obj->amount = $this->getAmount(); $mail_obj->amount = $this->getAmount();
@ -78,7 +77,7 @@ class AutoBillingFailureObject
return return
ctrans( ctrans(
'texts.auto_bill_failed', 'texts.auto_bill_failed',
['invoice_number' => $this->invoice->number] ['invoice_number' => $this->invoices->first()->number]
); );
} }
@ -89,7 +88,7 @@ class AutoBillingFailureObject
$data = [ $data = [
'title' => ctrans( 'title' => ctrans(
'texts.auto_bill_failed', 'texts.auto_bill_failed',
['invoice_number' => $this->invoice->number] ['invoice_number' => $this->invoices->first()->number]
), ),
'message' => $this->error, 'message' => $this->error,
'signature' => $signature, 'signature' => $signature,

View File

@ -11,29 +11,52 @@
namespace App\Mail\Admin; namespace App\Mail\Admin;
use App\Models\Invoice;
use App\Utils\Number; use App\Utils\Number;
use App\Utils\Traits\MakesHash;
use stdClass; use stdClass;
class PaymentFailureObject class PaymentFailureObject
{ {
use MakesHash;
public $client; public $client;
public $message; public $error;
public $company; 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->client = $client;
$this->message = $message;
$this->amount = $amount; $this->error = $error;
$this->company = $company; $this->company = $company;
$this->payment_hash = $payment_hash;
$this->company = $company;
} }
public function build() 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 = new stdClass;
$mail_obj->amount = $this->getAmount(); $mail_obj->amount = $this->getAmount();
$mail_obj->subject = $this->getSubject(); $mail_obj->subject = $this->getSubject();
@ -46,16 +69,20 @@ class PaymentFailureObject
private function getAmount() 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() private function getSubject()
{ {
return return
ctrans( ctrans(
'texts.payment_failed_subject', 'texts.payment_failed_subject',
['client' => $this->client->present()->name()] ['client' => $this->client->present()->name()]
); );
} }
private function getData() private function getData()
@ -65,23 +92,36 @@ class PaymentFailureObject
$data = [ $data = [
'title' => ctrans( 'title' => ctrans(
'texts.payment_failed_subject', 'texts.payment_failed_subject',
['client' => $this->client->present()->name()] [
), 'client' => $this->client->present()->name()
'message' => ctrans( ]
'texts.notification_payment_paid',
['amount' => $this->getAmount(),
'client' => $this->client->present()->name(),
'message' => $this->message,
]
), ),
'message' => $this->error,
'signature' => $signature, 'signature' => $signature,
'logo' => $this->company->present()->logo(), 'logo' => $this->company->present()->logo(),
'settings' => $this->client->getMergedSettings(), 'settings' => $this->client->getMergedSettings(),
'whitelabel' => $this->company->account->isPaid() ? true : false, 'whitelabel' => $this->company->account->isPaid() ? true : false,
'url' => config('ninja.app_url'), 'url' => config('ninja.app_url'),
'button' => ctrans('texts.login'), 'button' => ctrans('texts.login'),
'additional_info' => $this->buildFailedInvoices()
]; ];
return $data; 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;
}
} }

View File

@ -91,27 +91,27 @@ class Charge
switch ($e) { switch ($e) {
case ($e instanceof CardException): case ($e instanceof CardException):
$data['status'] => $e->getHttpStatus(); $data['status'] = $e->getHttpStatus();
$data['error_type'] => $e->getError()->type; $data['error_type'] = $e->getError()->type;
$data['error_code'] => $e->getError()->code; $data['error_code'] = $e->getError()->code;
$data['param'] => $e->getError()->param; $data['param'] = $e->getError()->param;
$data['message'] => $e->getError()->message; $data['message'] = $e->getError()->message;
break; break;
case ($e instanceof RateLimitException): 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; break;
case ($e instanceof InvalidRequestException): 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; break;
case ($e instanceof AuthenticationException): case ($e instanceof AuthenticationException):
$data['message'] => 'Authentication with Stripe\'s API failed'; $data['message'] = 'Authentication with Stripe\'s API failed';
break; break;
case ($e instanceof ApiErrorException): case ($e instanceof ApiErrorException):
$data['message'] => 'Network communication with Stripe failed'; $data['message'] = 'Network communication with Stripe failed';
break; break;
default: default:
$data['message'] => $e->getMessage(); $data['message'] = $e->getMessage();
break; break;
} }

View File

@ -9,6 +9,12 @@
<p>{{ $message }}</p> <p>{{ $message }}</p>
@if(isset($additional_info))
<p> {{ $additional_info }}</p>
@endif
@component('email.components.button', ['url' => $url]) @component('email.components.button', ['url' => $url])
@lang($button) @lang($button)
@endcomponent @endcomponent