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

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