diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 4414a685c9b0..0e05a43835c6 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -90,6 +90,7 @@ class Gateway extends StaticModel case 7: return [ GatewayType::CREDIT_CARD => ['refund' => false, 'token_billing' => true], // Mollie + GatewayType::BANK_TRANSFER => ['refund' => false, 'token_billing' => true], ]; case 15: return [GatewayType::PAYPAL => ['refund' => true, 'token_billing' => false]]; //Paypal diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index b6b28cd4c8f1..37723d0a01f3 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 MOLLIE_BANK_TRANSFER = 34; public static function parseCardType($cardName) { diff --git a/app/PaymentDrivers/Mollie/BankTransfer.php b/app/PaymentDrivers/Mollie/BankTransfer.php new file mode 100644 index 000000000000..bf8595e090bf --- /dev/null +++ b/app/PaymentDrivers/Mollie/BankTransfer.php @@ -0,0 +1,212 @@ +mollie = $mollie; + + $this->mollie->init(); + } + + /** + * Show the authorization page for bank transfer. + * + * @param array $data + * @return View + */ + public function authorizeView(array $data): View + { + return render('gateways.mollie.bank_transfer.authorize', $data); + } + + /** + * Handle the authorization for bank transfer. + * + * @param Request $request + * @return RedirectResponse + */ + public function authorizeResponse(Request $request): RedirectResponse + { + return redirect()->route('client.payment_methods.index'); + } + + /** + * Show the payment page for bank transfer. + * + * @param array $data + * @return Redirector|RedirectResponse + */ + public function paymentView(array $data) + { + $this->mollie->payment_hash + ->withData('gateway_type_id', GatewayType::BANK_TRANSFER) + ->withData('client_id', $this->mollie->client->id); + + try { + $payment = $this->mollie->gateway->payments->create([ + 'method' => 'banktransfer', + '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::BANK_TRANSFER, + ]), + '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 $e + * @throws PaymentFailed + * @return void + */ + public function processUnsuccessfulPayment(\Exception $e): void + { + PaymentFailureMailer::dispatch( + $this->mollie->client, + $e->getMessage(), + $this->mollie->client->company, + $this->mollie->payment_hash->data->amount_with_fee + ); + + SystemLogger::dispatch( + $e->getMessage(), + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_MOLLIE, + $this->mollie->client, + $this->mollie->client->company, + ); + + throw new PaymentFailed($e->getMessage(), $e->getCode()); + } + + /** + * Handle the payments for the bank transfer. + * + * @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); + } + + 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 bank transfer. + * + * @param ResourcesPayment $payment + * @param string $status + * @return RedirectResponse + */ + public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment, $status = 'paid'): RedirectResponse + { + $data = [ + 'gateway_type_id' => GatewayType::BANK_TRANSFER, + 'amount' => array_sum(array_column($this->mollie->payment_hash->invoices(), 'amount')) + $this->mollie->payment_hash->fee_total, + 'payment_type' => PaymentType::MOLLIE_BANK_TRANSFER, + '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 bank transfer. + * + * @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 1bffcca72cdf..18b808cb667d 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\BankTransfer; use App\PaymentDrivers\Mollie\CreditCard; use App\Utils\Traits\MakesHash; use Illuminate\Support\Facades\Validator; @@ -64,6 +65,7 @@ class MolliePaymentDriver extends BaseDriver */ public static $methods = [ GatewayType::CREDIT_CARD => CreditCard::class, + GatewayType::BANK_TRANSFER => BankTransfer::class, ]; const SYSTEM_LOG_TYPE = SystemLog::TYPE_MOLLIE; @@ -84,6 +86,7 @@ class MolliePaymentDriver extends BaseDriver $types = []; $types[] = GatewayType::CREDIT_CARD; + $types[] = GatewayType::BANK_TRANSFER; return $types; } diff --git a/database/migrations/2021_09_24_201319_add_mollie_bank_transfer_to_payment_types.php b/database/migrations/2021_09_24_201319_add_mollie_bank_transfer_to_payment_types.php new file mode 100644 index 000000000000..0f00a831f77a --- /dev/null +++ b/database/migrations/2021_09_24_201319_add_mollie_bank_transfer_to_payment_types.php @@ -0,0 +1,24 @@ +id = 34; + $type->name = 'Mollie Bank Transfer'; + $type->gateway_type_id = GatewayType::BANK_TRANSFER; + + $type->save(); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 04f3816d3927..bb078c1f043b 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/bank_transfer/authorize.blade.php b/resources/views/portal/ninja2020/gateways/mollie/bank_transfer/authorize.blade.php new file mode 100644 index 000000000000..abe8eb9470f1 --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/mollie/bank_transfer/authorize.blade.php @@ -0,0 +1,8 @@ +@extends('portal.ninja2020.layout.payments', ['gateway_title' => ctrans('texts.bank_transfer'), 'card_title' => +ctrans('texts.bank_transfer')]) + +@section('gateway_content') + @component('portal.ninja2020.components.general.card-element-single') + {{ __('texts.payment_method_cannot_be_preauthorized') }} + @endcomponent +@endsection diff --git a/tests/Browser/ClientPortal/Gateways/Mollie/BankTransferTest.php b/tests/Browser/ClientPortal/Gateways/Mollie/BankTransferTest.php new file mode 100644 index 000000000000..8feb940e928f --- /dev/null +++ b/tests/Browser/ClientPortal/Gateways/Mollie/BankTransferTest.php @@ -0,0 +1,72 @@ +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('Bank Transfer') + ->waitForText('Test profile') + ->radio('final_state', 'paid') + ->press('Continue') + ->waitForText('Details of the payment') + ->assertSee('Completed'); + }); + } + + public function testPendingPayment(): void + { + $this->browse(function (Browser $browser) { + $browser + ->visitRoute('client.invoices.index') + ->click('@pay-now') + ->press('Pay Now') + ->clickLink('Bank Transfer') + ->waitForText('Test profile') + ->radio('final_state', 'open') + ->press('Continue') + ->waitForText('Details of the payment') + ->assertSee('Pending'); + }); + } +} diff --git a/tests/Browser/ClientPortal/Gateways/Mollie/CreditCardTest.php b/tests/Browser/ClientPortal/Gateways/Mollie/CreditCardTest.php index fd0bad760aa1..4eb872fab74f 100644 --- a/tests/Browser/ClientPortal/Gateways/Mollie/CreditCardTest.php +++ b/tests/Browser/ClientPortal/Gateways/Mollie/CreditCardTest.php @@ -12,6 +12,7 @@ namespace Tests\Browser\ClientPortal\Gateways\Mollie; +use App\Models\CompanyGateway; use Laravel\Dusk\Browser; use Tests\Browser\Pages\ClientPortal\Login; use Tests\DuskTestCase; @@ -26,9 +27,9 @@ class CreditCardTest extends DuskTestCase $browser->driver->manage()->deleteAllCookies(); } - // $this->disableCompanyGateways(); + $this->disableCompanyGateways(); - // CompanyGateway::where('gateway_key', '3758e7f7c6f4cecf0f4f348b9a00f456')->restore(); + CompanyGateway::where('gateway_key', '1bd651fb213ca0c9d66ae3c336dc77e8')->restore(); $this->browse(function (Browser $browser) { $browser