PluginProbe
MakeCommerce for WooCommerce / 2.5.13
MakeCommerce for WooCommerce v2.5.13
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.5.13, at makecommerce-simplecheckout.php

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