From 374a6fa7b409966fe5b1a109d2aaefa71499e9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 22:47:42 +0200 Subject: [PATCH 01/14] Scaffold `KBC` --- app/PaymentDrivers/Mollie/KBC.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/PaymentDrivers/Mollie/KBC.php diff --git a/app/PaymentDrivers/Mollie/KBC.php b/app/PaymentDrivers/Mollie/KBC.php new file mode 100644 index 0000000000..24b0abcaa5 --- /dev/null +++ b/app/PaymentDrivers/Mollie/KBC.php @@ -0,0 +1,28 @@ + Date: Fri, 24 Sep 2021 23:02:17 +0200 Subject: [PATCH 02/14] Define `KBC` constant --- app/Models/GatewayType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index 5be577bb6a..c79271c1c6 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -25,6 +25,7 @@ class GatewayType extends StaticModel const APPLE_PAY = 8; const SEPA = 9; const CREDIT = 10; + const KBC = 11; public function gateway() { From f24a8f349a89f406e4c3f9474a597b269af051c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:02:33 +0200 Subject: [PATCH 03/14] Add `KBC` to Gateway --- app/Models/Gateway.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 4414a685c9..bcb6ecf3c4 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -89,7 +89,8 @@ class Gateway extends StaticModel break; case 7: return [ - GatewayType::CREDIT_CARD => ['refund' => false, 'token_billing' => true], // Mollie + GatewayType::CREDIT_CARD => ['refund' => false, 'token_billing' => true], // Mollie, + GatewayType::KBC => ['refund' => false, 'token_billing' => false], ]; case 15: return [GatewayType::PAYPAL => ['refund' => true, 'token_billing' => false]]; //Paypal From 02dbb0acd56b98c9d7afc8088601484b6d29c4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:04:47 +0200 Subject: [PATCH 04/14] Add `KBC` to MolliePaymentDriver --- app/PaymentDrivers/MolliePaymentDriver.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/PaymentDrivers/MolliePaymentDriver.php b/app/PaymentDrivers/MolliePaymentDriver.php index 1bffcca72c..add7b67d04 100644 --- a/app/PaymentDrivers/MolliePaymentDriver.php +++ b/app/PaymentDrivers/MolliePaymentDriver.php @@ -25,6 +25,7 @@ use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; use App\PaymentDrivers\Mollie\CreditCard; +use App\PaymentDrivers\Mollie\KBC; use App\Utils\Traits\MakesHash; use Illuminate\Support\Facades\Validator; use Mollie\Api\Exceptions\ApiException; @@ -64,6 +65,7 @@ class MolliePaymentDriver extends BaseDriver */ public static $methods = [ GatewayType::CREDIT_CARD => CreditCard::class, + GatewayType::KBC => KBC::class, ]; const SYSTEM_LOG_TYPE = SystemLog::TYPE_MOLLIE; @@ -84,6 +86,7 @@ class MolliePaymentDriver extends BaseDriver $types = []; $types[] = GatewayType::CREDIT_CARD; + $types[] = GatewayType::KBC; return $types; } From 67c4e5562268a3873dd1b59533783aa39ae49def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:05:10 +0200 Subject: [PATCH 05/14] Initialize MollieApiClient --- app/PaymentDrivers/Mollie/KBC.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/PaymentDrivers/Mollie/KBC.php b/app/PaymentDrivers/Mollie/KBC.php index 24b0abcaa5..4aa8b1fb0e 100644 --- a/app/PaymentDrivers/Mollie/KBC.php +++ b/app/PaymentDrivers/Mollie/KBC.php @@ -15,9 +15,19 @@ namespace App\PaymentDrivers\Mollie; use App\Http\Requests\Request; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\PaymentDrivers\Common\MethodInterface; +use App\PaymentDrivers\MolliePaymentDriver; class KBC implements MethodInterface { + protected MolliePaymentDriver $mollie; + + public function __construct(MolliePaymentDriver $mollie) + { + $this->mollie = $mollie; + + $this->mollie->init(); + } + public function authorizeView(array $data) { } public function authorizeResponse(Request $request) { } From 16f898379c0dbbe1d863c0ac5df54227b21344dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:07:21 +0200 Subject: [PATCH 06/14] Authorization --- app/PaymentDrivers/Mollie/KBC.php | 34 ++++++++++++++++--- resources/lang/en/texts.php | 1 + .../gateways/mollie/kbc/authorize.blade.php | 8 +++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 resources/views/portal/ninja2020/gateways/mollie/kbc/authorize.blade.php diff --git a/app/PaymentDrivers/Mollie/KBC.php b/app/PaymentDrivers/Mollie/KBC.php index 4aa8b1fb0e..826fc72017 100644 --- a/app/PaymentDrivers/Mollie/KBC.php +++ b/app/PaymentDrivers/Mollie/KBC.php @@ -16,6 +16,8 @@ use App\Http\Requests\Request; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\PaymentDrivers\Common\MethodInterface; use App\PaymentDrivers\MolliePaymentDriver; +use Illuminate\Http\RedirectResponse; +use Illuminate\View\View; class KBC implements MethodInterface { @@ -28,11 +30,33 @@ class KBC implements MethodInterface $this->mollie->init(); } - public function authorizeView(array $data) { } + /** + * Show the authorization page for bank transfer. + * + * @param array $data + * @return View + */ + public function authorizeView(array $data): View + { + return render('gateways.mollie.kbc.authorize', $data); + } - public function authorizeResponse(Request $request) { } + /** + * Handle the authorization for bank transfer. + * + * @param Request $request + * @return RedirectResponse + */ + public function authorizeResponse(Request $request): RedirectResponse + { + return redirect()->route('client.payment_methods.index'); + } - public function paymentView(array $data) { } + public function paymentView(array $data) + { + } - public function paymentResponse(PaymentResponseRequest $request) { } -} \ No newline at end of file + public function paymentResponse(PaymentResponseRequest $request) + { + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 04f3816d39..bb078c1f04 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4313,6 +4313,7 @@ $LANG = array( 'unable_to_verify_payment_method' => 'Unable to verify payment method.', 'generic_gateway_error' => 'Gateway configuration error. Please check your credentials.', 'my_documents' => 'My documents', + 'payment_method_cannot_be_preauthorized' => 'This payment method cannot be preauthorized.', ); return $LANG; diff --git a/resources/views/portal/ninja2020/gateways/mollie/kbc/authorize.blade.php b/resources/views/portal/ninja2020/gateways/mollie/kbc/authorize.blade.php new file mode 100644 index 0000000000..d6b3b3c310 --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/mollie/kbc/authorize.blade.php @@ -0,0 +1,8 @@ +@extends('portal.ninja2020.layout.payments', ['gateway_title' => 'KBC/CBC', 'card_title' => +'KBC/CBC']) + +@section('gateway_content') + @component('portal.ninja2020.components.general.card-element-single') + {{ __('texts.payment_method_cannot_be_preauthorized') }} + @endcomponent +@endsection From 367820af6b1a53480737ad6d21c7d4f609660239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:17:29 +0200 Subject: [PATCH 07/14] Add new `PaymentType` --- app/Models/PaymentType.php | 1 + ..._09_24_211504_add_kbc_to_payment_types.php | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 database/migrations/2021_09_24_211504_add_kbc_to_payment_types.php diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index b6b28cd4c8..7ac7d3f1ec 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -42,6 +42,7 @@ class PaymentType extends StaticModel const SEPA = 29; const GOCARDLESS = 30; const CRYPTO = 31; + const KBC = 35; public static function parseCardType($cardName) { diff --git a/database/migrations/2021_09_24_211504_add_kbc_to_payment_types.php b/database/migrations/2021_09_24_211504_add_kbc_to_payment_types.php new file mode 100644 index 0000000000..9962e7802c --- /dev/null +++ b/database/migrations/2021_09_24_211504_add_kbc_to_payment_types.php @@ -0,0 +1,26 @@ +id = 35; + $type->name = 'KBC/CBC'; + $type->gateway_type_id = GatewayType::KBC; + + $type->save(); + } +} From 69197039bc162816cbb4eeadad8f3aae145be55a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:18:07 +0200 Subject: [PATCH 08/14] Payments --- app/PaymentDrivers/Mollie/KBC.php | 141 +++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-) diff --git a/app/PaymentDrivers/Mollie/KBC.php b/app/PaymentDrivers/Mollie/KBC.php index 826fc72017..1e90725b6c 100644 --- a/app/PaymentDrivers/Mollie/KBC.php +++ b/app/PaymentDrivers/Mollie/KBC.php @@ -12,8 +12,15 @@ namespace App\PaymentDrivers\Mollie; +use App\Exceptions\PaymentFailed; use App\Http\Requests\Request; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; +use App\Jobs\Mail\PaymentFailureMailer; +use App\Jobs\Util\SystemLogger; +use App\Models\GatewayType; +use App\Models\Payment; +use App\Models\PaymentType; +use App\Models\SystemLog; use App\PaymentDrivers\Common\MethodInterface; use App\PaymentDrivers\MolliePaymentDriver; use Illuminate\Http\RedirectResponse; @@ -31,7 +38,7 @@ class KBC implements MethodInterface } /** - * Show the authorization page for bank transfer. + * Show the authorization page for KBC. * * @param array $data * @return View @@ -42,7 +49,7 @@ class KBC implements MethodInterface } /** - * Handle the authorization for bank transfer. + * Handle the authorization for KBC. * * @param Request $request * @return RedirectResponse @@ -52,11 +59,141 @@ class KBC implements MethodInterface return redirect()->route('client.payment_methods.index'); } + /** + * Show the payment page for KBC. + * + * @param array $data + * @return Redirector|RedirectResponse + */ public function paymentView(array $data) { + $this->mollie->payment_hash + ->withData('gateway_type_id', GatewayType::KBC) + ->withData('client_id', $this->mollie->client->id); + + try { + $payment = $this->mollie->gateway->payments->create([ + 'method' => 'kbc', + 'amount' => [ + 'currency' => $this->mollie->client->currency()->code, + 'value' => $this->mollie->convertToMollieAmount((float) $this->mollie->payment_hash->data->amount_with_fee), + ], + 'description' => \sprintf('Invoices: %s', collect($data['invoices'])->pluck('invoice_number')), + 'redirectUrl' => route('client.payments.response', [ + 'company_gateway_id' => $this->mollie->company_gateway->id, + 'payment_hash' => $this->mollie->payment_hash->hash, + 'payment_method_id' => GatewayType::KBC, + ]), + 'webhookUrl' => $this->mollie->company_gateway->webhookUrl(), + 'metadata' => [ + 'client_id' => $this->mollie->client->hashed_id, + ], + ]); + + $this->mollie->payment_hash->withData('payment_id', $payment->id); + + return redirect( + $payment->getCheckoutUrl() + ); + } catch (\Mollie\Api\Exceptions\ApiException | \Exception $exception) { + return $this->processUnsuccessfulPayment($exception); + } } + /** + * Handle unsuccessful payment. + * + * @param Exception $exception + * @throws PaymentFailed + * @return void + */ + public function processUnsuccessfulPayment(\Exception $exception): void + { + PaymentFailureMailer::dispatch( + $this->mollie->client, + $exception->getMessage(), + $this->mollie->client->company, + $this->mollie->payment_hash->data->amount_with_fee + ); + + SystemLogger::dispatch( + $exception->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_MOLLIE, + $this->mollie->client, + $this->mollie->client->company, + ); + + throw new PaymentFailed($exception->getMessage(), $exception->getCode()); + } + + /** + * Handle the payments for the KBC. + * + * @param PaymentResponseRequest $request + * @return mixed + */ public function paymentResponse(PaymentResponseRequest $request) { + if (! \property_exists($this->mollie->payment_hash->data, 'payment_id')) { + return $this->processUnsuccessfulPayment( + new PaymentFailed('Whoops, something went wrong. Missing required [payment_id] parameter. Please contact administrator. Reference hash: ' . $this->mollie->payment_hash->hash) + ); + } + + try { + $payment = $this->mollie->gateway->payments->get( + $this->mollie->payment_hash->data->payment_id + ); + + if ($payment->status === 'paid') { + return $this->processSuccessfulPayment($payment); + } + + if ($payment->status === 'failed') { + return $this->processUnsuccessfulPayment( + new PaymentFailed(ctrans('texts.status_failed')) + ); + } + + return $this->processUnsuccessfulPayment( + new PaymentFailed(ctrans('texts.status_voided')) + ); + } catch (\Mollie\Api\Exceptions\ApiException | \Exception $exception) { + return $this->processUnsuccessfulPayment($exception); + } + } + + /** + * Handle the successful payment for KBC. + * + * @param ResourcesPayment $payment + * @return RedirectResponse + */ + public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment): RedirectResponse + { + $data = [ + 'gateway_type_id' => GatewayType::KBC, + 'amount' => array_sum(array_column($this->mollie->payment_hash->invoices(), 'amount')) + $this->mollie->payment_hash->fee_total, + 'payment_type' => PaymentType::KBC, + 'transaction_reference' => $payment->id, + ]; + + $payment_record = $this->mollie->createPayment( + $data, + $payment->status === 'paid' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING + ); + + SystemLogger::dispatch( + ['response' => $payment, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_MOLLIE, + $this->mollie->client, + $this->mollie->client->company, + ); + + return redirect()->route('client.payments.show', ['payment' => $this->mollie->encodePrimaryKey($payment_record->id)]); } } From 4dfaf8f1a6ef4355f87194c4ef5fe7ca5fc200c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:18:48 +0200 Subject: [PATCH 09/14] Scaffold KBCTest --- .../ClientPortal/Gateways/Mollie/KBCTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php diff --git a/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php b/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php new file mode 100644 index 0000000000..a8ec810fe7 --- /dev/null +++ b/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php @@ -0,0 +1,40 @@ +driver->manage()->deleteAllCookies(); + } + + $this->disableCompanyGateways(); + + CompanyGateway::where('gateway_key', '1bd651fb213ca0c9d66ae3c336dc77e8')->restore(); + + $this->browse(function (Browser $browser) { + $browser + ->visit(new Login()) + ->auth(); + }); + } +} \ No newline at end of file From 76f646fc81bdf02ffe42067dcbc0460c9649d635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:23:16 +0200 Subject: [PATCH 10/14] Tests: Successful payments --- .../ClientPortal/Gateways/Mollie/KBCTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php b/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php index a8ec810fe7..584f5a2868 100644 --- a/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php +++ b/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php @@ -37,4 +37,21 @@ class KBCTest extends DuskTestCase ->auth(); }); } + + public function testSuccessfulPayment(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Undefined.') + ->waitForText('Test profile') + ->press('CBC') + ->radio('final_state', 'paid') + ->press('Continue') + ->waitForText('Details of the payment') + ->assertSee('Completed'); + }); + } } \ No newline at end of file From 4a475d9ef07acc2fcc2f13783c6a1d77361c9898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:24:43 +0200 Subject: [PATCH 11/14] Tests: Failed payments --- app/Models/CompanyGateway.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 5be23f8a6a..90f15049f7 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -377,6 +377,7 @@ class CompanyGateway extends BaseModel public function webhookUrl() { + return 'https://invoiceninja.com'; return route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $this->hashed_id]); } From dec33daae37d23389d89860f1f949abe37a6cbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 24 Sep 2021 23:26:36 +0200 Subject: [PATCH 12/14] Tests: Canceled payments --- app/Models/CompanyGateway.php | 1 - .../ClientPortal/Gateways/Mollie/KBCTest.php | 32 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 90f15049f7..5be23f8a6a 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -377,7 +377,6 @@ class CompanyGateway extends BaseModel public function webhookUrl() { - return 'https://invoiceninja.com'; return route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $this->hashed_id]); } diff --git a/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php b/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php index 584f5a2868..a8be7b6cbf 100644 --- a/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php +++ b/tests/Browser/ClientPortal/Gateways/Mollie/KBCTest.php @@ -54,4 +54,36 @@ class KBCTest extends DuskTestCase ->assertSee('Completed'); }); } + + public function testFailedPayment(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Undefined.') + ->waitForText('Test profile') + ->press('CBC') + ->radio('final_state', 'failed') + ->press('Continue') + ->waitForText('Failed.'); + }); + } + + public function testCancelledTest(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Undefined.') + ->waitForText('Test profile') + ->press('CBC') + ->radio('final_state', 'canceled') + ->press('Continue') + ->waitForText('Cancelled.'); + }); + } } \ No newline at end of file From 331070174e13cec3f89605d3cac8b6fcc719421a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Sun, 26 Sep 2021 20:56:09 +0200 Subject: [PATCH 13/14] Add translation for KBC/CBC --- resources/lang/en/texts.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index bb078c1f04..a7de19c420 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4314,6 +4314,7 @@ $LANG = array( 'generic_gateway_error' => 'Gateway configuration error. Please check your credentials.', 'my_documents' => 'My documents', 'payment_method_cannot_be_preauthorized' => 'This payment method cannot be preauthorized.', + 'kbc_cbc' => 'KBC/CBC', ); return $LANG; From 1890ba5227f1e4e09dc42031204c323208536890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Sun, 26 Sep 2021 20:59:32 +0200 Subject: [PATCH 14/14] Add KBC to GatewayType --- app/Models/GatewayType.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index c79271c1c6..dff3fc2e4f 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -67,7 +67,10 @@ class GatewayType extends StaticModel case self::SEPA: return ctrans('texts.sepa'); break; - + case self::KBC: + return ctrans('texts.kbc_cbc'); + break; + default: return 'Undefined.'; break;