From 684533a5914c587587463fa33bf8fe7e24104196 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 20 Jul 2024 13:36:38 +1000 Subject: [PATCH] Fixes for paypal driver --- .../PayPal/PayPalBasePaymentDriver.php | 86 +++++++++++-------- .../PayPalRestPaymentDriver.php | 79 ++++++++++------- .../gateways/paypal/ppcp/card.blade.php | 32 +++---- 3 files changed, 106 insertions(+), 91 deletions(-) diff --git a/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php b/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php index 4e01c7582c0b..ec637548d3ae 100644 --- a/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php +++ b/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php @@ -530,56 +530,68 @@ class PayPalBasePaymentDriver extends BaseDriver public function createNinjaPayment($request, $response) { - $data = [ - 'payment_type' => $this->getPaymentMethod($request->gateway_type_id), - 'amount' => $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'], - 'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'], - 'gateway_type_id' => GatewayType::PAYPAL, - ]; + if(isset($response['purchase_units'][0]['payments']['captures'][0]['status']) && in_array($response['purchase_units'][0]['payments']['captures'][0]['status'], ['COMPLETED', 'PENDING'])) + { - $payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); + $payment_status = $response['purchase_units'][0]['payments']['captures'][0]['status'] == 'COMPLETED' ? \App\Models\Payment::STATUS_COMPLETED : \App\Models\Payment::STATUS_PENDING; - if ($request->has('store_card') && $request->input('store_card') === true) { - $payment_source = $response->json()['payment_source'] ?? false; + $data = [ + 'payment_type' => $this->getPaymentMethod($request->gateway_type_id), + 'amount' => $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'], + 'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'], + 'gateway_type_id' => GatewayType::PAYPAL, + ]; - if(isset($payment_source['card']) && ($payment_source['card']['attributes']['vault']['status'] ?? false) && $payment_source['card']['attributes']['vault']['status'] == 'VAULTED') { + $payment = $this->createPayment($data, $payment_status); - $last4 = $payment_source['card']['last_digits']; - $expiry = $payment_source['card']['expiry']; //'2025-01' - $expiry_meta = explode('-', $expiry); - $brand = $payment_source['card']['brand']; + if ($request->has('store_card') && $request->input('store_card') === true) { + $payment_source = $response->json()['payment_source'] ?? false; - $payment_meta = new \stdClass(); - $payment_meta->exp_month = $expiry_meta[1] ?? ''; - $payment_meta->exp_year = $expiry_meta[0] ?? $expiry; - $payment_meta->brand = $brand; - $payment_meta->last4 = $last4; - $payment_meta->type = GatewayType::CREDIT_CARD; + if(isset($payment_source['card']) && ($payment_source['card']['attributes']['vault']['status'] ?? false) && $payment_source['card']['attributes']['vault']['status'] == 'VAULTED') { - $token = $payment_source['card']['attributes']['vault']['id']; // 09f28652d01257021 - $gateway_customer_reference = $payment_source['card']['attributes']['vault']['customer']['id']; //rbTHnLsZqE; + $last4 = $payment_source['card']['last_digits']; + $expiry = $payment_source['card']['expiry']; //'2025-01' + $expiry_meta = explode('-', $expiry); + $brand = $payment_source['card']['brand']; - $data['token'] = $token; - $data['payment_method_id'] = GatewayType::PAYPAL_ADVANCED_CARDS; - $data['payment_meta'] = $payment_meta; + $payment_meta = new \stdClass(); + $payment_meta->exp_month = $expiry_meta[1] ?? ''; + $payment_meta->exp_year = $expiry_meta[0] ?? $expiry; + $payment_meta->brand = $brand; + $payment_meta->last4 = $last4; + $payment_meta->type = GatewayType::CREDIT_CARD; - $additional['gateway_customer_reference'] = $gateway_customer_reference; + $token = $payment_source['card']['attributes']['vault']['id']; // 09f28652d01257021 + $gateway_customer_reference = $payment_source['card']['attributes']['vault']['customer']['id']; //rbTHnLsZqE; - $this->storeGatewayToken($data, $additional); + $data['token'] = $token; + $data['payment_method_id'] = GatewayType::PAYPAL_ADVANCED_CARDS; + $data['payment_meta'] = $payment_meta; + $additional['gateway_customer_reference'] = $gateway_customer_reference; + + $this->storeGatewayToken($data, $additional); + + } } + + SystemLogger::dispatch( + ['response' => $response->json(), 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_PAYPAL, + $this->client, + $this->client->company, + ); + + return response()->json(['redirect' => route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)], false)]); } - SystemLogger::dispatch( - ['response' => $response->json(), 'data' => $data], - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_SUCCESS, - SystemLog::TYPE_PAYPAL, - $this->client, - $this->client->company, - ); - - return response()->json(['redirect' => route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)], false)]); + SystemLogger::dispatch($response, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_PAYPAL, $this->client, $this->client->company); + + $error = isset($response['purchase_units'][0]['payments']['captures'][0]['status_details'][0]) ? $response['purchase_units'][0]['payments']['captures'][0]['status_details'][0] : $response['purchase_units'][0]['payments']['captures'][0]['status']; + + return response()->json(['message' => $error], 400); } diff --git a/app/PaymentDrivers/PayPalRestPaymentDriver.php b/app/PaymentDrivers/PayPalRestPaymentDriver.php index d6e8dbadcf5c..524b59f8c632 100644 --- a/app/PaymentDrivers/PayPalRestPaymentDriver.php +++ b/app/PaymentDrivers/PayPalRestPaymentDriver.php @@ -288,8 +288,6 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver $orderId = $this->createOrder($data); - // $r = $this->gatewayRequest("/v2/checkout/orders/{$orderId}", 'get', ['body' => '']); - try { $r = $this->gatewayRequest("/v2/checkout/orders/{$orderId}", 'get', ['body' => '']); @@ -318,25 +316,33 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver $response = $r->json(); - $data = [ - 'payment_type' => $this->getPaymentMethod($request->gateway_type_id), - 'amount' => $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'], - 'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'], - 'gateway_type_id' => $this->gateway_type_id, - ]; + if(isset($response['purchase_units'][0]['payments']['captures'][0]['status']) && $response['purchase_units'][0]['payments']['captures'][0]['status'] == 'COMPLETED') + { + $data = [ + 'payment_type' => $this->getPaymentMethod($request->gateway_type_id), + 'amount' => $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'], + 'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'], + 'gateway_type_id' => $this->gateway_type_id, + ]; - $payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); + $payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); - SystemLogger::dispatch( - ['response' => $response, 'data' => $data], - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_SUCCESS, - SystemLog::TYPE_PAYPAL, - $this->client, - $this->client->company, - ); + SystemLogger::dispatch( + ['response' => $response, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_PAYPAL, + $this->client, + $this->client->company, + ); - return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); + return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); + + } + + + + } @@ -387,24 +393,31 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver $response = $r->json(); - $data = [ - 'payment_type' => $this->getPaymentMethod((string)$cgt->gateway_type_id), - 'amount' => $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'], - 'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'], - 'gateway_type_id' => $this->gateway_type_id, - ]; + if(isset($response['purchase_units'][0]['payments']['captures'][0]['status']) && $response['purchase_units'][0]['payments']['captures'][0]['status'] == 'COMPLETED') + { - $payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); + $data = [ + 'payment_type' => $this->getPaymentMethod((string)$cgt->gateway_type_id), + 'amount' => $response['purchase_units'][0]['payments']['captures'][0]['amount']['value'], + 'transaction_reference' => $response['purchase_units'][0]['payments']['captures'][0]['id'], + 'gateway_type_id' => $this->gateway_type_id, + ]; - SystemLogger::dispatch( - ['response' => $response, 'data' => $data], - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_SUCCESS, - SystemLog::TYPE_PAYPAL_PPCP, - $this->client, - $this->client->company, - ); + $payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); + SystemLogger::dispatch( + ['response' => $response, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_PAYPAL_PPCP, + $this->client, + $this->client->company, + ); + } + + $this->processInternallyFailedPayment($this, new \Exception('Auto billing failed.', 400)); + + SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_PAYPAL, $this->client, $this->client->company); } } diff --git a/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php b/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php index 63e1578f3dd8..43554bebb306 100644 --- a/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php +++ b/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php @@ -14,10 +14,10 @@ @endphp @section('gateway_head') - + script-src 'self' https://c.paypal.com;"> --> @endsection @section('gateway_content') @@ -86,7 +86,7 @@ } - + @if(isset($merchantId)) @@ -138,10 +138,15 @@ body: formData, }) .then(response => { + if (!response.ok) { - throw new Error('Network response was not ok ' + response.statusText); + return response.json().then(errorData => { + throw new Error(errorData.message); + }); } + return response.json(); + }) .then(data => { @@ -164,7 +169,6 @@ document.getElementById('errors').textContent = `Sorry, your transaction could not be processed...\n\n${error.message}`; document.getElementById('errors').hidden = false; - }); }, @@ -172,20 +176,6 @@ window.location.href = "/client/invoices/"; }, - // onError: function(error) { - - - // console.log("submit catch"); - // const errorM = parseError(error); - - // console.log(errorM); - - // const msg = handle422Error(errorM); - - // document.getElementById('errors').textContent = `Sorry, your transaction could not be processed...\n\n${msg.description}`; - // document.getElementById('errors').hidden = false; - - // }, onClick: function (){ } @@ -195,8 +185,8 @@ // Render each field after checking for eligibility if (cardField.isEligible()) { - // const nameField = cardField.NameField(); - // nameField.render("#card-name-field-container"); + const nameField = cardField.NameField(); + nameField.render("#card-name-field-container"); const numberField = cardField.NumberField({ inputEvents: {