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

445 lines 19.9 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_with_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_order_number(), '_shipping_first_name', get_post_meta($order->get_order_number(), '_billing_first_name', true));
160 update_post_meta($order->get_order_number(), '_shipping_last_name', get_post_meta($order->get_order_number(), '_billing_last_name', true));
161 update_post_meta($order->get_order_number(), '_shipping_address_1', sanitize_text_field($machine['name']));
162 update_post_meta($order->get_order_number(), '_shipping_address_2', sanitize_text_field($machine['address']));
163 update_post_meta($order->get_order_number(), '_shipping_city', sanitize_text_field($machine['city']));
164 update_post_meta($order->get_order_number(), '_shipping_postcode', '');
165 update_post_meta($order->get_order_number(), '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId);
166 }
167 }
168 }
169 update_post_meta($order->get_order_number(), '_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_order_number(), '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_order_number(),
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 add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1);
247 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
248 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
249 }
250 function before_cart() {
251 if (!$this->is_available()) { return; }
252 if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') {
253 echo '<style>.shipping,.order-total{display:none;}</style>';
254 echo '<style>.tax-rate{display:none;}</style>';
255 }
256 }
257 function install_tables() {
258 global $wpdb;
259 $charset_collate = $wpdb->get_charset_collate();
260 $table_name = $wpdb->prefix . "woocommerce_makecommerce_sc";
261 $sql = "CREATE TABLE `$table_name` (
262 `id` int(11) unsigned not null auto_increment,
263 `cart_id` varchar(64) not null default '',
264 `order_id` int(10) unsigned not null,
265 `created` int(10) unsigned not null,
266 `modified` int(10) unsigned not null,
267 `status` tinyint unsigned not null,
268 `content` mediumtext,
269 unique key id (`id`),
270 key cart_id (`cart_id`)
271 ) $charset_collate;";
272 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
273 dbDelta($sql);
274 }
275 function take_over_checkout($checkout) {
276 if (!$this->is_available()) {
277 return;
278 }
279 $this->install_tables();
280 global $woocommerce, $wpdb;
281 if (get_option('woocommerce_enable_guest_checkout') !== 'yes' && !is_user_logged_in()) {
282 wc_add_notice(__('You have to log in to continue to checkout', 'wc_makecommerce_domain'), 'error');
283 $redir_url = $woocommerce->cart->get_cart_url();
284 wp_redirect($redir_url);
285 exit;
286 }
287 $data = array(
288 'order' => array(),
289 'coupons' => $woocommerce->cart->get_applied_coupons(),
290 'discount' => $woocommerce->cart->get_cart_discount_total(),
291 'discount_tax' => $woocommerce->cart->get_cart_discount_tax_total()
292 );
293 $free_shipping = false;
294 foreach ($woocommerce->cart->get_applied_coupons() as $coupon) {
295 $coupon = new WC_Coupon($coupon);
296 if ((method_exists($coupon, 'get_free_shipping') && $coupon->get_free_shipping()) || $coupon->free_shipping) { $free_shipping = true; }
297 }
298 $qty = 0; $amount = 0;
299 $cart = $woocommerce->cart->get_cart();
300 foreach ($cart as $cart_item) {
301 $data['order'][] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity'], 'var' => $cart_item['variation_id']);
302 $qty += $cart_item['quantity'];
303 $amount += $cart_item['line_total'] + $cart_item['line_tax'];
304 }
305 if (count($data) > 0) {
306 $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array(
307 'created' => time(),
308 'modified' => time(),
309 'status' => 0,
310 'content' => json_encode($data)
311 ));
312 $tmp_order_id = $wpdb->insert_id;
313 $cart_to_order_url = site_url('/?mc_cart_to_order=1');
314 $calculate_shipment_url = site_url('/?mc_calculate_shipment=1');
315
316 $locale = get_locale();
317 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
318 $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale;
319 if (empty($locale)) { $locale = 'ee'; }
320 $tos_url = !empty($this->settings['shop_tos_url']) ? $this->settings['shop_tos_url'] : site_url();
321 $data = array(
322 'cartRef' => 'PreOrder '.$tmp_order_id,
323 'pluginUrls' => array(
324 'cartToOrder' => $cart_to_order_url,
325 'calculateShipment' => $calculate_shipment_url,
326 'tos' => $tos_url
327 ),
328 'amount' => sprintf("%.2f", round(max($amount, 0.01), 2)),
329 'currency' => 'EUR',
330 'sourceCountry' => 'EE',
331 'locale' => $locale,
332 'shipmentMethods' => array()
333 );
334 $package = $woocommerce->cart->get_shipping_packages();
335 if (!empty($package)) { $package = $package[0]; }
336 $zones = array();
337
338 $zone = new WC_Shipping_Zone(0);
339 $zones[ $zone->get_id() ] = $zone->get_data();
340 $zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
341 $zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods();
342 $zones = array_merge( $zones, WC_Shipping_Zones::get_zones() );
343 $continents = WC()->countries->get_continents();
344 $method_country_x = array();
345 $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU');
346 foreach ($zones as $zone) {
347 foreach ($zone['shipping_methods'] as $method_key => $method) {
348 if ($method->enabled !== 'yes') { continue; }
349 if (!empty($supported_methods[$method->id])) {
350 foreach ($woocommerce->cart->get_shipping_packages() as $package) {
351 if (!$method->is_available($package)) { continue(2); }
352 }
353 $method_type = $supported_methods[$method->id];
354 if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); }
355 $carrier = array('countries' => array());
356 if (!empty($method->carrier)) { $carrier['carrier'] = mb_strtoupper($method->carrier); }
357 if (empty($zone['zone_locations'])) {
358 $carrier['countries'] = array_keys(WC()->countries->get_allowed_countries());
359 } else {
360 foreach ($zone['zone_locations'] as $location) {
361 if ($location->type === 'continent' && !empty($continents[$location->code])) {
362 $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]);
363 $carrier['countries'] = array_merge($carrier['countries'], $countries);
364 } else if ($location->type === 'state') {
365 list($country, $state) = explode(':', $location->code);
366 $carrier['countries'][] = $country;
367 } else if ($location->type === 'country') {
368 $carrier['countries'][] = $location->code;
369 }
370 }
371 }
372 $method_country_x[$method_type] = array_merge($method_country_x[$method_type], $carrier['countries']);
373 $prices = $method->get_rates_for_package($package);
374 $price = $free_shipping ? 0.00 : get_rate_with_taxes($method->id.':'.$method_key, $prices);
375 $carrier['type'] = strtoupper($method_type);
376 $carrier['name'] = $method->title;
377 $carrier['methodId'] = $method->id . ':' . $method_key;
378 $carrier['amount'] = sprintf("%.2f", round($price, 2));
379 $data['shipmentMethods'][] = $carrier;
380 }
381 }
382 }
383 $MK = mk_get_api();
384 $cart = $MK->createCart($data);
385 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id));
386 wp_redirect($cart->scoUrl);
387 exit;
388 }
389 die('no content');
390 }
391 function init_form_fields() {
392 $this->form_fields = array();
393 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
394 $this->form_fields['scointro'] = array(
395 'type' => 'title',
396 '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'),
397 );
398 $this->form_fields['active'] = array(
399 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
400 'type' => 'checkbox',
401 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'),
402 'default' => 'no'
403 );
404 $this->form_fields['shop_tos_url'] = array(
405 'title' => __('Shop ToS url', 'wc_makecommerce_domain'),
406 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'),
407 'type' => 'text',
408 );
409 $this->form_fields['hide_shipping_methods'] = array(
410 'title' => __('Hide shipping methods block', 'wc_makecommerce_domain'),
411 'type' => 'checkbox',
412 'label' => __('Hide shipping methods block on cart page. This will make it look more clean and simple', 'wc_makecommerce_domain'),
413 'default' => 'no'
414 );
415 }
416 public function is_available() {
417 if ($this->settings['active'] == "yes") {
418 return true;
419 }
420 return false;
421 }
422
423 }
424
425 }
426
427 function get_rate_with_taxes($method, $rate) {
428 if (empty($rate[$method])) { return 0; }
429 $rate = $rate[$method];
430 $price = $rate->cost;
431 if (empty($rate->taxes)) { return $price; }
432 foreach ($rate->taxes as $tax) {
433 $price += $tax;
434 }
435 return $price;
436 }
437 function woocommerce_payment_makecommerce_sc_add($methods) {
438 $methods[] = 'woocommerce_makecommerce_sc';
439 return $methods;
440 }
441 add_action('plugins_loaded', 'woocommerce_makecommerce_sc_init');
442 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_sc_add');
443
444
445