diff --git a/app/Listeners/Invoice/InvoicePaidActivity.php b/app/Listeners/Invoice/InvoicePaidActivity.php index fd39151d86e7..c2d260d9658f 100644 --- a/app/Listeners/Invoice/InvoicePaidActivity.php +++ b/app/Listeners/Invoice/InvoicePaidActivity.php @@ -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; diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index a23002984172..d6089437a2e2 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -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. * diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 45ecfd8bd3e0..0fa692d50fb6 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -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 diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index b646ffee4386..6dc73c1f7569 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -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) { diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index fc0d7160619b..893f3a31c3bd 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -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')]; diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index b9f14b54c991..4323653b70aa 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3268,4 +3268,5 @@ return [ 'no_items_selected' => 'No items selected.', 'payment_due' => 'Payment due', + 'account_balance' => 'Account balance', ]; diff --git a/resources/views/portal/ninja2020/gateways/unsuccessful.blade.php b/resources/views/portal/ninja2020/gateways/unsuccessful.blade.php index 94ae74289b3f..6b9b900dad2c 100644 --- a/resources/views/portal/ninja2020/gateways/unsuccessful.blade.php +++ b/resources/views/portal/ninja2020/gateways/unsuccessful.blade.php @@ -6,8 +6,10 @@ {{ ctrans('texts.payment_error_code', ['code' => isset($code) ? $code : '']) }} - {{ ctrans('texts.common_codes') }}: - + @if($message) + {{ ctrans('texts.message') }}: + + @endif @endsection