diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 9c6edf68e91c..209be095aff8 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -89,9 +89,10 @@ 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::BANK_TRANSFER => ['refund' => false, 'token_billing' => true], GatewayType::KBC => ['refund' => false, 'token_billing' => false], + GatewayType::BANCONTACT => ['refund' => false, 'token_billing' => false], ]; case 15: return [GatewayType::PAYPAL => ['refund' => true, 'token_billing' => false]]; //Paypal diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index dff3fc2e4f60..6435db19b0ef 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -26,6 +26,7 @@ class GatewayType extends StaticModel const SEPA = 9; const CREDIT = 10; const KBC = 11; + const BANCONTACT = 12; public function gateway() { @@ -70,6 +71,9 @@ class GatewayType extends StaticModel case self::KBC: return ctrans('texts.kbc_cbc'); break; + case self::BANCONTACT: + return ctrans('texts.bancontact'); + break; default: return 'Undefined.'; diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index d8c0cf2c78e0..7b64be5733cd 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -44,6 +44,7 @@ class PaymentType extends StaticModel const CRYPTO = 31; const MOLLIE_BANK_TRANSFER = 34; const KBC = 35; + const BANCONTACT = 36; public static function parseCardType($cardName) { diff --git a/app/PaymentDrivers/Mollie/Bancontact.php b/app/PaymentDrivers/Mollie/Bancontact.php new file mode 100644 index 000000000000..53199e8eee55 --- /dev/null +++ b/app/PaymentDrivers/Mollie/Bancontact.php @@ -0,0 +1,216 @@ +mollie = $mollie; + + $this->mollie->init(); + } + + /** + * Show the authorization page for Bancontact. + * + * @param array $data + * @return View + */ + public function authorizeView(array $data): View + { + return render('gateways.mollie.bancontact.authorize', $data); + } + + /** + * Handle the authorization for Bancontact. + * + * @param Request $request + * @return RedirectResponse + */ + public function authorizeResponse(Request $request): RedirectResponse + { + return redirect()->route('client.payment_methods.index'); + } + + /** + * Show the payment page for Bancontact. + * + * @param array $data + * @return Redirector|RedirectResponse + */ + public function paymentView(array $data) + { + $this->mollie->payment_hash + ->withData('gateway_type_id', GatewayType::BANCONTACT) + ->withData('client_id', $this->mollie->client->id); + + try { + $payment = $this->mollie->gateway->payments->create([ + 'method' => 'bancontact', + '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::BANCONTACT, + ]), + '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 === 'open') { + return $this->processOpenPayment($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 Bancontact. + * + * @param string $status + * @param ResourcesPayment $payment + * @return RedirectResponse + */ + public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment, string $status = 'paid'): RedirectResponse + { + $data = [ + 'gateway_type_id' => GatewayType::BANCONTACT, + 'amount' => array_sum(array_column($this->mollie->payment_hash->invoices(), 'amount')) + $this->mollie->payment_hash->fee_total, + 'payment_type' => PaymentType::BANCONTACT, + 'transaction_reference' => $payment->id, + ]; + + $payment_record = $this->mollie->createPayment( + $data, + $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)]); + } + + /** + * Handle 'open' payment status for Bancontact. + * + * @param ResourcesPayment $payment + * @return RedirectResponse + */ + public function processOpenPayment(\Mollie\Api\Resources\Payment $payment): RedirectResponse + { + return $this->processSuccessfulPayment($payment, 'open'); + } +} diff --git a/app/PaymentDrivers/MolliePaymentDriver.php b/app/PaymentDrivers/MolliePaymentDriver.php index f9e2644668a5..e5566eb7a1af 100644 --- a/app/PaymentDrivers/MolliePaymentDriver.php +++ b/app/PaymentDrivers/MolliePaymentDriver.php @@ -24,6 +24,7 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; +use App\PaymentDrivers\Mollie\Bancontact; use App\PaymentDrivers\Mollie\BankTransfer; use App\PaymentDrivers\Mollie\CreditCard; use App\PaymentDrivers\Mollie\KBC; @@ -66,6 +67,7 @@ class MolliePaymentDriver extends BaseDriver */ public static $methods = [ GatewayType::CREDIT_CARD => CreditCard::class, + GatewayType::BANCONTACT => Bancontact::class, GatewayType::BANK_TRANSFER => BankTransfer::class, GatewayType::KBC => KBC::class, ]; @@ -88,6 +90,7 @@ class MolliePaymentDriver extends BaseDriver $types = []; $types[] = GatewayType::CREDIT_CARD; + $types[] = GatewayType::BANCONTACT; $types[] = GatewayType::BANK_TRANSFER; $types[] = GatewayType::KBC; diff --git a/database/migrations/2021_09_24_213858_add_bancontact_to_payment_types.php b/database/migrations/2021_09_24_213858_add_bancontact_to_payment_types.php new file mode 100644 index 000000000000..a4daab9c6821 --- /dev/null +++ b/database/migrations/2021_09_24_213858_add_bancontact_to_payment_types.php @@ -0,0 +1,26 @@ +id = 35; + $type->name = 'Bancontact'; + $type->gateway_type_id = GatewayType::BANCONTACT; + + $type->save(); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index a7de19c42014..14df4963a7c2 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4315,6 +4315,7 @@ $LANG = array( 'my_documents' => 'My documents', 'payment_method_cannot_be_preauthorized' => 'This payment method cannot be preauthorized.', 'kbc_cbc' => 'KBC/CBC', + 'bancontact' => 'Bancontact', ); return $LANG; diff --git a/resources/views/portal/ninja2020/gateways/mollie/bancontact/authorize.blade.php b/resources/views/portal/ninja2020/gateways/mollie/bancontact/authorize.blade.php new file mode 100644 index 000000000000..7d739d29532d --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/mollie/bancontact/authorize.blade.php @@ -0,0 +1,8 @@ +@extends('portal.ninja2020.layout.payments', ['gateway_title' => 'Bancontact', 'card_title' => +'Bancontact']) + +@section('gateway_content') + @component('portal.ninja2020.components.general.card-element-single') + {{ __('texts.payment_method_cannot_be_preauthorized') }} + @endcomponent +@endsection \ No newline at end of file diff --git a/tests/Browser/ClientPortal/Gateways/Mollie/BancontactTest.php b/tests/Browser/ClientPortal/Gateways/Mollie/BancontactTest.php new file mode 100644 index 000000000000..659a8cae6594 --- /dev/null +++ b/tests/Browser/ClientPortal/Gateways/Mollie/BancontactTest.php @@ -0,0 +1,102 @@ +driver->manage()->deleteAllCookies(); + } + + $this->disableCompanyGateways(); + + CompanyGateway::where('gateway_key', '1bd651fb213ca0c9d66ae3c336dc77e8')->restore(); + + $this->browse(function (Browser $browser) { + $browser + ->visit(new Login()) + ->auth(); + }); + } + + public function testSuccessfulPayment(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Bancontact') + ->waitForText('Test profile') + ->radio('final_state', 'paid') + ->press('Continue') + ->waitForText('Details of the payment') + ->assertSee('Completed'); + }); + } + + public function testOpenPayments(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Bancontact') + ->waitForText('Test profile') + ->radio('final_state', 'open') + ->press('Continue') + ->waitForText('Details of the payment') + ->assertSee('Pending'); + }); + } + + public function testFailedPayment(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Bancontact') + ->waitForText('Test profile') + ->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('Bancontact') + ->waitForText('Test profile') + ->radio('final_state', 'canceled') + ->press('Continue') + ->waitForText('Cancelled.'); + }); + } +}