Generic handling page for unsuccessful payments

This commit is contained in:
David Bomba 2020-09-11 08:30:12 +10:00
parent f7dc573551
commit c55f660f7f
7 changed files with 64 additions and 12 deletions

View File

@ -45,6 +45,8 @@ class InvoicePaidActivity implements ShouldQueue
{
MultiDB::setDb($event->company->db);
$event->invoice->service()->touchPdf();
$fields = new \stdClass;
$fields->invoice_id = $event->invoice->id;

View File

@ -194,6 +194,24 @@ class BaseDriver extends AbstractPaymentDriver
});
}
/**
* In case of a payment failure we should always
* return the invoice to its original state
*
* @param PaymentHash $payment_hash The payment hash containing the list of invoices
* @return void
*/
public function unWindGatewayFees(PaymentHash $payment_hash)
{
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->get();
$invoices->each(function ($invoice) {
$invoice->service()->removeUnpaidGatewayFees();
});
}
/**
* Return the contact if possible.
*

View File

@ -253,7 +253,7 @@ class CheckoutComPaymentDriver extends BaseDriver
$payment = $this->createPayment($data, Payment::STATUS_PENDING);
$this->attachInvoices($payment, $state['hashed_ids']);
$this->attachInvoices($payment, $state['payment_hash']);
$payment->service()->updateInvoicePayment();
@ -269,9 +269,16 @@ class CheckoutComPaymentDriver extends BaseDriver
try {
return redirect($state['payment_response']->_links['redirect']['href']);
} catch (\Exception $e) {
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
throw new \Exception('Failed to process the payment.', 1);
$this->unWindGatewayFees($state['payment_hash']);
return render('gateways.unsuccessful', [
'code' => $state['payment_response']->response_code,
'message' => ctrans('texts.payment_error'),
]);
}
}
@ -290,6 +297,7 @@ class CheckoutComPaymentDriver extends BaseDriver
return render('gateways.unsuccessful', [
'code' => $state['payment_response']->response_code,
'message' => ctrans('texts.payment_error'),
]);
}
@ -308,7 +316,12 @@ class CheckoutComPaymentDriver extends BaseDriver
//todo push to a error page with the exception message.
throw new \Exception('Failed to process the payment.', 1);
//throw new \Exception('Failed to process the payment.', 1);
return render('gateways.unsuccessful', [
'code' => '500',
'message' => ctrans('texts.payment_error'),
]);
}
public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment

View File

@ -11,6 +11,7 @@
namespace App\Services\Invoice;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Models\CompanyGateway;
use App\Models\Invoice;
use App\Models\Payment;
@ -211,6 +212,8 @@ class InvoiceService
return $item;
})->toArray();
$this->invoice = $this->invoice->calc()->getInvoice();
return $this;
}
@ -221,6 +224,8 @@ class InvoiceService
return $item->type_id == '3';
})->toArray();
$this->invoice = $this->invoice->calc()->getInvoice();
return $this;
}
@ -241,6 +246,20 @@ class InvoiceService
return $this;
}
/**
* Sometimes we need to refresh the
* PDF when it is updated etc.
* @return void
*/
public function touchPdf()
{
$this->invoice->invitations->each(function ($invitation){
CreateInvoicePdf::dispatch($invitation);
});
return $this;
}
/*When a reminder is sent we want to touch the dates they were sent*/
public function touchReminder(string $reminder_template)
{

View File

@ -141,8 +141,6 @@ class HtmlEngine
}
$data['$entity_number'] = &$data['$number'];
//$data['$paid_to_date'] = ;
$data['$invoice.discount'] = ['value' => Number::formatMoney($this->entity_calc->getTotalDiscount(), $this->client) ?: ' ', 'label' => ctrans('texts.discount')];
$data['$discount'] = &$data['$invoice.discount'];
$data['$subtotal'] = ['value' => Number::formatMoney($this->entity_calc->getSubTotal(), $this->client) ?: ' ', 'label' => ctrans('texts.subtotal')];
@ -236,9 +234,8 @@ class HtmlEngine
$data['$client.country'] = &$data['$country'];
$data['$client.email'] = &$data['$email'];
$data['$client.balance'] = ['value' => $this->client->balance, 'label' => ctrans('texts.balance')];
$data['$paid_to_date'] = ['value' => $this->client->paid_to_date, 'label' => ctrans('texts.paid_to_date')];
$data['$client.balance'] = ['value' => Number::formatMoney($this->client->balance, $this->client), 'label' => ctrans('texts.account_balance')];
$data['$paid_to_date'] = ['value' => Number::formatMoney($this->client->paid_to_date, $this->client), 'label' => ctrans('texts.paid_to_date')];
$data['$contact.full_name'] = ['value' => $this->contact->present()->name(), 'label' => ctrans('texts.name')];
$data['$contact.email'] = ['value' => $this->contact->email, 'label' => ctrans('texts.email')];

View File

@ -3268,4 +3268,5 @@ return [
'no_items_selected' => 'No items selected.',
'payment_due' => 'Payment due',
'account_balance' => 'Account balance',
];

View File

@ -6,8 +6,10 @@
{{ ctrans('texts.payment_error_code', ['code' => isset($code) ? $code : '']) }}
</span>
<span class="mt-6 block">{{ ctrans('texts.common_codes') }}:</span>
<ul>
<li>{{ ctrans('texts.payment_error_code_20087') }}.</li>
</ul>
@if($message)
<span class="mt-6 block">{{ ctrans('texts.message') }}:</span>
<ul>
<li>{{ $message }}.</li>
</ul>
@endif
@endsection