From 990c75db601758a43fc26678131571ac11784b1b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 26 Apr 2024 21:17:15 +1000 Subject: [PATCH] Clean up for paypal rest --- app/Helpers/Epc/EpcQrGenerator.php | 6 +- .../PayPalPPCPPaymentDriver.php | 56 +++++++++++-------- .../PayPalRestPaymentDriver.php | 22 ++++++-- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/app/Helpers/Epc/EpcQrGenerator.php b/app/Helpers/Epc/EpcQrGenerator.php index 520ec11ea9c9..07bfc9080cd6 100644 --- a/app/Helpers/Epc/EpcQrGenerator.php +++ b/app/Helpers/Epc/EpcQrGenerator.php @@ -59,13 +59,13 @@ class EpcQrGenerator {$qr}"; } catch(\Throwable $e) { - nlog("EPC QR failure => ".$e->getMessage()); + // nlog("EPC QR failure => ".$e->getMessage()); return ''; } catch(\Exception $e) { - nlog("EPC QR failure => ".$e->getMessage()); + // nlog("EPC QR failure => ".$e->getMessage()); return ''; } catch(InvalidArgumentException $e) { - nlog("EPC QR failure => ".$e->getMessage()); + // nlog("EPC QR failure => ".$e->getMessage()); return ''; } diff --git a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php index 080251af007b..12758fa10171 100644 --- a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php +++ b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php @@ -233,8 +233,8 @@ class PayPalPPCPPaymentDriver extends BaseDriver /** * Presents the Payment View to the client * - * @param mixed $data - * @return void + * @param array $data + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function processPaymentView($data) { @@ -251,7 +251,6 @@ class PayPalPPCPPaymentDriver extends BaseDriver $data['gateway_type_id'] = $this->gateway_type_id; $data['merchantId'] = $this->company_gateway->getConfigField('merchantId'); $data['currency'] = $this->client->currency()->code; - // nlog($data['merchantId']); return render('gateways.paypal.ppcp.pay', $data); @@ -279,11 +278,11 @@ class PayPalPPCPPaymentDriver extends BaseDriver "op" => "replace", "path" => "/purchase_units/@reference_id=='default'/shipping/address", "value" => [ - "address_line_1" => strlen($this->client->shipping_address1) > 1 ? $this->client->shipping_address1 : $this->client->address1, + "address_line_1" => strlen($this->client->shipping_address1 ?? '') > 1 ? $this->client->shipping_address1 : $this->client->address1, "address_line_2" => $this->client->shipping_address2, - "admin_area_2" => strlen($this->client->shipping_city) > 1 ? $this->client->shipping_city : $this->client->city, - "admin_area_1" => strlen($this->client->shipping_state) > 1 ? $this->client->shipping_state : $this->client->state, - "postal_code" => strlen($this->client->shipping_postal_code) > 1 ? $this->client->shipping_postal_code : $this->client->postal_code, + "admin_area_2" => strlen($this->client->shipping_city ?? '') > 1 ? $this->client->shipping_city : $this->client->city, + "admin_area_1" => strlen($this->client->shipping_state ?? '') > 1 ? $this->client->shipping_state : $this->client->state, + "postal_code" => strlen($this->client->shipping_postal_code ?? '') > 1 ? $this->client->shipping_postal_code : $this->client->postal_code, "country_code" => $this->client->present()->shipping_country_code(), ], ]]; @@ -406,21 +405,32 @@ class PayPalPPCPPaymentDriver extends BaseDriver private function injectPayPalPaymentSource(): array { - return [ + $order = [ "paypal" => [ - "name" => [ "given_name" => $this->client->present()->first_name(), "surname" => $this->client->present()->last_name(), ], "email_address" => $this->client->present()->email(), - "address" => $this->getBillingAddress(), "experience_context" => [ "user_action" => "PAY_NOW" ], ], ]; + if( + strlen($this->client->address1 ?? '') > 2 && + strlen($this->client->city ?? '') > 2 && + strlen($this->client->state ?? '') >= 2 && + strlen($this->client->postal_code ?? '') > 2 && + strlen($this->client->country->iso_3166_2 ?? '') >= 2 + ) + { + $order["paypal"]["address"] = $this->getBillingAddress(); + } + + return $order; + } /** @@ -488,8 +498,6 @@ class PayPalPPCPPaymentDriver extends BaseDriver $r = $this->gatewayRequest('/v2/checkout/orders', 'post', $order); - // nlog($r->json()); - return $r->json()['id']; } @@ -497,14 +505,14 @@ class PayPalPPCPPaymentDriver extends BaseDriver private function getBillingAddress(): array { return - [ - "address_line_1" => $this->client->address1, - "address_line_2" => $this->client->address2, - "admin_area_2" => $this->client->city, - "admin_area_1" => $this->client->state, - "postal_code" => $this->client->postal_code, - "country_code" => $this->client->country->iso_3166_2, - ]; + [ + "address_line_1" => $this->client->address1, + "address_line_2" => $this->client->address2, + "admin_area_2" => $this->client->city, + "admin_area_1" => $this->client->state, + "postal_code" => $this->client->postal_code, + "country_code" => $this->client->country->iso_3166_2, + ]; } private function getShippingAddress(): ?array @@ -513,11 +521,11 @@ class PayPalPPCPPaymentDriver extends BaseDriver [ "address" => [ - "address_line_1" => strlen($this->client->shipping_address1) > 1 ? $this->client->shipping_address1 : $this->client->address1, + "address_line_1" => strlen($this->client->shipping_address1 ?? '') > 1 ? $this->client->shipping_address1 : $this->client->address1, "address_line_2" => $this->client->shipping_address2, - "admin_area_2" => strlen($this->client->shipping_city) > 1 ? $this->client->shipping_city : $this->client->city, - "admin_area_1" => strlen($this->client->shipping_state) > 1 ? $this->client->shipping_state : $this->client->state, - "postal_code" => strlen($this->client->shipping_postal_code) > 1 ? $this->client->shipping_postal_code : $this->client->postal_code, + "admin_area_2" => strlen($this->client->shipping_city ?? '') > 1 ? $this->client->shipping_city : $this->client->city, + "admin_area_1" => strlen($this->client->shipping_state ?? '') > 1 ? $this->client->shipping_state : $this->client->state, + "postal_code" => strlen($this->client->shipping_postal_code ?? '') > 1 ? $this->client->shipping_postal_code : $this->client->postal_code, "country_code" => $this->client->present()->shipping_country_code(), ], ] diff --git a/app/PaymentDrivers/PayPalRestPaymentDriver.php b/app/PaymentDrivers/PayPalRestPaymentDriver.php index 6285cda5b41b..8799162493dc 100644 --- a/app/PaymentDrivers/PayPalRestPaymentDriver.php +++ b/app/PaymentDrivers/PayPalRestPaymentDriver.php @@ -491,7 +491,24 @@ return render('gateways.paypal.pay', $data); ], ]; + /** If we have a complete address, add it to the order, otherwise leave it blank! */ if( + strlen($this->client->shipping_address1 ?? '') > 2 && + strlen($this->client->shipping_city ?? '') > 2 && + strlen($this->client->shipping_state ?? '') >= 2 && + strlen($this->client->shipping_postal_code ?? '') > 2 && + strlen($this->client->shipping_country->iso_3166_2 ?? '') >= 2 + ) { + $order['paypal']['address'] = [ + "address_line_1" => $this->client->shipping_address1, + "address_line_2" => $this->client->shipping_address2, + "admin_area_2" => $this->client->shipping_city, + "admin_area_1" => $this->client->shipping_state, + "postal_code" => $this->client->shipping_postal_code, + "country_code" => $this->client->present()->shipping_country_code(), + ]; + } + elseif( strlen($this->client->address1 ?? '') > 2 && strlen($this->client->city ?? '') > 2 && strlen($this->client->state ?? '') >= 2 && @@ -533,7 +550,6 @@ return render('gateways.paypal.pay', $data); "custom_id" => $this->payment_hash->hash, "description" => ctrans('texts.invoice_number') . '# ' . $invoice->number, "invoice_id" => $invoice->number, - $this->getShippingAddress(), "amount" => [ "value" => (string) $data['amount_with_fee'], "currency_code" => $this->client->currency()->code, @@ -566,13 +582,9 @@ return render('gateways.paypal.pay', $data); if(isset($data['payment_source'])) $order['payment_source'] = $data['payment_source']; - - nlog($order); $r = $this->gatewayRequest('/v2/checkout/orders', 'post', $order); - // nlog($r->json()); - return $r->json()['id']; }