From 7b132f9d92e54df72860045210383c9672250a7e Mon Sep 17 00:00:00 2001 From: dinamiko Date: Fri, 4 Feb 2022 17:11:40 +0100 Subject: [PATCH] Get payer info from checkout form (WIP) --- .../src/Factory/PayerFactory.php | 20 +++++++++++++++++++ .../src/Endpoint/CreateOrderEndpoint.php | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/modules/ppcp-api-client/src/Factory/PayerFactory.php b/modules/ppcp-api-client/src/Factory/PayerFactory.php index fedeaa5a1..c0e10de61 100644 --- a/modules/ppcp-api-client/src/Factory/PayerFactory.php +++ b/modules/ppcp-api-client/src/Factory/PayerFactory.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Factory; +use WooCommerce\PayPalCommerce\ApiClient\Entity\Address; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerName; use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerTaxInfo; @@ -147,4 +148,23 @@ class PayerFactory { $tax_info ); } + + public function from_checkout_form(array $data) { + + $first_name = $data['billing_first_name'] ?? ''; + $last_name = $data['billing_last_name'] ?? ''; + $billing_email = $data['billing_email'] ?? ''; + $billing_country = $data['billing_country'] ?? ''; + $billing_address_1 = $data['billing_address_1'] ?? ''; + + return new Payer( + new PayerName($first_name, $last_name), + $billing_email, + '', + new Address( + $billing_country, + $billing_address_1 + ) + ); + } } diff --git a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index 4a7f63348..a7d62fae3 100644 --- a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -12,8 +12,10 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint; use Exception; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; +use WooCommerce\PayPalCommerce\ApiClient\Entity\Address; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; +use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerName; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; @@ -337,6 +339,12 @@ class CreateOrderEndpoint implements EndpointInterface { $payer = $this->payer_factory->from_paypal_response( json_decode( wp_json_encode( $data['payer'] ) ) ); } + + if(!$payer && isset($data['form'])) { + parse_str($data['form'], $output); + return $this->payer_factory->from_checkout_form($output); + } + return $payer; }