From 91a7c02e250315e237c49ae126a20d3bdb86f352 Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Thu, 11 Aug 2022 21:17:55 +0300 Subject: [PATCH] Add support for payment method on delivery --- CHANGELOG.md | 3 + VERSION | 2 +- src/include/class-wc-retailcrm-orders.php | 2 +- src/include/functions.php | 2 +- .../class-wc-retailcrm-order-payment.php | 40 ++- src/readme.txt | 5 +- src/retailcrm.php | 2 +- src/uninstall.php | 2 +- tests/test-wc-retailcrm-orders.php | 229 ++++++++++++------ 9 files changed, 198 insertions(+), 89 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7f2efb..f80518c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2022-08-10 4.4.7 +* Add support for payment method on delivery + ## 2022-08-06 4.4.6 * Add automatically upload ICML in CRM * Add filter for changing ICML product information diff --git a/VERSION b/VERSION index b58299c..3401474 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.4.6 \ No newline at end of file +4.4.7 \ No newline at end of file diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index a573310..66f516b 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -475,7 +475,7 @@ if (!class_exists('WC_Retailcrm_Orders')) : return (array) $this->ordersGetRequestCache[$orderId]; } - $crmOrder = array(); + $crmOrder = []; $response = $this->retailcrm->ordersGet($orderId); if (!empty($response) && $response->isSuccessful() && isset($response['order'])) { diff --git a/src/include/functions.php b/src/include/functions.php index 99bfbc8..e8f4cf3 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -170,7 +170,7 @@ function getShippingRates() /** * Write base logs in retailcrm file. * - * @codeCoverageIgnore Check in another tests + * @codeCoverageIgnore */ function writeBaseLogs($message) { diff --git a/src/include/order/class-wc-retailcrm-order-payment.php b/src/include/order/class-wc-retailcrm-order-payment.php index 1266e96..cc46dcf 100644 --- a/src/include/order/class-wc-retailcrm-order-payment.php +++ b/src/include/order/class-wc-retailcrm-order-payment.php @@ -1,4 +1,5 @@ is_paid()) { - $paymentData['status'] = 'paid'; + if ($order->get_status() != 'completed' && $order->get_payment_method() == 'cod') { + writeBaseLogs( + implode( + ' ', + [ + 'Payment for order: ' . $order->get_id(), + 'Payment status cannot be changed as it is cash (or other payment method) on delivery.', + 'The status will be changed when the order is in status completed.', + ] + ) + ); + } else { + $paymentData['status'] = 'paid'; - if (isset($this->settings[$order->get_payment_method()])) { - $paymentData['type'] = $this->settings[$order->get_payment_method()]; + if (isset($this->settings[$order->get_payment_method()])) { + $paymentData['type'] = $this->settings[$order->get_payment_method()]; + } + + $paidAt = $order->get_date_paid(); + + if (!empty($paidAt)) { + $paymentData['paidAt'] = $paidAt->date('Y-m-d H:i:s'); + } } } - $paidAt = $order->get_date_paid(); - - if (!empty($paidAt)) { - $paymentData['paidAt'] = $paidAt->date('Y-m-d H:i:s'); - } - if ($this->isNew) { if (isset($this->settings[$order->get_payment_method()])) { $paymentData['type'] = $this->settings[$order->get_payment_method()]; @@ -80,6 +94,12 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data } } + $paymentData = apply_filters( + 'retailcrm_process_payment', + WC_Retailcrm_Plugin::clearArray($paymentData), + $order + ); + $this->set_data_fields($paymentData); return $this; @@ -112,4 +132,4 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data 'order' => [] ]; } -} \ No newline at end of file +} diff --git a/src/readme.txt b/src/readme.txt index 40ac724..36a948d 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -5,7 +5,7 @@ Tags: Интеграция, Simla.com, simla Requires PHP: 5.6 Requires at least: 5.3 Tested up to: 5.9 -Stable tag: 4.4.5 +Stable tag: 4.4.7 License: GPLv1 or later License URI: http://www.gnu.org/licenses/gpl-1.0.html @@ -82,6 +82,9 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i == Changelog == += 4.4.7 = +* Add support for payment method on delivery + = 4.4.6 = * Add automatically upload ICML in CRM * Add filter for changing ICML product information diff --git a/src/retailcrm.php b/src/retailcrm.php index e365fff..01feee3 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -5,7 +5,7 @@ * Description: Integration plugin for WooCommerce & Simla.com * Author: RetailDriver LLC * Author URI: http://retailcrm.pro/ - * Version: 4.4.6 + * Version: 4.4.7 * Tested up to: 5.9 * WC requires at least: 5.4 * WC tested up to: 6.7 diff --git a/src/uninstall.php b/src/uninstall.php index e7d95b4..22594ad 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -16,7 +16,7 @@ * * @link https://wordpress.org/plugins/woo-retailcrm/ * - * @version 4.4.6 + * @version 4.4.7 * * @package RetailCRM */ diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index 3e98e7f..d5d9ab5 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -21,7 +21,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy') ->disableOriginalConstructor() ->setMethods( - array( + [ 'ordersGet', 'ordersCreate', 'ordersEdit', @@ -37,7 +37,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper 'getSingleSiteForKey', 'customersCorporateAddressesCreate', 'customersCorporateCompaniesCreate' - ) + ] ) ->getMock(); @@ -130,10 +130,10 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->setMockResponse($responseMockSearch, 'isSuccessful', true); $responseMockSearch->setResponse( - array( + [ 'success' => true, - 'customersCorporate' => array() - ) + 'customersCorporate' => [] + ] ); // Mock response for create customer corporate, his addresses and companies @@ -141,10 +141,10 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->setMockResponse($responseMockCustomerCorporate, 'isSuccessful', true); $responseMockCustomerCorporate->setResponse( - array( + [ 'success' => true, 'id' => 1 - ) + ] ); // Mock response for get companies @@ -152,12 +152,12 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->setMockResponse($responseMockCompany, 'isSuccessful', true); $responseMockCompany->setResponse( - array( + [ 'success' => true, - 'companies' => array( - array('name' => 'WooCompany', 'id' => 777) - ) - ) + 'companies' => [ + ['name' => 'WooCompany', 'id' => 777] + ] + ] ); $this->setMockResponse($retailcrm, 'ordersCreate', $responseMock); @@ -325,6 +325,89 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper } } + /** + * @param $isSuccessful + * @param $retailcrm + * + * @throws Exception + * @dataProvider dataProviderUpdateOrder + */ + public function test_payment_on_delivery($isSuccessful, $retailcrm) + { + $this->createTestOrder(); + $this->order->set_payment_method('cod'); + $this->order->save(); + + if ($retailcrm) { + $responseMock = $this->createResponseMock(); + + $this->setMockResponse($responseMock, 'isSuccessful', $isSuccessful); + $this->setMockResponse($retailcrm, 'ordersEdit', $responseMock); + $this->setMockResponse($retailcrm, 'ordersPaymentDelete', $responseMock); + + $response = $this->getResponseData($this->order->get_id()); + $responseMock->setResponse($response); + + $this->setMockResponse($retailcrm, 'ordersGet', $responseMock); + } + + $retailcrmOrders = $this->getRetailcrmOrders($retailcrm); + $order = $retailcrmOrders->updateOrder($this->order->get_id()); + $orderData = $retailcrmOrders->getOrder(); + + if ($retailcrm) { + $this->assertInstanceOf('WC_Order', $order); + $this->assertInternalType('array', $orderData); + + $payment = $retailcrmOrders->getPayment(); + + $this->assertInternalType('array', $payment); + + if (!empty($payment)) { + $this->assertArrayHasKey('type', $payment); + $this->assertArrayHasKey('order', $payment); + $this->assertArrayHasKey('externalId', $payment); + $this->assertEquals('payment3', $payment['type']); + + $this->assertArrayNotHasKey('amount', $payment); + $this->assertArrayNotHasKey('status', $payment); + $this->assertArrayNotHasKey('paidAt', $payment); + } else { + $this->assertEquals([], $payment); + } + + // Check payment status + $this->order->set_status('completed'); + $this->order->save(); + + $order = $retailcrmOrders->updateOrder($this->order->get_id()); + $orderData = $retailcrmOrders->getOrder(); + + $this->assertInstanceOf('WC_Order', $order); + $this->assertInternalType('array', $orderData); + $this->assertArrayHasKey('status', $orderData); + $this->assertEquals('status4', $orderData['status']); + + $payment = $retailcrmOrders->getPayment(); + + $this->assertInternalType('array', $payment); + + if (!empty($payment)) { + $this->assertArrayHasKey('type', $payment); + $this->assertArrayHasKey('order', $payment); + $this->assertArrayHasKey('externalId', $payment); + $this->assertArrayHasKey('status', $payment); + $this->assertArrayHasKey('paidAt', $payment); + $this->assertEquals('payment3', $payment['type']); + $this->assertEquals('paid', $payment['status']); + } else { + $this->assertEquals([], $payment); + } + } else { + $this->assertEquals(null, $order); + } + } + public function test_is_corporate_order() { $this->createTestOrder(); @@ -358,11 +441,11 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals( true, WC_Retailcrm_Orders::isCorporateCrmOrder( - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer_corporate' - ) - ) + ] + ] ) ); @@ -370,11 +453,11 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals( false, WC_Retailcrm_Orders::isCorporateCrmOrder( - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer' - ) - ) + ] + ] ) ); } @@ -390,11 +473,11 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper true, WC_Retailcrm_Orders::isOrderCustomerWasChanged( $this->order, - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer' - ) - ) + ] + ] ) ); @@ -403,14 +486,14 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper true, WC_Retailcrm_Orders::isOrderCustomerWasChanged( $this->order, - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer_corporate' - ), - 'company' => array( + ], + 'company' => [ 'name' => 'Test1' - ) - ) + ] + ] ) ); @@ -421,15 +504,15 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper true, WC_Retailcrm_Orders::isOrderCustomerWasChanged( $this->order, - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer_corporate', 'externalId' => 2 - ), - 'company' => array( + ], + 'company' => [ 'name' => 'Test' - ) - ) + ] + ] ) ); @@ -440,16 +523,16 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper true, WC_Retailcrm_Orders::isOrderCustomerWasChanged( $this->order, - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer_corporate', 'externalId' => 1, 'email' => 'test1@mail.es' - ), - 'company' => array( + ], + 'company' => [ 'name' => 'Test' - ) - ) + ] + ] ) ); @@ -458,16 +541,16 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper false, WC_Retailcrm_Orders::isOrderCustomerWasChanged( $this->order, - array( - 'customer' => array( + [ + 'customer' => [ 'type' => 'customer_corporate', 'externalId' => 1, 'email' => 'test@mail.es' - ), - 'company' => array( + ], + 'company' => [ 'name' => 'Test' - ) - ) + ] + ] ) ); } @@ -476,38 +559,38 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper { $this->setUp(); - return array( - array( + return [ + [ 'is_successful' => true, 'retailcrm' => $this->apiMock - ), - array( + ], + [ 'is_successful' => true, 'retailcrm' => false - ), - array( + ], + [ 'is_successful' => false, 'retailcrm' => false - ), - array( + ], + [ 'is_successful' => false, 'retailcrm' => $this->apiMock - ) - ); + ] + ]; } public function dataProviderRetailcrm() { $this->setUp(); - return array( - array( + return [ + [ 'retailcrm' => $this->apiMock - ), - array( + ], + [ 'retailcrm' => false - ) - ); + ] + ]; } private function createTestOrder() @@ -528,18 +611,18 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper private function getResponseData($externalId) { - return array( + return [ 'success' => true, - 'order' => array( - 'payments' => array( - array( + 'order' => [ + 'payments' => [ + [ 'id' => 1, 'externalId' => $externalId, 'type' => 'payment2' - ) - ) - ) - ); + ] + ] + ] + ]; } /** @@ -572,7 +655,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper { return $this->getMockBuilder('\WC_Retailcrm_Response_Helper') ->disableOriginalConstructor() - ->setMethods(array('isSuccessful')) + ->setMethods(['isSuccessful']) ->getMock(); } }