From e37c11499cff4eb22e9a08e8cf22045070756bf9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Nov 2023 14:06:51 +1100 Subject: [PATCH] Updates for PayPal PPCP --- app/Models/Presenters/ClientPresenter.php | 5 + .../PayPalPPCPPaymentDriver.php | 102 ++++++++++++++---- app/Providers/EventServiceProvider.php | 12 +-- 3 files changed, 93 insertions(+), 26 deletions(-) diff --git a/app/Models/Presenters/ClientPresenter.php b/app/Models/Presenters/ClientPresenter.php index ac4b184d94e1..433490107eba 100644 --- a/app/Models/Presenters/ClientPresenter.php +++ b/app/Models/Presenters/ClientPresenter.php @@ -110,6 +110,11 @@ class ClientPresenter extends EntityPresenter return $str; } + public function shipping_country_code(): string + { + return $this->entity->shipping_country ? $this->entity->shipping_country->iso_3166_2 : $this->entity->country->iso_3166_2; + } + public function phone() { return $this->entity->phone ?: ''; diff --git a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php index 3d6eafa5e7e2..49f2191ee2ae 100644 --- a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php +++ b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php @@ -87,8 +87,8 @@ class PayPalPPCPPaymentDriver extends BaseDriver $method = PaymentType::PAYPAL; match($gateway_type_id){ - "3" => $method = PaymentType::PAYPAL, "1" => $method = PaymentType::CREDIT_CARD_OTHER, + "3" => $method = PaymentType::PAYPAL, "25" => $method = PaymentType::VENMO, }; @@ -99,8 +99,8 @@ class PayPalPPCPPaymentDriver extends BaseDriver { $enums = [ - 3 => 'paypal', 1 => 'card', + 3 => 'paypal', 25 => 'venmo', // 9 => 'sepa', // 12 => 'bancontact', @@ -301,6 +301,63 @@ class PayPalPPCPPaymentDriver extends BaseDriver } } + private function paymentSource(): array + { + return match($this->paypal_payment_method) { + 'paypal' => $this->injectPayPalPaymentSource(), + 'card' => $this->injectCardPaymentSource(), + 'venmo' => $this->injectVenmoPaymentSource(), + }; + } + + private function injectVenmoPaymentSource(): array + { + + return [ + "venmo" => [ + "email_address" => $this->client->present()->email(), + ], + ]; + + } + + private function injectCardPaymentSource(): array + { + + return [ + "card" => [ + + "name" => $this->client->present()->name(), + "email_address" => $this->client->present()->email(), + "billing_address" => $this->getBillingAddress(), + "experience_context"=> [ + "user_action" => "PAY_NOW" + ], + ], + ]; + + } + + private function injectPayPalPaymentSource(): array + { + + return [ + "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" + ], + ], + ]; + + } + private function createOrder(array $data): string { @@ -315,14 +372,15 @@ class PayPalPPCPPaymentDriver extends BaseDriver $order = [ "intent" => "CAPTURE", - "payer" => [ - "name" => [ - "given_name" => $this->client->present()->first_name(), - "surname" => $this->client->present()->last_name(), - ], - "email_address" => $this->client->present()->email(), - "address" => $this->getBillingAddress(), - ], + "payment_source" => $this->paymentSource(), + // "payer" => [ + // "name" => [ + // "given_name" => $this->client->present()->first_name(), + // "surname" => $this->client->present()->last_name(), + // ], + // "email_address" => $this->client->present()->email(), + // "address" => $this->getBillingAddress(), + // ], "purchase_units" => [ [ "description" =>ctrans('texts.invoice_number').'# '.$invoice->number, @@ -333,9 +391,7 @@ class PayPalPPCPPaymentDriver extends BaseDriver "payment_instruction" => [ "disbursement_mode" => "INSTANT", ], - "shipping" => [ - "address" => $this->getShippingAddress() - ], + $this->getShippingAddress(), "amount" => [ "value" => (string)$data['amount_with_fee'], "currency_code"=> $this->client->currency()->code, @@ -385,15 +441,21 @@ class PayPalPPCPPaymentDriver extends BaseDriver private function getShippingAddress(): array { return $this->company_gateway->require_shipping_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->shipping_country->iso_3166_2, + [ + "shipping" => [ + "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(), + ], ] + ] : []; + } public function gatewayRequest(string $uri, string $verb, array $data, ?array $headers = []) diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 14972f02736a..ce6133cbb0ad 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -290,12 +290,12 @@ class EventServiceProvider extends ServiceProvider */ protected $listen = [ - // RequestSending::class => [ - // LogRequestSending::class, - // ], - // ResponseReceived::class => [ - // LogResponseReceived::class, - // ], + RequestSending::class => [ + LogRequestSending::class, + ], + ResponseReceived::class => [ + LogResponseReceived::class, + ], AccountCreated::class => [ ], MessageSending::class => [