From 4818ea81bcd5cf1e5d43a80862dc05a9ee3ab9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 23 Jul 2021 16:17:32 +0200 Subject: [PATCH] Credit card: Pay with new credit card --- app/PaymentDrivers/Mollie/CreditCard.php | 72 ++++++++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/app/PaymentDrivers/Mollie/CreditCard.php b/app/PaymentDrivers/Mollie/CreditCard.php index 3f227c76e634..63c425e2228a 100644 --- a/app/PaymentDrivers/Mollie/CreditCard.php +++ b/app/PaymentDrivers/Mollie/CreditCard.php @@ -4,12 +4,14 @@ namespace App\PaymentDrivers\Mollie; use App\Exceptions\PaymentFailed; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; +use App\Jobs\Util\SystemLogger; +use App\Models\GatewayType; +use App\Models\Payment; +use App\Models\PaymentType; +use App\Models\SystemLog; use App\PaymentDrivers\MolliePaymentDriver; use Illuminate\Contracts\View\Factory; use Illuminate\View\View; -use Illuminate\Contracts\Container\BindingResolutionException; - -use function Symfony\Component\String\b; class CreditCard { @@ -38,26 +40,72 @@ class CreditCard return render('gateways.mollie.credit_card.pay', $data); } + /** + * Create a payment object. + * + * @param PaymentResponseRequest $request + * @return mixed + */ public function paymentResponse(PaymentResponseRequest $request) { + $amount = number_format((float) $this->mollie->payment_hash->data->amount_with_fee, 2, '.', ''); + try { $payment = $this->mollie->gateway->payments->create([ - "amount" => [ - "currency" => "USD", - "value" => "10.00" + 'amount' => [ + 'currency' => $this->mollie->client->currency()->code, + 'value' => $amount, ], - "description" => "Order #12345", - "redirectUrl" => "https://webshop.example.org/order/12345/", - "webhookUrl" => "https://webshop.example.org/mollie-webhook/", + 'description' => \sprintf('Hash: %s', $this->mollie->payment_hash->hash), + 'redirectUrl' => 'https://webshop.example.org/order/12345/', + 'webhookUrl' => 'https://webshop.example.org/mollie-webhook/', + 'cardToken' => $request->token, ]); - if ($payment->status === 'open') { - return redirect($payment->getCheckoutUrl()); + if ($payment->status === 'paid') { + $this->mollie->logSuccessfulGatewayResponse( + ['response' => $payment, 'data' => $this->mollie->payment_hash], + SystemLog::TYPE_MOLLIE + ); + + $this->processSuccessfulPayment($payment); } + + if ($payment->status === 'open') { + // Handle redirect payment + } + + dd($payment); + } catch (\Exception $e) { throw new PaymentFailed($e->getMessage(), $e->getCode()); } + } - dd($payment); + protected function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment) + { + // Check if storing credit card is enabled + + $payment_hash = $this->mollie->payment_hash; + + $data = [ + 'gateway_type_id' => GatewayType::CREDIT_CARD, + 'amount' => array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total, + 'payment_type' => PaymentType::CREDIT_CARD_OTHER, + 'transaction_reference' => $payment->id, + ]; + + $payment_record = $this->mollie->createPayment($data, Payment::STATUS_COMPLETED); + + 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)]); } }