Credit card 3ds processing

This commit is contained in:
Benjamin Beganović 2021-07-29 15:13:38 +02:00
parent 3d12fd80e8
commit 1e2e55c9e4
5 changed files with 69 additions and 10 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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());
}
}

View File

@ -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);
}
}
}