From 751260c00a8fe3d6e0581ff7ec4ca0270fe281de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20R=C3=B3bert?= Date: Wed, 15 Apr 2020 11:39:42 +0300 Subject: [PATCH] Make Order createTime optional When the intent is set to authorize and the status is completed in many cases the created time is not returned back --- modules.local/ppcp-api-client/src/Entity/Order.php | 10 ++++++---- .../ppcp-api-client/src/Factory/OrderFactory.php | 12 +++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/modules.local/ppcp-api-client/src/Entity/Order.php b/modules.local/ppcp-api-client/src/Entity/Order.php index 60851a36f..27c9e6cee 100644 --- a/modules.local/ppcp-api-client/src/Entity/Order.php +++ b/modules.local/ppcp-api-client/src/Entity/Order.php @@ -21,16 +21,15 @@ class Order */ public function __construct( string $id, - \DateTime $createTime, array $purchaseUnits, OrderStatus $orderStatus, Payer $payer = null, string $intent = 'CAPTURE', + \DateTime $createTime = null, \DateTime $updateTime = null ) { $this->id = $id; - $this->createTime = $createTime; //phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType $this->purchaseUnits = array_values(array_filter( $purchaseUnits, @@ -43,6 +42,7 @@ class Order $this->orderStatus = $orderStatus; $this->intent = ($intent === 'CAPTURE') ? 'CAPTURE' : 'AUTHORIZE'; $this->purchaseUnits = $purchaseUnits; + $this->createTime = $createTime; $this->updateTime = $updateTime; } @@ -51,7 +51,7 @@ class Order return $this->id; } - public function createTime() : \DateTime + public function createTime() : ?\DateTime { return $this->createTime; } @@ -96,8 +96,10 @@ class Order }, $this->purchaseUnits() ), - 'create_time' => $this->createTime()->format(\DateTimeInterface::ISO8601), ]; + if ($this->createTime()) { + $order['create_time'] = $this->createTime()->format(\DateTimeInterface::ISO8601); + } if ($this->payer()) { $order['payer'] =$this->payer()->toArray(); } diff --git a/modules.local/ppcp-api-client/src/Factory/OrderFactory.php b/modules.local/ppcp-api-client/src/Factory/OrderFactory.php index 892231d54..8280e4fe8 100644 --- a/modules.local/ppcp-api-client/src/Factory/OrderFactory.php +++ b/modules.local/ppcp-api-client/src/Factory/OrderFactory.php @@ -28,11 +28,11 @@ class OrderFactory return new Order( $order->id(), - $order->createTime(), $purchaseUnits, $order->status(), $order->payer(), $order->intent(), + $order->createTime(), $order->updateTime() ); } @@ -44,11 +44,6 @@ class OrderFactory __('Order does not contain an id.', 'woocommerce-paypal-commerce-gateway') ); } - if (! isset($orderData->create_time)) { - throw new RuntimeException( - __('Order does not contain a create time.', 'woocommerce-paypal-commerce-gateway') - ); - } if (! isset($orderData->purchase_units) || !is_array($orderData->purchase_units)) { throw new RuntimeException( __('Order does not contain items.', 'woocommerce-paypal-commerce-gateway') @@ -72,6 +67,9 @@ class OrderFactory $orderData->purchase_units ); + $createTime = (isset($orderData->create_time)) ? + \DateTime::createFromFormat(\DateTime::ISO8601, $orderData->create_time) + : null; $updateTime = (isset($orderData->update_time)) ? \DateTime::createFromFormat(\DateTime::ISO8601, $orderData->update_time) : null; @@ -81,11 +79,11 @@ class OrderFactory return new Order( $orderData->id, - \DateTime::createFromFormat(\DateTime::ISO8601, $orderData->create_time), $purchaseUnits, new OrderStatus($orderData->status), $payer, $orderData->intent, + $createTime, $updateTime ); }