2017-06-07 16:29:57 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-07-30 15:42:49 +02:00
|
|
|
* RetailCRM Integration.
|
2017-06-07 16:29:57 +03:00
|
|
|
*
|
|
|
|
* @package WC_Retailcrm_Orders
|
|
|
|
* @category Integration
|
2017-07-30 15:42:49 +02:00
|
|
|
* @author RetailCRM
|
2017-06-07 16:29:57 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Retailcrm_Orders
|
|
|
|
*/
|
|
|
|
class WC_Retailcrm_Orders
|
|
|
|
{
|
2018-01-10 14:26:05 +03:00
|
|
|
protected $retailcrm_settings;
|
|
|
|
protected $retailcrm;
|
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
private $order = array();
|
|
|
|
private $payment = array();
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
public function __construct($retailcrm = false)
|
|
|
|
{
|
|
|
|
$this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key);
|
|
|
|
$this->retailcrm = $retailcrm;
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload orders to CRM
|
2018-04-27 11:19:02 +03:00
|
|
|
*
|
2018-05-28 12:56:19 +03:00
|
|
|
* @param bool $withCustomers
|
|
|
|
* @param array $include
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return array $uploadOrders | null
|
2017-06-07 16:29:57 +03:00
|
|
|
*/
|
2018-05-28 12:56:19 +03:00
|
|
|
public function ordersUpload($include = array(), $withCustomers = false)
|
2017-06-07 16:29:57 +03:00
|
|
|
{
|
2018-04-27 11:19:02 +03:00
|
|
|
if (!$this->retailcrm) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:29:57 +03:00
|
|
|
$orders = get_posts(array(
|
|
|
|
'numberposts' => -1,
|
|
|
|
'post_type' => wc_get_order_types('view-orders'),
|
2018-05-28 12:56:19 +03:00
|
|
|
'post_status' => array_keys(wc_get_order_statuses()),
|
|
|
|
'include' => $include
|
2017-06-07 16:29:57 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$orders_data = array();
|
|
|
|
|
|
|
|
foreach ($orders as $data_order) {
|
2018-04-27 11:19:02 +03:00
|
|
|
$order = wc_get_order($data_order->ID);
|
|
|
|
$this->processOrder($order);
|
2017-06-07 16:29:57 +03:00
|
|
|
$customer = $order->get_user();
|
2018-05-28 12:56:19 +03:00
|
|
|
$customers = array();
|
2017-06-07 16:29:57 +03:00
|
|
|
|
|
|
|
if ($customer != false) {
|
2018-04-27 11:19:02 +03:00
|
|
|
$this->order['customer']['externalId'] = $customer->get('ID');
|
2018-05-28 12:56:19 +03:00
|
|
|
|
|
|
|
if ($withCustomers === true) {
|
|
|
|
$customers[] = $customer->get('ID');
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$orders_data[] = $this->order;
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 15:58:13 +03:00
|
|
|
if ($withCustomers === true && !empty($customers)) {
|
2019-01-22 12:03:42 +03:00
|
|
|
if (!class_exists('WC_Retailcrm_Customers')) {
|
|
|
|
include_once(WC_Retailcrm_Base::checkCustomFile('customers'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$retailcrmCustomer = new WC_Retailcrm_Customers($this->retailcrm);
|
2018-05-28 12:56:19 +03:00
|
|
|
$retailcrmCustomer->customersUpload($customers);
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:29:57 +03:00
|
|
|
$uploadOrders = array_chunk($orders_data, 50);
|
|
|
|
|
|
|
|
foreach ($uploadOrders as $uploadOrder) {
|
2018-04-27 11:19:02 +03:00
|
|
|
$this->retailcrm->ordersUpload($uploadOrder);
|
2018-05-28 12:56:19 +03:00
|
|
|
time_nanosleep(0, 250000000);
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2018-04-27 11:19:02 +03:00
|
|
|
|
|
|
|
return $uploadOrders;
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create order
|
|
|
|
*
|
|
|
|
* @param $order_id
|
2018-04-27 11:19:02 +03:00
|
|
|
*
|
2018-05-28 12:56:19 +03:00
|
|
|
* @return mixed
|
2017-06-07 16:29:57 +03:00
|
|
|
*/
|
|
|
|
public function orderCreate($order_id)
|
|
|
|
{
|
2018-04-27 11:19:02 +03:00
|
|
|
if (!$this->retailcrm) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$order = wc_get_order($order_id);
|
|
|
|
$this->processOrder($order);
|
2017-06-07 16:29:57 +03:00
|
|
|
$customer = $order->get_user();
|
|
|
|
|
2019-01-17 15:58:13 +03:00
|
|
|
if (!class_exists('WC_Retailcrm_Customers')) {
|
|
|
|
include_once(WC_Retailcrm_Base::checkCustomFile('customers'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$retailcrm_customers = new WC_Retailcrm_Customers($this->retailcrm);
|
|
|
|
|
2017-06-07 16:29:57 +03:00
|
|
|
if ($customer != false) {
|
2019-01-17 15:58:13 +03:00
|
|
|
$search = $retailcrm_customers->searchCustomer(array('id' => $customer->get('ID')));
|
|
|
|
|
|
|
|
if (!$search) {
|
|
|
|
$retailcrm_customers->createCustomer($customer);
|
|
|
|
} else {
|
|
|
|
$this->order['customer']['externalId'] = $search['externalId'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$search = $retailcrm_customers->searchCustomer(array('email' => $order->get_billing_email()));
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2019-01-17 15:58:13 +03:00
|
|
|
if (!$search) {
|
|
|
|
$new_customer = $retailcrm_customers->buildCustomerFromOrderData($order);
|
|
|
|
$id = $retailcrm_customers->createCustomer($new_customer);
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2019-01-17 15:58:13 +03:00
|
|
|
if ($id !== null) {
|
|
|
|
$this->order['customer']['id'] = $id;
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
} else {
|
2019-01-17 15:58:13 +03:00
|
|
|
$this->order['customer']['externalId'] = $search['externalId'];
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2019-01-17 15:58:13 +03:00
|
|
|
|
|
|
|
unset($new_customer);
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$this->retailcrm->ordersCreate($this->order);
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
return $order;
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2017-06-07 16:29:57 +03:00
|
|
|
/**
|
2018-04-27 11:19:02 +03:00
|
|
|
* Edit order in CRM
|
2017-06-07 16:29:57 +03:00
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @param int $order_id
|
2017-06-07 16:29:57 +03:00
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return WC_Order $order | null
|
2017-06-07 16:29:57 +03:00
|
|
|
*/
|
2018-04-27 11:19:02 +03:00
|
|
|
public function updateOrder($order_id)
|
|
|
|
{
|
|
|
|
if (!$this->retailcrm) {
|
|
|
|
return null;
|
2018-02-19 16:59:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$order = wc_get_order($order_id);
|
|
|
|
$this->processOrder($order, true);
|
2017-07-30 15:42:49 +02:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if ($this->retailcrm_settings['api_version'] == 'v4') {
|
|
|
|
$this->order['paymentType'] = $this->retailcrm_settings[$order->get_payment_method()];
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$response = $this->retailcrm->ordersEdit($this->order);
|
2017-07-30 15:42:49 +02:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if ($response->isSuccessful() && $this->retailcrm_settings['api_version'] == 'v5') {
|
|
|
|
$this->payment = $this->orderUpdatePaymentType($order);
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
return $order;
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-27 11:19:02 +03:00
|
|
|
* Update order payment type
|
|
|
|
*
|
|
|
|
* @param WC_Order $order
|
2017-06-07 16:29:57 +03:00
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return null | array $payment
|
2017-06-07 16:29:57 +03:00
|
|
|
*/
|
2018-04-27 11:19:02 +03:00
|
|
|
protected function orderUpdatePaymentType($order)
|
|
|
|
{
|
|
|
|
if (!isset($this->retailcrm_settings[$order->get_payment_method()])) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$response = $this->retailcrm->ordersGet($order->get_id());
|
2017-07-30 15:42:49 +02:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
$retailcrmOrder = $response['order'];
|
|
|
|
|
|
|
|
foreach ($retailcrmOrder['payments'] as $payment_data) {
|
2019-02-25 17:08:04 +03:00
|
|
|
$payment_external_id = explode('-', $payment_data['externalId']);
|
|
|
|
|
|
|
|
if ($payment_external_id[0] == $order->get_id()) {
|
2018-04-27 11:19:02 +03:00
|
|
|
$payment = $payment_data;
|
|
|
|
}
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2018-08-30 14:52:48 +03:00
|
|
|
if (isset($payment) && $payment['type'] == $this->retailcrm_settings[$order->get_payment_method()] && $order->is_paid()) {
|
2019-03-13 11:46:58 +03:00
|
|
|
$payment = $this->sendPayment($order, true, $payment['externalId']);
|
2018-08-30 14:52:48 +03:00
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if (isset($payment) && $payment['type'] != $this->retailcrm_settings[$order->get_payment_method()]) {
|
|
|
|
$response = $this->retailcrm->ordersPaymentDelete($payment['id']);
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if ($response->isSuccessful()) {
|
2018-08-30 14:52:48 +03:00
|
|
|
$payment = $this->sendPayment($order);
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
return $payment;
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
return null;
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2017-09-22 15:29:50 +03:00
|
|
|
/**
|
2018-04-27 11:19:02 +03:00
|
|
|
* Get order data
|
2017-09-22 15:29:50 +03:00
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @param WC_Order $order
|
2017-09-22 15:29:50 +03:00
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return array $order_data_arr
|
2017-09-22 15:29:50 +03:00
|
|
|
*/
|
2018-08-30 14:52:48 +03:00
|
|
|
protected function getOrderData($order)
|
|
|
|
{
|
2017-11-24 16:04:28 +03:00
|
|
|
$order_data_arr = array();
|
2018-04-27 11:19:02 +03:00
|
|
|
$order_info = $order->get_data();
|
2017-09-22 15:29:50 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$order_data_arr['id'] = $order_info['id'];
|
|
|
|
$order_data_arr['payment_method'] = $order->get_payment_method();
|
|
|
|
$order_data_arr['date'] = $order_info['date_created']->date('Y-m-d H:i:s');
|
|
|
|
$order_data_arr['discount_total'] = $order_info['discount_total'];
|
|
|
|
$order_data_arr['discount_tax'] = $order_info['discount_tax'];
|
|
|
|
$order_data_arr['customer_comment'] = $order->get_customer_note();
|
2017-09-22 15:29:50 +03:00
|
|
|
|
|
|
|
return $order_data_arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process to combine order data
|
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @param WC_Order $order
|
2018-01-23 14:09:44 +03:00
|
|
|
* @param boolean $update
|
2018-04-27 11:19:02 +03:00
|
|
|
*
|
|
|
|
* @return void
|
2017-09-22 15:29:50 +03:00
|
|
|
*/
|
2018-04-27 11:19:02 +03:00
|
|
|
protected function processOrder($order, $update = false)
|
2017-06-07 16:29:57 +03:00
|
|
|
{
|
2018-04-27 11:19:02 +03:00
|
|
|
if (!$order instanceof WC_Order) {
|
2017-06-07 16:29:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
$order_data_info = $this->getOrderData($order);
|
2017-06-07 16:29:57 +03:00
|
|
|
$order_data = array();
|
|
|
|
|
2017-09-22 15:29:50 +03:00
|
|
|
$order_data['externalId'] = $order_data_info['id'];
|
2017-06-07 16:29:57 +03:00
|
|
|
$order_data['number'] = $order->get_order_number();
|
2017-12-14 11:32:53 +02:00
|
|
|
$order_data['createdAt'] = trim($order_data_info['date']);
|
2017-09-22 15:29:50 +03:00
|
|
|
$order_data['customerComment'] = $order_data_info['customer_comment'];
|
2017-06-07 16:29:57 +03:00
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if (!empty($order_data_info['payment_method'])
|
|
|
|
&& !empty($this->retailcrm_settings[$order_data_info['payment_method']])
|
|
|
|
&& $this->retailcrm_settings['api_version'] != 'v5'
|
|
|
|
) {
|
2017-09-22 15:29:50 +03:00
|
|
|
$order_data['paymentType'] = $this->retailcrm_settings[$order_data_info['payment_method']];
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 11:19:02 +03:00
|
|
|
if ($order->get_items('shipping')) {
|
2018-02-19 16:59:49 +03:00
|
|
|
$shippings = $order->get_items( 'shipping' );
|
2018-04-27 11:19:02 +03:00
|
|
|
$shipping = reset($shippings);
|
2017-06-07 16:29:57 +03:00
|
|
|
$shipping_code = explode(':', $shipping['method_id']);
|
2017-12-14 11:32:53 +02:00
|
|
|
|
2018-01-23 14:09:44 +03:00
|
|
|
if (isset($this->retailcrm_settings[$shipping['method_id']])) {
|
|
|
|
$shipping_method = $shipping['method_id'];
|
2018-07-19 12:16:30 +03:00
|
|
|
} elseif (isset($this->retailcrm_settings[$shipping_code[0]])) {
|
2017-12-14 11:32:53 +02:00
|
|
|
$shipping_method = $shipping_code[0];
|
2018-07-19 12:16:30 +03:00
|
|
|
} else {
|
|
|
|
$shipping_method = $shipping['method_id'] . ':' . $shipping['instance_id'];
|
2017-12-14 11:32:53 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 12:16:30 +03:00
|
|
|
$shipping_cost = $shipping['total'] + $shipping['total_tax'];
|
2017-06-07 16:29:57 +03:00
|
|
|
|
|
|
|
if (!empty($shipping_method) && !empty($this->retailcrm_settings[$shipping_method])) {
|
|
|
|
$order_data['delivery']['code'] = $this->retailcrm_settings[$shipping_method];
|
2018-07-19 12:16:30 +03:00
|
|
|
$service = retailcrm_get_delivery_service($shipping['method_id'], $shipping['instance_id']);
|
|
|
|
|
|
|
|
if ($service) {
|
|
|
|
$order_data['delivery']['service'] = array(
|
|
|
|
'name' => $service['title'],
|
|
|
|
'code' => $service['instance_id'],
|
|
|
|
'active' => true
|
|
|
|
);
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($shipping_cost)) {
|
|
|
|
$order_data['delivery']['cost'] = $shipping_cost;
|
|
|
|
}
|
2018-08-06 16:23:23 +03:00
|
|
|
|
|
|
|
if ($shipping['total']) {
|
|
|
|
$order_data['delivery']['netCost'] = $shipping['total'];
|
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 15:42:49 +02:00
|
|
|
if ($this->retailcrm_settings['api_version'] != 'v5' && $order->is_paid()) {
|
2017-06-07 16:29:57 +03:00
|
|
|
$order_data['paymentStatus'] = 'paid';
|
|
|
|
}
|
|
|
|
|
|
|
|
$status = $order->get_status();
|
|
|
|
$order_data['status'] = $this->retailcrm_settings[$status];
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2017-08-15 13:28:29 +02:00
|
|
|
$user_data_billing = $order->get_address('billing');
|
|
|
|
|
|
|
|
if (!empty($user_data_billing)) {
|
2019-01-17 15:58:13 +03:00
|
|
|
$order_data['phone'] = $user_data_billing['phone'];
|
|
|
|
$order_data['email'] = $user_data_billing['email'];
|
2017-08-15 13:28:29 +02:00
|
|
|
}
|
|
|
|
|
2019-01-17 15:58:13 +03:00
|
|
|
$user_data_shipping = $order->get_address('shipping');
|
|
|
|
|
|
|
|
if (!empty($user_data_shipping)) {
|
|
|
|
$order_data['firstName'] = $user_data_shipping['first_name'];
|
|
|
|
$order_data['lastName'] = $user_data_shipping['last_name'];
|
|
|
|
$order_data['delivery']['address']['index'] = $user_data_shipping['postcode'];
|
|
|
|
$order_data['delivery']['address']['city'] = $user_data_shipping['city'];
|
|
|
|
$order_data['delivery']['address']['region'] = $user_data_shipping['state'];
|
|
|
|
$order_data['countryIso'] = $user_data_shipping['country'];
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
2017-08-24 08:40:05 +03:00
|
|
|
$order_data['delivery']['address']['text'] = sprintf(
|
2017-09-13 17:30:54 +03:00
|
|
|
"%s %s %s %s %s",
|
2019-01-17 15:58:13 +03:00
|
|
|
$user_data_shipping['postcode'],
|
|
|
|
$user_data_shipping['state'],
|
|
|
|
$user_data_shipping['city'],
|
|
|
|
$user_data_shipping['address_1'],
|
|
|
|
$user_data_shipping['address_2']
|
2017-08-24 08:40:05 +03:00
|
|
|
);
|
|
|
|
|
2017-06-07 16:29:57 +03:00
|
|
|
$order_items = array();
|
|
|
|
|
|
|
|
foreach ($order->get_items() as $item) {
|
|
|
|
$uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ;
|
2018-02-02 12:13:45 +03:00
|
|
|
$price = round(($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity()), 2);
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2018-02-01 15:07:16 +03:00
|
|
|
$product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
|
|
|
|
$product_tax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;
|
|
|
|
$price_item = $product_price + $product_tax;
|
|
|
|
$discount_price = $price - $price_item;
|
2017-11-24 16:04:28 +03:00
|
|
|
|
2018-02-01 15:07:16 +03:00
|
|
|
$order_item = array(
|
|
|
|
'offer' => array('externalId' => $uid),
|
|
|
|
'productName' => $item['name'],
|
|
|
|
'initialPrice' => (float)$price,
|
|
|
|
'quantity' => $item['qty'],
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->retailcrm_settings['api_version'] == 'v5' && round($discount_price, 2)) {
|
|
|
|
$order_item['discountManualAmount'] = round($discount_price, 2);
|
|
|
|
} elseif ($this->retailcrm_settings['api_version'] == 'v4' && round($discount_price, 2)) {
|
|
|
|
$order_item['discount'] = round($discount_price, 2);
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$order_items[] = $order_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
$order_data['items'] = $order_items;
|
|
|
|
|
2017-07-30 15:42:49 +02:00
|
|
|
if ($this->retailcrm_settings['api_version'] == 'v5') {
|
|
|
|
$payment = array(
|
|
|
|
'amount' => $order->get_total(),
|
2019-02-25 17:08:04 +03:00
|
|
|
'externalId' => $order->get_id() . uniqid('-')
|
2017-07-30 15:42:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$payment['order'] = array(
|
2018-04-27 11:19:02 +03:00
|
|
|
'externalId' => $order->get_id()
|
2017-07-30 15:42:49 +02:00
|
|
|
);
|
|
|
|
|
2017-09-22 15:29:50 +03:00
|
|
|
if (!empty($order_data_info['payment_method']) && !empty($this->retailcrm_settings[$order_data_info['payment_method']])) {
|
|
|
|
$payment['type'] = $this->retailcrm_settings[$order_data_info['payment_method']];
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($order->is_paid()) {
|
|
|
|
$payment['status'] = 'paid';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order->get_date_paid()) {
|
|
|
|
$pay_date = $order->get_date_paid();
|
2017-12-14 11:32:53 +02:00
|
|
|
$payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s'));
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 14:09:44 +03:00
|
|
|
if (!$update) {
|
|
|
|
$order_data['payments'][] = $payment;
|
|
|
|
}
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 10:03:46 +03:00
|
|
|
$this->order = apply_filters('retailcrm_process_order', $order_data, $order);
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2017-07-30 15:42:49 +02:00
|
|
|
|
2018-01-23 14:09:44 +03:00
|
|
|
/**
|
2018-08-30 14:52:48 +03:00
|
|
|
* Send payment in CRM
|
2019-02-25 17:08:04 +03:00
|
|
|
*
|
2018-01-23 14:09:44 +03:00
|
|
|
* @param WC_Order $order
|
2018-08-30 14:52:48 +03:00
|
|
|
* @param boolean $update
|
2019-02-25 17:08:04 +03:00
|
|
|
*
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return array $payment
|
2018-01-23 14:09:44 +03:00
|
|
|
*/
|
2019-03-13 11:46:58 +03:00
|
|
|
protected function sendPayment($order, $update = false, $externalId = false)
|
2017-07-30 15:42:49 +02:00
|
|
|
{
|
|
|
|
$payment = array(
|
2019-03-13 11:46:58 +03:00
|
|
|
'amount' => $order->get_total()
|
2017-07-30 15:42:49 +02:00
|
|
|
);
|
|
|
|
|
2019-03-14 15:54:25 +03:00
|
|
|
if ($update) {
|
2019-03-13 11:46:58 +03:00
|
|
|
$payment['externalId'] = $externalId;
|
2019-03-14 15:58:36 +03:00
|
|
|
} else {
|
2019-03-13 11:46:58 +03:00
|
|
|
$payment['externalId'] = $order->get_id() . uniqid('-');
|
|
|
|
}
|
|
|
|
|
2017-07-30 15:42:49 +02:00
|
|
|
$payment['order'] = array(
|
2018-04-27 11:19:02 +03:00
|
|
|
'externalId' => $order->get_id()
|
2017-07-30 15:42:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($order->is_paid()) {
|
|
|
|
$payment['status'] = 'paid';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order->get_date_paid()) {
|
|
|
|
$pay_date = $order->get_date_paid();
|
2017-12-14 11:32:53 +02:00
|
|
|
$payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s'));
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
2017-08-24 08:40:05 +03:00
|
|
|
|
2019-01-17 15:58:13 +03:00
|
|
|
if ($update === false) {
|
2018-08-30 14:52:48 +03:00
|
|
|
if (isset($this->retailcrm_settings[$order->get_payment_method()])) {
|
|
|
|
$payment['type'] = $this->retailcrm_settings[$order->get_payment_method()];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->retailcrm->ordersPaymentCreate($payment);
|
|
|
|
} else {
|
|
|
|
$this->retailcrm->ordersPaymentEdit($payment);
|
|
|
|
}
|
2018-04-27 11:19:02 +03:00
|
|
|
|
|
|
|
return $payment;
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-23 14:09:44 +03:00
|
|
|
/**
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return array
|
2018-01-23 14:09:44 +03:00
|
|
|
*/
|
2018-04-27 11:19:02 +03:00
|
|
|
public function getOrder()
|
2018-01-23 14:09:44 +03:00
|
|
|
{
|
2018-04-27 11:19:02 +03:00
|
|
|
return $this->order;
|
2018-01-23 14:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-27 11:19:02 +03:00
|
|
|
* @return array
|
2018-01-23 14:09:44 +03:00
|
|
|
*/
|
2018-04-27 11:19:02 +03:00
|
|
|
public function getPayment()
|
2017-07-30 15:42:49 +02:00
|
|
|
{
|
2018-04-27 11:19:02 +03:00
|
|
|
return $this->payment;
|
2017-07-30 15:42:49 +02:00
|
|
|
}
|
2017-06-07 16:29:57 +03:00
|
|
|
}
|
2018-02-01 15:07:16 +03:00
|
|
|
endif;
|