· 6 years ago · Mar 15, 2020, 05:02 PM
1<?php
2
3if( ! class_exists( 'Woocommerce_Customer_To_PHPPOS' ) ):
4 require_once WC_PHPPOS_DIR."PHPPOSAPIClient/vendor/autoload.php";
5
6 class Woocommerce_Customer_To_PHPPOS extends WC_Integration
7 {
8 public function __construct()
9 {
10 global $woocommerce;
11
12 $this->id = 'woocommerce_customer_to_phppos';
13 $this->method_title = __( 'Woocommerce Customer To PHPPOS', 'woocommerce_customer_to_phppos_exporter' );
14 $this->method_description = __('Woocommerce Customer To PHPPOS is extension to export the customers of woocommerce to PHPPOS', 'woocommerce_customer_to_phppos_exporter');
15
16 $this->init_form_fields();
17
18 $this->init_settings();
19
20 $this->phppos_url = $this->get_option('phppos_url');
21 $this->api_key = $this->get_option('api_key');
22
23 add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) );
24 add_action( 'woocommerce_new_order', array($this, 'save_sale_on_phppos') );
25 }
26
27 public function init_form_fields()
28 {
29 $this->form_fields = array(
30 'phppos_url' => array(
31 'title' => __( 'PHPPOS Url', 'woocommerce_customer_to_phppos_exporter' ),
32 'description' => __( 'Enter the url of your PHPPOS Site to Access the REST API', 'woocommerce_customer_to_phppos_exporter' ),
33 'type' => 'text',
34 'desc_tip' => true,
35 'default' => 'https://demo.phppointofsale.com/index.php/api/v1'
36 ),
37 'api_key' => array(
38 'title' => __( 'Api Key', 'woocommerce_customer_to_phppos_exporter' ),
39 'description' => __( 'Enter the API key with permition for READ and WRITE from your PHPPOS', 'woocommerce_customer_to_phppos_exporter' ),
40 'type' => 'text',
41 'desc_tip' => true,
42 'default' => 'oks00okggs4s4808o88gggk8kg8ocs0gogsko0gs'
43 )
44 );
45 }
46
47 public function save_sale_on_phppos( $order_id )
48 {
49 $order = new WC_Order($order_id);
50
51 $customer = $this->getOrCreateCustomer($this->prepare_api_config(), $order)[0];
52
53 $cart_items = $this->prepare_cart_items($order);
54 $payment = $this->prepare_body_payment($order);
55
56 $body_sale = $this->prepare_body_sale($order, $customer['person_id'], $payment, $cart_items);
57
58 $request = new \OpenAPI\Client\Api\SalesApi(new GuzzleHttp\Client(), $this->prepare_api_config());
59
60 try {
61 $result = $request->addSale($body_sale);
62 $this->logger(json_encode($result));
63 } catch(Exception $e) {
64 echo 'Exception when calling CustomersApi->searchCustomers: ', $e->getMessage(), PHP_EOL;
65 $this->logger('Exception when calling CustomersApi->searchCustomers: ' . $e->getMessage());
66 }
67 }
68
69 public function prepare_body_customer( $order )
70 {
71 $body = array(
72 'first_name' => $order->get_billing_first_name(),
73 'last_name' => $order->get_billing_last_name(),
74 'email' => $order->get_billing_email(),
75 'phone_number' => preg_replace('/[^0-9]/', '', $order->get_billing_phone()),
76 'address_1' => $order->get_billing_address_1(),
77 'address_2' => $order->get_billing_address_2(),
78 'city' => $order->get_billing_city(),
79 'state' => $order->get_billing_state(),
80 'zip' => $order->get_billing_postcode(),
81 'country' => $order->get_billing_country(),
82 'comments' => "Customer Exporter From the Online Store",
83 'company_name' => $order->get_billing_company(),
84 );
85
86 return new \OpenAPI\Client\Model\NewCustomer($body);
87 }
88
89 public function prepare_body_payment($order)
90 {
91 $body = array(
92 'payment_type' => $order->get_payment_method(),
93 'payment_amount' => $order->get_total(),
94 'payment_date' => $order->get_date_paid()
95 );
96
97 // return new \OpenApi\Client\Model\CartPayment($body);
98 return $body;
99 }
100
101 public function prepare_cart_items($order)
102 {
103 $body = array();
104
105 foreach($order->get_items() as $item_id => $item_data) {
106 array_push($body, $this->prepare_cart_item($item_data));
107 }
108
109 return $body;
110 }
111
112 public function prepare_cart_item($item)
113 {
114 $body = array(
115 'quantity' => $item->get_quantity(),
116 'unit_price' => $item->get_total(),
117 'name' => $item->get_name(),
118 'cost_price' => $item->get_total(),
119 );
120
121 return new \OpenAPI\Client\Model\SaleCartItem($body);
122 }
123
124 public function prepare_body_sale($order, $customer_id, $body_payment, $cart_items)
125 {
126 $delivery = new \OpenAPI\Client\Model\Delivery(null);
127 $body = array(
128 'mode' => 'sale',
129 'customer_id' => $customer_id,
130 'show_comment_on_receipt' => false,
131 'comment' => "Created from the Online Store",
132 'suspended' => 1,
133 'payment' => [
134 $body_payment
135 ],
136 'cart_items' => $cart_items,
137 'has_delivery' => false,
138 // 'delivery' => $delivery
139 );
140
141 // return new \OpenAPI\Client\Model\NewSale($body);
142 return $body;
143 }
144
145 public function getOrCreateCustomer($config, $order)
146 {
147 $request = new \OpenAPI\Client\Api\CustomersApi(new GuzzleHttp\Client(), $config);
148
149 try {
150 $result = $request->searchCustomers(null, preg_replace('/[^0-9]/', '', $order->get_billing_phone()), null, null, 'phone_number', 0, 1);
151
152 if (empty($result)) {
153 try {
154 $result = array(
155 $request->addCustomer($this->prepare_body_customer($order))
156 );
157
158 print_r($result);
159 } catch (Exception $e) {
160 echo 'Exception when calling CustomersApi->addCustomer: ', $e->getMessage(), PHP_EOL;
161 }
162 }
163
164 return $result;
165
166 } catch (Exception $e) {
167 echo 'Exception when calling CustomersApi->searchCustomers: ', $e->getMessage(), PHP_EOL;
168 }
169 }
170
171 // public function customer_not_exist($config, $order)
172 // {
173 // $request = new \OpenAPI\Client\Api\CustomersApi(new GuzzleHttp\Client(), $config, null, 1);
174
175 // try {
176 // $result = $request->searchCustomers(null, preg_replace('/[^0-9]/', '', $order->get_billing_phone()), null, null, 'phone_number', 0, 1);
177 // print_r($result);
178
179 // return empty($result);
180
181 // } catch (Exception $e) {
182 // echo 'Exception when calling CustomersApi->searchCustomers: ', $e->getMessage(), PHP_EOL;
183 // }
184 // }
185
186 protected function logger( $data )
187 {
188 $logger = new WC_Logger();
189 $logger->add('WC_CUSTOMER_TO_PHPPOS_DEBUG', $data);
190 }
191
192 protected function prepare_api_config( )
193 {
194 $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setHost(
195 esc_url($this->get_option('phppos_url'))
196 )->setApiKey(
197 'x-api-key',
198 $this->get_option('api_key')
199 );
200 return $config;
201 }
202 }
203
204endif;