diff --git a/app/Http/Controllers/Gateways/Mollie3dsController.php b/app/Http/Controllers/Gateways/Mollie3dsController.php index 7f6a0d9121dd..0e3d8f8c113f 100644 --- a/app/Http/Controllers/Gateways/Mollie3dsController.php +++ b/app/Http/Controllers/Gateways/Mollie3dsController.php @@ -14,11 +14,14 @@ namespace App\Http\Controllers\Gateways; use App\Http\Controllers\Controller; use App\Http\Requests\Gateways\Mollie\Mollie3dsRequest; +use App\Models\PaymentHash; class Mollie3dsController extends Controller { public function index(Mollie3dsRequest $request) { - dd($request->all()); + return $request->getCompanyGateway() + ->driver($request->getClient()) + ->process3dsConfirmation($request); } } diff --git a/app/Http/Requests/Gateways/Mollie/Mollie3dsRequest.php b/app/Http/Requests/Gateways/Mollie/Mollie3dsRequest.php index 85415e6e8dc9..8c06791cd957 100644 --- a/app/Http/Requests/Gateways/Mollie/Mollie3dsRequest.php +++ b/app/Http/Requests/Gateways/Mollie/Mollie3dsRequest.php @@ -12,10 +12,18 @@ namespace App\Http\Requests\Gateways\Mollie; +use App\Models\Client; +use App\Models\ClientGatewayToken; +use App\Models\Company; +use App\Models\CompanyGateway; +use App\Models\PaymentHash; +use App\Utils\Traits\MakesHash; use Illuminate\Foundation\Http\FormRequest; class Mollie3dsRequest extends FormRequest { + use MakesHash; + /** * Determine if the user is authorized to make this request. * @@ -23,7 +31,7 @@ class Mollie3dsRequest extends FormRequest */ public function authorize() { - return false; + return true; } /** @@ -37,4 +45,29 @@ class Mollie3dsRequest extends FormRequest // ]; } + + public function getCompany(): ?Company + { + return Company::where('company_key', $this->company_key)->first(); + } + + public function getCompanyGateway(): ?CompanyGateway + { + return CompanyGateway::find($this->decodePrimaryKey($this->company_gateway_id)); + } + + public function getPaymentHash(): ?PaymentHash + { + return PaymentHash::where('hash', $this->hash)->first(); + } + + public function getClient(): ?Client + { + return Client::find($this->getPaymentHash()->data->client_id); + } + + public function getPaymentId(): ?string + { + return $this->getPaymentHash()->data->payment_id; + } } diff --git a/app/Models/PaymentHash.php b/app/Models/PaymentHash.php index 0839120f5a9a..c2ea7232b651 100644 --- a/app/Models/PaymentHash.php +++ b/app/Models/PaymentHash.php @@ -41,7 +41,7 @@ class PaymentHash extends Model return $this->belongsTo(Invoice::class, 'fee_invoice_id', 'id'); } - public function withData(string $property, $value): PaymentHash + public function withData(string $property, $value): self { $this->data = array_merge((array) $this->data, [$property => $value]); $this->save(); diff --git a/app/PaymentDrivers/Mollie/CreditCard.php b/app/PaymentDrivers/Mollie/CreditCard.php index c7495ca6b52a..38886e65d722 100644 --- a/app/PaymentDrivers/Mollie/CreditCard.php +++ b/app/PaymentDrivers/Mollie/CreditCard.php @@ -50,21 +50,26 @@ class CreditCard */ public function paymentResponse(PaymentResponseRequest $request) { - $this->mollie->payment_hash->withData('gateway_type_id', GatewayType::CREDIT_CARD); + // TODO: Unit tests. + $amount = number_format((float) $this->mollie->payment_hash->data->amount_with_fee, 2, '.', ''); + + $this->mollie->payment_hash + ->withData('gateway_type_id', GatewayType::CREDIT_CARD) + ->withData('client_id', $this->mollie->client->id); try { $payment = $this->mollie->gateway->payments->create([ 'amount' => [ 'currency' => $this->mollie->client->currency()->code, - 'value' => Number::formatValue($this->mollie->payment_hash->data->amount_with_fee, $this->mollie->client->currency()), + 'value' => $amount, ], 'description' => \sprintf('Hash: %s', $this->mollie->payment_hash->hash), - 'redirectUrl' => 'https://webshop.example.org/order/12345/', - 'webhookUrl' => route('mollie.3ds_redirect', [ + 'redirectUrl' => route('mollie.3ds_redirect', [ 'company_key' => $this->mollie->client->company->company_key, 'company_gateway_id' => $this->mollie->company_gateway->hashed_id, 'hash' => $this->mollie->payment_hash->hash, ]), + 'webhookUrl' => 'https://invoiceninja.com', 'cardToken' => $request->token, ]); @@ -78,6 +83,8 @@ class CreditCard } if ($payment->status === 'open') { + $this->mollie->payment_hash->withData('payment_id', $payment->id); + return redirect($payment->getCheckoutUrl()); } } catch (\Exception $e) { @@ -87,10 +94,8 @@ class CreditCard } } - protected function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment) + public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment) { - // Check if storing credit card is enabled - $payment_hash = $this->mollie->payment_hash; $data = [ @@ -131,5 +136,7 @@ class CreditCard $this->mollie->client, $this->mollie->client->company, ); + + throw new PaymentFailed($e->getMessage(), $e->getCode()); } } diff --git a/app/PaymentDrivers/MolliePaymentDriver.php b/app/PaymentDrivers/MolliePaymentDriver.php index 6cdcb8b97676..88d7883ac359 100644 --- a/app/PaymentDrivers/MolliePaymentDriver.php +++ b/app/PaymentDrivers/MolliePaymentDriver.php @@ -12,6 +12,7 @@ namespace App\PaymentDrivers; +use App\Http\Requests\Gateways\Mollie\Mollie3dsRequest; use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -122,4 +123,19 @@ class MolliePaymentDriver extends BaseDriver public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null) { } + + public function process3dsConfirmation(Mollie3dsRequest $request) + { + $this->init(); + + $this->setPaymentHash($request->getPaymentHash()); + + try { + $payment = $this->gateway->payments->get($request->getPaymentId()); + + return (new CreditCard($this))->processSuccessfulPayment($payment); + } catch(\Mollie\Api\Exceptions\ApiException $e) { + return (new CreditCard($this))->processUnsuccessfulPayment($e); + } + } }