PluginProbe
MakeCommerce for WooCommerce / 2.3.1
MakeCommerce for WooCommerce v2.3.1
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / makecommerce-simplecheckout.php

makecommerce-simplecheckout.php in MakeCommerce for WooCommerce 2.3.1, at makecommerce-simplecheckout.php

437 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * SimpleCheckout functionality
5 */
6
7
8 function sc_return_trigger($vars) {
9 $vars[] = 'mc_cart_to_order';
10 $vars[] = 'mc_calculate_shipment';
11 $vars[] = 'mc_cart_update';
12 $vars[] = 'mc_cart_id';
13 $vars[] = 'mc_nonce';
14 return $vars;
15 }
16 function sc_return_trigger_check() {
17 if (intval(get_query_var('mc_cart_to_order')) === 1) {
18 $cart_info = json_decode(file_get_contents('php://input'));
19 global $wpdb;
20 $tmp_order = $wpdb->get_row("select content, order_id from {$wpdb->prefix}woocommerce_makecommerce_sc where cart_id = '{$cart_info->cartId}'");
21 if (!$tmp_order) {
22 error_log('no such cart');
23 echo json_encode(array('code' => -1));
24 exit;
25 }
26 $tmp_cart = new WC_Cart();
27 $tmp_order->content = json_decode($tmp_order->content);
28 $content = !empty($tmp_order->content->order) ? $tmp_order->content->order : $tmp_order->content;
29 $coupons = !empty($tmp_order->content->coupons) ? $tmp_order->content->coupons : array();
30 foreach ($content as $item_row) {
31 $tmp_cart->add_to_cart($item_row->id, $item_row->qty);
32 }
33 if ($tmp_order->order_id) {
34 $order = new WC_Order($tmp_order->order_id);
35 } else {
36 $order = wc_create_order();
37 foreach ($content as $item_row) {
38 $product_id = !empty($item_row->var) ? $item_row->var : $item_row->id;
39 if (function_exists('wc_get_product')) {
40 $item_id = $order->add_product(wc_get_product($product_id), $item_row->qty);
41 } else {
42 $item_id = $order->add_product(get_product($product_id), $item_row->qty);
43 }
44 }
45 $order->calculate_totals();
46 foreach ($coupons as $coupon) {
47 $coupon = new WC_Coupon($coupon);
48 $amount = $coupon->get_amount();
49 if ($coupon->is_type('percent')) {
50 $amount = $order->get_total() / 100 * $amount;
51 }
52 if (method_exists($order, 'add_item')) {
53 $item = new WC_Order_Item_Coupon();
54 $item->set_props(array(
55 'code' => $coupon->get_code(),
56 'discount' => $amount,
57 'discount_tax' => 0
58 ));
59 $order->add_item($item);
60 } else {
61 //$order->add_code($coupon->get_code(), $amount);
62 }
63 }
64 }
65 $payment_method = new woocommerce_makecommerce();
66 $order->set_payment_method($payment_method);
67 $billing_address = array(
68 'first_name' => $cart_info->customer->firstname,
69 'last_name' => $cart_info->customer->lastname,
70 'email' => $cart_info->customer->email,
71 'phone' => $cart_info->customer->phone,
72 );
73 $shipping_address = $billing_address;
74 $shipment_method = false;
75 if (!empty($cart_info->invoiceAddress)) {
76 $invoice = $cart_info->invoiceAddress;
77 $billing_address['company'] = '';
78 if (!empty($invoice->firstname)) { $billing_address['first_name'] = $invoice->firstname; }
79 if (!empty($invoice->lastname)) { $billing_address['last_name'] = $invoice->lastname; }
80 if (!empty($invoice->country)) { $billing_address['country'] = $invoice->country; }
81 if (!empty($invoice->county)) { $billing_address['state'] = $invoice->county; }
82 if (!empty($invoice->city)) { $billing_address['city'] = $invoice->city; }
83 if (!empty($invoice->street1)) { $billing_address['address_1'] = $invoice->street1; }
84 if (!empty($invoice->street2)) { $billing_address['address_2'] = $invoice->street2; }
85 if (!empty($invoice->postalCode)) { $billing_address['postcode'] = $invoice->postalCode; }
86 if (!empty($invoice->legalName)) { $billing_address['company'] .= $invoice->legalName; }
87 if (!empty($invoice->registryCode)) { $billing_address['company'] .= ' ' . $invoice->registryCode; }
88 if (!empty($invoice->vatNum)) { $billing_address['company'] .= ' ' . $invoice->vatNum; }
89 }
90 if (!empty($cart_info->shipmentAddress)) {
91 $shipment = $cart_info->shipmentAddress;
92 if (!empty($shipment->country)) { $shipment_address['country'] = $shipment->country; }
93 if (!empty($shipment->county)) { $shipment_address['state'] = $shipment->county; }
94 if (!empty($shipment->city)) { $shipment_address['city'] = $shipment->city; }
95 if (!empty($shipment->street1)) { $shipment_address['address_1'] = $shipment->street1; }
96 if (!empty($shipment->street2)) { $shipment_address['address_2'] = $shipment->street2; }
97 if (!empty($shipment->postalCode)) { $shipment_address['postcode'] = $shipment->postalCode; }
98 if (!empty($shipment->firstname)) { $shipment_address['first_name'] = $shipment->firstname; }
99 if (!empty($shipment->lastname)) { $shipment_address['last_name'] = $shipment->lastname; }
100 }
101 if (!empty($cart_info->shipmentMethod)) {
102 $shipment_details = $cart_info->shipmentMethod;
103 $zones = WC_Shipping_Zones::get_zones();
104 $continents = WC()->countries->get_continents();
105 $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU');
106 foreach ($zones as $zone) {
107 if ($shipment_method) { continue; }
108 foreach ($zone['shipping_methods'] as $method) {
109 if ($shipment_method) { continue; }
110 if (empty($supported_methods[$method->id])) { continue; }
111 if ($supported_methods[$method->id] === $shipment_details->type && (empty($shipment_details->carrier) || strtoupper($method->carrier) === $shipment_details->carrier)) {
112 foreach ($zone['zone_locations'] as $location) {
113 if ($location->type === 'continent' && !empty($continents[$location->code])) {
114 if (in_array($shipment->country, $continents[$location->code]['countries'])) {
115 $shipment_method = $method;
116 }
117 } else if ($location->type === 'state') {
118 list($country, $state) = explode(':', $location->code);
119 if ($country === $shipment->country) {
120 $shipment_method = $method;
121 }
122 } else if ($shipment->country === $location->code) {
123 $shipment_method = $method;
124 }
125 }
126 }
127 }
128 }
129 }
130 $order->set_address($billing_address, 'billing');
131 if (empty($shipment_address)) {
132 $order->set_address($billing_address, 'shipping');
133 } else {
134 $order->set_address($shipment_address, 'shipping');
135 }
136 $order->remove_order_items('shipping');
137 if ($shipment_method) {
138 $package = $tmp_cart->get_shipping_packages();
139 if (!empty($package)) { $package = $package[0]; }
140 $price_int = $shipment_method->get_rates_for_package($package);
141 $price_int = get_rate_with_taxes($shipment_method->id.':'.$shipment_method->instance_id, $price_int);
142 $price = !empty($cart_info->shipmentMethod->amount) ? (double)$cart_info->shipmentMethod->amount : 0;
143 //if (round($price_int,2) != round($price,2)) { error_log('Price does not match ['.round($price,2).'] vs ['.round($price_int,2).']'); exit; }
144 $rate = new WC_Shipping_Rate($shipment_method->id.':'.$shipment_method->instance_id, !empty($shipment_method->name_ext) ? $shipment_method->name_ext : $shipment_method->method_title, $price_int, array(), $shipment_method->id.':'.$shipment_method->instance_id);
145 if (class_exists('WC_Order_Item_Shipping')) {
146 $item = new WC_Order_Item_Shipping();
147 $item->set_order_id($order->get_id());
148 $item->set_shipping_rate($rate);
149 $shipping_id = $item->save();
150 } else {
151 $order->add_shipping($rate);
152 }
153 if (!empty($shipment_method->type) && $shipment_method->type === 'apt') {
154 $machine = mk_get_machine(strtolower($shipment_method->carrier), $shipment->destinationId);
155 if ($machine) {
156 update_post_meta($order->get_order_number(), '_shipping_first_name', get_post_meta($order->get_order_number(), '_billing_first_name', true));
157 update_post_meta($order->get_order_number(), '_shipping_last_name', get_post_meta($order->get_order_number(), '_billing_last_name', true));
158 update_post_meta($order->get_order_number(), '_shipping_address_1', sanitize_text_field($machine['name']));
159 update_post_meta($order->get_order_number(), '_shipping_address_2', sanitize_text_field($machine['address']));
160 update_post_meta($order->get_order_number(), '_shipping_city', sanitize_text_field($machine['city']));
161 update_post_meta($order->get_order_number(), '_shipping_postcode', '');
162 update_post_meta($order->get_order_number(), '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId);
163 }
164 }
165 }
166 update_post_meta($order->get_order_number(), '_makecommerce_sc_cart_id', $cart_info->cartId);
167 $order->calculate_totals();
168 if (!empty($tmp_order->content->discount)) { $order->set_discount_total($tmp_order->content->discount); error_log($tmp_order->content->discount); }
169 if (!empty($tmp_order->content->discount_tax)) { $order->set_discount_tax($tmp_order->content->discount_tax); error_log($tmp_order->content->discount_tax); }
170 $order->set_total($order->get_total() - ($order->get_discount_total() + $order->get_discount_tax()));
171 $order->save();
172 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('status' => 1, 'order_id' => $order->get_order_number(), 'modified' => time()), array('cart_id' => $cart_info->cartId));
173 $locale = get_locale();
174 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
175 $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale;
176 $response = array(
177 'cartId' => $cart_info->cartId,
178 'reference' => (string)$order->get_order_number(),
179 'locale' => $locale,
180 'transactionUrl' => array(
181 'returnUrl' => array(
182 'url' => site_url('/?mc_cart_update=1'),
183 'method' => 'POST'
184 ),
185 'cancelUrl' => array(
186 'url' => site_url('/?mc_cart_update=2'),
187 'method' => 'POST'
188 ),
189 'notificationUrl' => array(
190 'url' => site_url('/?mc_cart_update=3'),
191 'method' => 'POST'
192 ),
193 )
194 );
195 header('Content-type: application/json');
196 echo json_encode($response);
197 exit;
198 }
199 if (intval(get_query_var('mc_calculate_shipment')) === 1) {
200 $cart_info = json_decode(file_get_contents('php://input'));
201 header('Content-type: application/json');
202 echo json_encode(array('amount' => 2.99));
203 }
204 if (intval(get_query_var('mc_cart_update')) > 0) {
205 $return_url = mk_check_payment();
206 if (intval(get_query_var('mc_cart_update')) === 3) {
207 echo json_encode(array('redirect' => $return_url));
208 } else {
209 wp_redirect($return_url);
210 }
211 exit;
212 }
213 }
214 function sc_cart_scripts() {
215 ?>
216 <script>
217 jQuery(document).on('updated_cart_totals', function(){
218 var rand = Math.round(Math.random() * 100000);
219 window.history.pushState({rand: rand}, "Rand "+rand, "./?r="+rand);
220 });
221 </script>
222 <?php
223 $mcsc = new woocommerce_makecommerce_sc();
224 $mcsc->before_cart();
225 }
226 add_filter('query_vars', 'sc_return_trigger');
227 add_action('template_redirect', 'sc_return_trigger_check');
228 add_action('woocommerce_before_cart', 'sc_cart_scripts');
229
230 function woocommerce_makecommerce_sc_init() {
231
232 load_plugin_textdomain('wc_makecommerce_sc_domain', false, dirname(plugin_basename(__FILE__)) . '/');
233
234 class woocommerce_makecommerce_sc extends WC_Payment_Gateway {
235 function __construct() {
236 $this->id = 'makecommerce_sc';
237 $this->method_title = __('SimpleCheckout (MC)', 'wc_makecommerce_sc_domain');
238 $this->init();
239 }
240 function init() {
241 $this->init_form_fields();
242 $this->init_settings();
243 add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1);
244 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
245 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
246 }
247 function before_cart() {
248 if (!$this->is_available()) { return; }
249 if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') {
250 echo '<style>.shipping,.order-total{display:none;}</style>';
251 echo '<style>.tax-rate{display:none;}</style>';
252 }
253 }
254 function install_tables() {
255 global $wpdb;
256 $charset_collate = $wpdb->get_charset_collate();
257 $table_name = $wpdb->prefix . "woocommerce_makecommerce_sc";
258 $sql = "CREATE TABLE `$table_name` (
259 `id` int(11) unsigned not null auto_increment,
260 `cart_id` varchar(64) not null default '',
261 `order_id` int(10) unsigned not null,
262 `created` int(10) unsigned not null,
263 `modified` int(10) unsigned not null,
264 `status` tinyint unsigned not null,
265 `content` mediumtext,
266 unique key id (`id`),
267 key cart_id (`cart_id`)
268 ) $charset_collate;";
269 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
270 dbDelta($sql);
271 }
272 function take_over_checkout($checkout) {
273 if (!$this->is_available()) {
274 return;
275 }
276 $this->install_tables();
277 global $woocommerce, $wpdb;
278 if (get_option('woocommerce_enable_guest_checkout') !== 'yes' && !is_user_logged_in()) {
279 wc_add_notice(__('You have to log in to continue to checkout', 'wc_makecommerce_domain'), 'error');
280 $redir_url = $woocommerce->cart->get_cart_url();
281 wp_redirect($redir_url);
282 exit;
283 }
284 $data = array(
285 'order' => array(),
286 'coupons' => $woocommerce->cart->get_applied_coupons(),
287 'discount' => $woocommerce->cart->get_cart_discount_total(),
288 'discount_tax' => $woocommerce->cart->get_cart_discount_tax_total()
289 );
290 $qty = 0; $amount = 0;
291 $cart = $woocommerce->cart->get_cart();
292 foreach ($cart as $cart_item) {
293 $data['order'][] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity'], 'var' => $cart_item['variation_id']);
294 $qty += $cart_item['quantity'];
295 $amount += $cart_item['line_total'] + $cart_item['line_tax'];
296 }
297 if (count($data) > 0) {
298 $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array(
299 'created' => time(),
300 'modified' => time(),
301 'status' => 0,
302 'content' => json_encode($data)
303 ));
304 $tmp_order_id = $wpdb->insert_id;
305 $cart_to_order_url = site_url('/?mc_cart_to_order=1');
306 $calculate_shipment_url = site_url('/?mc_calculate_shipment=1');
307
308 $locale = get_locale();
309 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
310 $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale;
311 if (empty($locale)) { $locale = 'ee'; }
312 $tos_url = !empty($this->settings['shop_tos_url']) ? $this->settings['shop_tos_url'] : site_url();
313 $data = array(
314 'cartRef' => 'PreOrder '.$tmp_order_id,
315 'pluginUrls' => array(
316 'cartToOrder' => $cart_to_order_url,
317 'calculateShipment' => $calculate_shipment_url,
318 'tos' => $tos_url
319 ),
320 'amount' => sprintf("%.2f", round($amount, 2)),
321 'currency' => 'EUR',
322 'sourceCountry' => 'EE',
323 'locale' => $locale,
324 'shipmentMethods' => array()
325 );
326 $package = $woocommerce->cart->get_shipping_packages();
327 if (!empty($package)) { $package = $package[0]; }
328 $zones = array();
329
330 $zone = new WC_Shipping_Zone(0);
331 $zones[ $zone->get_id() ] = $zone->get_data();
332 $zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
333 $zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods();
334 $zones = array_merge( $zones, WC_Shipping_Zones::get_zones() );
335 $continents = WC()->countries->get_continents();
336 $method_country_x = array();
337 $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU');
338 foreach ($zones as $zone) {
339 foreach ($zone['shipping_methods'] as $method_key => $method) {
340 if ($method->enabled !== 'yes') { continue; }
341 if (!empty($supported_methods[$method->id])) {
342 foreach ($woocommerce->cart->get_shipping_packages() as $package) {
343 if (!$method->is_available($package)) { continue(2); }
344 }
345 $method_type = $supported_methods[$method->id];
346 if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); }
347 $carrier = array('countries' => array());
348 if (!empty($method->carrier)) { $carrier['carrier'] = mb_strtoupper($method->carrier); }
349 if (empty($zone['zone_locations'])) {
350 $carrier['countries'] = array_keys(WC()->countries->get_allowed_countries());
351 } else {
352 foreach ($zone['zone_locations'] as $location) {
353 if ($location->type === 'continent' && !empty($continents[$location->code])) {
354 $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]);
355 $carrier['countries'] = array_merge($carrier['countries'], $countries);
356 } else if ($location->type === 'state') {
357 list($country, $state) = explode(':', $location->code);
358 $carrier['countries'][] = $country;
359 } else if ($location->type === 'country') {
360 $carrier['countries'][] = $location->code;
361 }
362 }
363 }
364 $method_country_x[$method_type] = array_merge($method_country_x[$method_type], $carrier['countries']);
365 $prices = $method->get_rates_for_package($package);
366 $price = get_rate_with_taxes($method->id.':'.$method_key, $prices);
367 $carrier['type'] = strtoupper($method_type);
368 $carrier['name'] = $method->title;
369 $carrier['methodId'] = $method->id . ':' . $method_key;
370 $carrier['amount'] = sprintf("%.2f", round($price, 2));
371 $data['shipmentMethods'][] = $carrier;
372 }
373 }
374 }
375 $MK = mk_get_api();
376 $cart = $MK->createCart($data);
377 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id));
378 wp_redirect($cart->scoUrl);
379 exit;
380 }
381 die('no content');
382 }
383 function init_form_fields() {
384 $this->form_fields = array();
385 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
386 $this->form_fields['scointro'] = array(
387 'type' => 'title',
388 'description' => __('SimpleCheckout replaces Woocommerce built-in check-out dialog with Makecommerce hosted dialog. It is more convenient and faster for your customers', 'wc_makecommerce_domain').'<br>'.__('See more about SimpleCheckout on: <a target=_blank href="https://makecommerce.net/simplecheckout/">makecommerce.net/simplecheckout</a>', 'wc_makecommerce_domain'),
389 );
390 $this->form_fields['active'] = array(
391 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
392 'type' => 'checkbox',
393 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'),
394 'default' => 'no'
395 );
396 $this->form_fields['shop_tos_url'] = array(
397 'title' => __('Shop ToS url', 'wc_makecommerce_domain'),
398 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'),
399 'type' => 'text',
400 );
401 $this->form_fields['hide_shipping_methods'] = array(
402 'title' => __('Hide shipping methods block', 'wc_makecommerce_domain'),
403 'type' => 'checkbox',
404 'label' => __('Hide shipping methods block on cart page. This will make it look more clean and simple', 'wc_makecommerce_domain'),
405 'default' => 'no'
406 );
407 }
408 public function is_available() {
409 if ($this->settings['active'] == "yes") {
410 return true;
411 }
412 return false;
413 }
414
415 }
416
417 }
418
419 function get_rate_with_taxes($method, $rate) {
420 if (empty($rate[$method])) { return 0; }
421 $rate = $rate[$method];
422 $price = $rate->cost;
423 if (empty($rate->taxes)) { return $price; }
424 foreach ($rate->taxes as $tax) {
425 $price += $tax;
426 }
427 return $price;
428 }
429 function woocommerce_payment_makecommerce_sc_add($methods) {
430 $methods[] = 'woocommerce_makecommerce_sc';
431 return $methods;
432 }
433 add_action('plugins_loaded', 'woocommerce_makecommerce_sc_init');
434 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_sc_add');
435
436
437