| @@ -10,13 +10,29 @@ | ||
| 10 | 10 | $vars[] = 'mc_calculate_shipment'; |
| 11 | 11 | $vars[] = 'mc_cart_update'; |
| 12 | 12 | $vars[] = 'mc_cart_id'; |
| 13 | 13 | $vars[] = 'mc_nonce'; |
| 14 | + $vars[] = 'lang1'; | |
| 14 | 15 | return $vars; |
| 15 | 16 | } |
| 16 | 17 | function sc_return_trigger_check() { |
| 17 | 18 | if (intval(get_query_var('mc_cart_to_order')) === 1) { |
| 18 | 19 | $cart_info = json_decode(file_get_contents('php://input')); |
| 20 | + | |
| 21 | + if (isset($_GET["lang1"])) { | |
| 22 | + $_GET["lang"]=$_GET["lang1"]; | |
| 23 | + //set wpml language | |
| 24 | + if (function_exists('icl_object_id')) { | |
| 25 | + do_action('wpml_switch_language', $_GET["lang1"]); | |
| 26 | + | |
| 27 | + } else { | |
| 28 | + //set polylang language | |
| 29 | + if (function_exists('pll_current_language')) { | |
| 30 | + //not implemented yet | |
| 31 | + } | |
| 32 | + } | |
| 33 | + } | |
| 34 | + | |
| 19 | 35 | global $wpdb; |
| 20 | 36 | $tmp_order = $wpdb->get_row("select content, order_id from {$wpdb->prefix}woocommerce_makecommerce_sc where cart_id = '{$cart_info->cartId}'"); |
| 21 | 37 | if (!$tmp_order) { |
| 22 | 38 | error_log('no such cart'); |
| @@ -22,10 +38,42 @@ | ||
| 22 | 38 | error_log('no such cart'); |
| 23 | 39 | echo json_encode(array('code' => -1)); |
| 24 | 40 | exit; |
| 25 | 41 | } |
| 42 | + | |
| 43 | + //check for mitm manipulations | |
| 44 | + $tmp_order_sco_data = $wpdb->get_row("select content, order_id from {$wpdb->prefix}woocommerce_makecommerce_sc where cart_id = '{$cart_info->cartId}_provided_sco_data'"); | |
| 45 | + if (!$tmp_order_sco_data) { | |
| 46 | + error_log('Missing sco provided data for cart'); | |
| 47 | + echo json_encode(array('code' => -1)); | |
| 48 | + exit; | |
| 49 | + } else { | |
| 50 | + $matching_shipment_method_found = false; | |
| 51 | + $provided_cart_data = json_decode($tmp_order_sco_data->content); | |
| 52 | + | |
| 53 | + foreach ($provided_cart_data->shipmentMethods as $p_shipment_method) { | |
| 54 | + //check to see if the selected shipment has a matching cart id and amount. | |
| 55 | + if ($p_shipment_method->methodId == $cart_info->shipmentMethod->methodId && $p_shipment_method->amount == $cart_info->shipmentMethod->amount) { | |
| 56 | + $matching_shipment_method_found = true; | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + //response option does not match with provided sco data options. Either something went terribly wrong or someone manipulated with the data. Cancel payment | |
| 61 | + if (!$matching_shipment_method_found) { | |
| 62 | + error_log('Cart shipping method with matching price not found in sco_provided_data'); | |
| 63 | + header("HTTP/1.1 400 Bad Request"); | |
| 64 | + exit; | |
| 65 | + } | |
| 66 | + | |
| 67 | + } | |
| 68 | + | |
| 69 | + $free_shipping = false; | |
| 70 | + | |
| 26 | 71 | $tmp_cart = new WC_Cart(); |
| 27 | - foreach (json_decode($tmp_order->content) as $item_row) { | |
| 72 | + $tmp_order->content = json_decode($tmp_order->content); | |
| 73 | + $content = !empty($tmp_order->content->order) ? $tmp_order->content->order : $tmp_order->content; | |
| 74 | + $coupons = !empty($tmp_order->content->coupons) ? $tmp_order->content->coupons : array(); | |
| 75 | + foreach ($content as $item_row) { | |
| 28 | 76 | $tmp_cart->add_to_cart($item_row->id, $item_row->qty); |
| 29 | 77 | } |
| 30 | 78 | if ($tmp_order->order_id) { |
| 31 | 79 | $order = new WC_Order($tmp_order->order_id); |
| @@ -30,11 +78,37 @@ | ||
| 30 | 78 | if ($tmp_order->order_id) { |
| 31 | 79 | $order = new WC_Order($tmp_order->order_id); |
| 32 | 80 | } else { |
| 33 | 81 | $order = wc_create_order(); |
| 34 | - foreach (json_decode($tmp_order->content) as $item_row) { | |
| 35 | - $item_id = $order->add_product(get_product($item_row->id), $item_row->qty); | |
| 82 | + foreach ($content as $item_row) { | |
| 83 | + $product_id = !empty($item_row->var) ? $item_row->var : $item_row->id; | |
| 84 | + if (function_exists('wc_get_product')) { | |
| 85 | + $item_id = $order->add_product(wc_get_product($product_id), $item_row->qty); | |
| 86 | + } else { | |
| 87 | + $item_id = $order->add_product(get_product($product_id), $item_row->qty); | |
| 88 | + } | |
| 36 | 89 | } |
| 90 | + $order->calculate_totals(); | |
| 91 | + $free_shipping = 0; | |
| 92 | + foreach ($coupons as $coupon) { | |
| 93 | + $coupon = new WC_Coupon($coupon); | |
| 94 | + $amount = $coupon->get_amount(); | |
| 95 | + if ($coupon->is_type('percent')) { | |
| 96 | + $amount = $order->get_total() / 100 * $amount; | |
| 97 | + } | |
| 98 | + if (method_exists($order, 'add_item')) { | |
| 99 | + $item = new WC_Order_Item_Coupon(); | |
| 100 | + $item->set_props(array( | |
| 101 | + 'code' => $coupon->get_code(), | |
| 102 | + 'discount' => $amount, | |
| 103 | + 'discount_tax' => 0 | |
| 104 | + )); | |
| 105 | + $order->add_item($item); | |
| 106 | + } else { | |
| 107 | + //$order->add_code($coupon->get_code(), $amount); | |
| 108 | + } | |
| 109 | + if ((method_exists($coupon, 'get_free_shipping') && $coupon->get_free_shipping()) || $coupon->free_shipping) { $free_shipping = true; } | |
| 110 | + } | |
| 37 | 111 | } |
| 38 | 112 | $payment_method = new woocommerce_makecommerce(); |
| 39 | 113 | $order->set_payment_method($payment_method); |
| 40 | 114 | $billing_address = array( |
| @@ -42,8 +116,9 @@ | ||
| 42 | 116 | 'last_name' => $cart_info->customer->lastname, |
| 43 | 117 | 'email' => $cart_info->customer->email, |
| 44 | 118 | 'phone' => $cart_info->customer->phone, |
| 45 | 119 | ); |
| 120 | + | |
| 46 | 121 | $shipping_address = $billing_address; |
| 47 | 122 | $shipment_method = false; |
| 48 | 123 | if (!empty($cart_info->invoiceAddress)) { |
| 49 | 124 | $invoice = $cart_info->invoiceAddress; |
| @@ -105,54 +180,74 @@ | ||
| 105 | 180 | $order->set_address($billing_address, 'shipping'); |
| 106 | 181 | } else { |
| 107 | 182 | $order->set_address($shipment_address, 'shipping'); |
| 108 | 183 | } |
| 109 | - //$order->update_shipping($item_id, array('method_title' => 'Mingi transport nii', 'cost' => 1.99)); | |
| 110 | - $order->calculate_totals(); | |
| 111 | 184 | $order->remove_order_items('shipping'); |
| 112 | 185 | if ($shipment_method) { |
| 113 | 186 | $package = $tmp_cart->get_shipping_packages(); |
| 114 | 187 | if (!empty($package)) { $package = $package[0]; } |
| 115 | - $price_int = array_pop($shipment_method->get_rates_for_package($package)); | |
| 116 | - $price_int = !empty($price_int) ? $price_int->cost : 0; | |
| 188 | + $price_int = $shipment_method->get_rates_for_package($package); | |
| 189 | + $price_int = get_rate_without_taxes($shipment_method->id.':'.$shipment_method->instance_id, $price_int); | |
| 117 | 190 | $price = !empty($cart_info->shipmentMethod->amount) ? (double)$cart_info->shipmentMethod->amount : 0; |
| 118 | - if ($price_int != $price) { error_log('Price does not match'); exit; } | |
| 119 | - $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, array(), $shipment_method->id.':'.$shipment_method->instance_id); | |
| 120 | - $order->add_shipping($rate); | |
| 191 | + //if (round($price_int,2) != round($price,2)) { error_log('Price does not match ['.round($price,2).'] vs ['.round($price_int,2).']'); exit; } | |
| 192 | + if ($free_shipping) $price = $price_int = 0; | |
| 193 | + $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); | |
| 194 | + if (class_exists('WC_Order_Item_Shipping')) { | |
| 195 | + $item = new WC_Order_Item_Shipping(); | |
| 196 | + $item->set_order_id($order->get_id()); | |
| 197 | + $item->set_shipping_rate($rate); | |
| 198 | + $shipping_id = $item->save(); | |
| 199 | + } else { | |
| 200 | + $order->add_shipping($rate); | |
| 201 | + } | |
| 121 | 202 | if (!empty($shipment_method->type) && $shipment_method->type === 'apt') { |
| 122 | 203 | $machine = mk_get_machine(strtolower($shipment_method->carrier), $shipment->destinationId); |
| 123 | 204 | if ($machine) { |
| 124 | - update_post_meta($order->id, '_shipping_first_name', get_post_meta($order->id, '_billing_first_name', true)); | |
| 125 | - update_post_meta($order->id, '_shipping_last_name', get_post_meta($order->id, '_billing_last_name', true)); | |
| 126 | - update_post_meta($order->id, '_shipping_address_1', sanitize_text_field($machine['name'])); | |
| 127 | - update_post_meta($order->id, '_shipping_address_2', sanitize_text_field($machine['address'])); | |
| 128 | - update_post_meta($order->id, '_shipping_city', sanitize_text_field($machine['city'])); | |
| 129 | - update_post_meta($order->id, '_shipping_postcode', ''); | |
| 130 | - update_post_meta($order->id, '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId); | |
| 205 | + update_post_meta($order->get_id(), '_shipping_first_name', get_post_meta($order->get_id(), '_billing_first_name', true)); | |
| 206 | + update_post_meta($order->get_id(), '_shipping_last_name', get_post_meta($order->get_id(), '_billing_last_name', true)); | |
| 207 | + update_post_meta($order->get_id(), '_shipping_address_1', sanitize_text_field($machine['name'])); | |
| 208 | + update_post_meta($order->get_id(), '_shipping_address_2', sanitize_text_field($machine['address'])); | |
| 209 | + update_post_meta($order->get_id(), '_shipping_city', sanitize_text_field($machine['city'])); | |
| 210 | + update_post_meta($order->get_id(), '_shipping_postcode', ''); | |
| 211 | + update_post_meta($order->get_id(), '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId); | |
| 131 | 212 | } |
| 132 | 213 | } |
| 133 | - $order->calculate_totals(); | |
| 134 | 214 | } |
| 135 | - update_post_meta($order->id, '_makecommerce_sc_cart_id', $cart_info->cartId); | |
| 136 | - $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('status' => 1, 'order_id' => $order->id, 'modified' => time()), array('cart_id' => $cart_info->cartId)); | |
| 137 | - $locale = get_locale(); | |
| 138 | - if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); } | |
| 139 | - $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale; | |
| 215 | + update_post_meta($order->get_id(), '_makecommerce_sc_cart_id', $cart_info->cartId); | |
| 216 | + | |
| 217 | + $order->calculate_totals(); | |
| 218 | + if (!empty($tmp_order->content->discount)) { $order->set_discount_total($tmp_order->content->discount); } | |
| 219 | + if (!empty($tmp_order->content->discount_tax)) { $order->set_discount_tax($tmp_order->content->discount_tax); } | |
| 220 | + $order->set_total($order->get_total() - ($order->get_discount_total() + $order->get_discount_tax())); | |
| 221 | + $order->save(); | |
| 222 | + $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('status' => 1, 'order_id' => $order->get_id(), 'modified' => time()), array('cart_id' => $cart_info->cartId)); | |
| 223 | + | |
| 224 | + if (isset($_GET["lang1"])) { | |
| 225 | + $locale=$_GET["lang1"]; | |
| 226 | + } else { | |
| 227 | + $locale = get_locale(); | |
| 228 | + if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); } | |
| 229 | + $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale; | |
| 230 | + } | |
| 231 | + | |
| 232 | + //locale has to be 2 chars: | |
| 233 | + $locale = substr($locale, 0, 2); | |
| 234 | + | |
| 140 | 235 | $response = array( |
| 141 | 236 | 'cartId' => $cart_info->cartId, |
| 142 | - 'reference' => (string)$order->id, | |
| 237 | + 'reference' => (string)$order->get_id(), | |
| 143 | 238 | 'locale' => $locale, |
| 144 | 239 | 'transactionUrl' => array( |
| 145 | 240 | 'returnUrl' => array( |
| 146 | - 'url' => site_url('/?mc_cart_update=1'), | |
| 241 | + 'url' => site_url('/?mc_cart_update=1&lang1='.$locale), | |
| 147 | 242 | 'method' => 'POST' |
| 148 | 243 | ), |
| 149 | 244 | 'cancelUrl' => array( |
| 150 | - 'url' => site_url('/?mc_cart_update=2'), | |
| 245 | + 'url' => site_url('/?mc_cart_update=2&lang1='.$locale), | |
| 151 | 246 | 'method' => 'POST' |
| 152 | 247 | ), |
| 153 | 248 | 'notificationUrl' => array( |
| 154 | - 'url' => site_url('/?mc_cart_update=3'), | |
| 249 | + 'url' => site_url('/?mc_cart_update=3&lang1='.$locale), | |
| 155 | 250 | 'method' => 'POST' |
| 156 | 251 | ), |
| 157 | 252 | ) |
| 158 | 253 | ); |
| @@ -166,8 +261,12 @@ | ||
| 166 | 261 | echo json_encode(array('amount' => 2.99)); |
| 167 | 262 | } |
| 168 | 263 | if (intval(get_query_var('mc_cart_update')) > 0) { |
| 169 | 264 | $return_url = mk_check_payment(); |
| 265 | + if( isset($_GET["lang1"])) { | |
| 266 | + $return_url .= "&lang1=".$_GET["lang1"]; | |
| 267 | + do_action('wpml_switch_language', $_GET["lang1"]); | |
| 268 | + } | |
| 170 | 269 | if (intval(get_query_var('mc_cart_update')) === 3) { |
| 171 | 270 | echo json_encode(array('redirect' => $return_url)); |
| 172 | 271 | } else { |
| 173 | 272 | wp_redirect($return_url); |
| @@ -192,19 +291,20 @@ | ||
| 192 | 291 | add_action('woocommerce_before_cart', 'sc_cart_scripts'); |
| 193 | 292 | |
| 194 | 293 | function woocommerce_makecommerce_sc_init() { |
| 195 | 294 | |
| 196 | - load_plugin_textdomain('wc_makecommerce_sc_domain', false, dirname(plugin_basename(__FILE__)) . '/'); | |
| 295 | + load_plugin_textdomain('wc_makecommerce_domain', false, dirname(plugin_basename(__FILE__)) . '/languages/'); | |
| 197 | 296 | |
| 198 | 297 | class woocommerce_makecommerce_sc extends WC_Payment_Gateway { |
| 199 | 298 | function __construct() { |
| 200 | 299 | $this->id = 'makecommerce_sc'; |
| 201 | - $this->method_title = __('SimpleCheckout (MC)', 'wc_makecommerce_sc_domain'); | |
| 300 | + $this->method_title = __('SimpleCheckout (MC)', 'wc_makecommerce_domain'); | |
| 202 | 301 | $this->init(); |
| 203 | 302 | } |
| 204 | 303 | function init() { |
| 205 | 304 | $this->init_form_fields(); |
| 206 | 305 | $this->init_settings(); |
| 306 | + $this->enabled = $this->settings['active']; | |
| 207 | 307 | add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1); |
| 208 | 308 | add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options')); |
| 209 | 309 | add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options')); |
| 210 | 310 | } |
| @@ -211,8 +311,9 @@ | ||
| 211 | 311 | function before_cart() { |
| 212 | 312 | if (!$this->is_available()) { return; } |
| 213 | 313 | if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') { |
| 214 | 314 | echo '<style>.shipping,.order-total{display:none;}</style>'; |
| 315 | + echo '<style>.tax-rate{display:none;}</style>'; | |
| 215 | 316 | } |
| 216 | 317 | } |
| 217 | 318 | function install_tables() { |
| 218 | 319 | global $wpdb; |
| @@ -237,14 +338,31 @@ | ||
| 237 | 338 | return; |
| 238 | 339 | } |
| 239 | 340 | $this->install_tables(); |
| 240 | 341 | global $woocommerce, $wpdb; |
| 241 | - $data = array(); $qty = 0; $amount = 0; | |
| 342 | + if (get_option('woocommerce_enable_guest_checkout') !== 'yes' && !is_user_logged_in()) { | |
| 343 | + wc_add_notice(__('You have to log in to continue to checkout', 'wc_makecommerce_domain'), 'error'); | |
| 344 | + $redir_url = $woocommerce->cart->get_cart_url(); | |
| 345 | + wp_redirect($redir_url); | |
| 346 | + exit; | |
| 347 | + } | |
| 348 | + $data = array( | |
| 349 | + 'order' => array(), | |
| 350 | + 'coupons' => $woocommerce->cart->get_applied_coupons(), | |
| 351 | + 'discount' => $woocommerce->cart->get_cart_discount_total(), | |
| 352 | + 'discount_tax' => $woocommerce->cart->get_cart_discount_tax_total() | |
| 353 | + ); | |
| 354 | + $free_shipping = false; | |
| 355 | + foreach ($woocommerce->cart->get_applied_coupons() as $coupon) { | |
| 356 | + $coupon = new WC_Coupon($coupon); | |
| 357 | + if ((method_exists($coupon, 'get_free_shipping') && $coupon->get_free_shipping()) || $coupon->free_shipping) { $free_shipping = true; } | |
| 358 | + } | |
| 359 | + $qty = 0; $amount = 0; | |
| 242 | 360 | $cart = $woocommerce->cart->get_cart(); |
| 243 | 361 | foreach ($cart as $cart_item) { |
| 244 | - $data[] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity']); | |
| 362 | + $data['order'][] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity'], 'var' => $cart_item['variation_id']); | |
| 245 | 363 | $qty += $cart_item['quantity']; |
| 246 | - $amount += $cart_item['line_total']; | |
| 364 | + $amount += $cart_item['line_total'] + $cart_item['line_tax']; | |
| 247 | 365 | } |
| 248 | 366 | if (count($data) > 0) { |
| 249 | 367 | $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array( |
| 250 | 368 | 'created' => time(), |
| @@ -251,17 +369,37 @@ | ||
| 251 | 369 | 'modified' => time(), |
| 252 | 370 | 'status' => 0, |
| 253 | 371 | 'content' => json_encode($data) |
| 254 | 372 | )); |
| 255 | - $tmp_order_id = $wpdb->insert_id; | |
| 256 | - $cart_to_order_url = site_url('/?mc_cart_to_order=1'); | |
| 257 | - $calculate_shipment_url = site_url('/?mc_calculate_shipment=1'); | |
| 258 | - | |
| 259 | 373 | $locale = get_locale(); |
| 260 | 374 | if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); } |
| 261 | 375 | $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale; |
| 262 | - if (empty($locale)) { $locale = 'ee'; } | |
| 263 | - $tos_url = !empty($this->settings['shop_tos_url']) ? $this->settings['shop_tos_url'] : site_url(); | |
| 376 | + if (empty($locale)) { $locale = 'et'; } | |
| 377 | + | |
| 378 | + //polylang uses different locale names, while wpml uses lv, lt, en. Polylang uses lv_LV, lt_LT and en_GB | |
| 379 | + //this method should be improved and made generic everywhere | |
| 380 | + if (function_exists('pll_languages_list')) { | |
| 381 | + $locale = get_locale(); | |
| 382 | + } | |
| 383 | + | |
| 384 | + //locale has to be 2 chars: | |
| 385 | + $locale = substr($locale, 0, 2); | |
| 386 | + | |
| 387 | + $tmp_order_id = $wpdb->insert_id; | |
| 388 | + $cart_to_order_url = site_url('/?mc_cart_to_order=1&lang1='.$locale); | |
| 389 | + $calculate_shipment_url = site_url('/?mc_calculate_shipment=1&lang1='.$locale); | |
| 390 | + | |
| 391 | + //set correct tos url. | |
| 392 | + if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['shop_tos_url_'.$locale])) { | |
| 393 | + $tos_url = $this->settings['shop_tos_url_'.$locale]; | |
| 394 | + } else { | |
| 395 | + if (!empty($this->settings['shop_tos_url'])) { | |
| 396 | + $tos_url = $this->settings['shop_tos_url']; | |
| 397 | + } else { | |
| 398 | + $tos_url = site_url(); | |
| 399 | + } | |
| 400 | + } | |
| 401 | + | |
| 264 | 402 | $data = array( |
| 265 | 403 | 'cartRef' => 'PreOrder '.$tmp_order_id, |
| 266 | 404 | 'pluginUrls' => array( |
| 267 | 405 | 'cartToOrder' => $cart_to_order_url, |
| @@ -267,22 +405,30 @@ | ||
| 267 | 405 | 'cartToOrder' => $cart_to_order_url, |
| 268 | 406 | 'calculateShipment' => $calculate_shipment_url, |
| 269 | 407 | 'tos' => $tos_url |
| 270 | 408 | ), |
| 271 | - 'amount' => round($amount, 2), | |
| 409 | + 'amount' => sprintf("%.2f", round(max($amount, 0.01), 2)), | |
| 272 | 410 | 'currency' => 'EUR', |
| 273 | - 'sourceCountry' => 'EE', | |
| 411 | + 'sourceCountry' => WC()->countries->get_base_country(), // 'EE' | |
| 274 | 412 | 'locale' => $locale, |
| 275 | 413 | 'shipmentMethods' => array() |
| 276 | 414 | ); |
| 277 | 415 | $package = $woocommerce->cart->get_shipping_packages(); |
| 278 | 416 | if (!empty($package)) { $package = $package[0]; } |
| 279 | - $zones = WC_Shipping_Zones::get_zones(); | |
| 417 | + $zones = array(); | |
| 418 | + | |
| 419 | + $zone = new WC_Shipping_Zone(0); | |
| 420 | + $zones[ $zone->get_id() ] = $zone->get_data(); | |
| 421 | + $zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location(); | |
| 422 | + $zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods(); | |
| 423 | + $zones = array_merge( $zones, WC_Shipping_Zones::get_zones() ); | |
| 280 | 424 | $continents = WC()->countries->get_continents(); |
| 281 | 425 | $method_country_x = array(); |
| 282 | 426 | $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU'); |
| 283 | 427 | foreach ($zones as $zone) { |
| 284 | 428 | foreach ($zone['shipping_methods'] as $method_key => $method) { |
| 429 | + if ($method->enabled !== 'yes') { continue; } | |
| 430 | + $carrier = array('countries' => array()); | |
| 285 | 431 | if (!empty($supported_methods[$method->id])) { |
| 286 | 432 | foreach ($woocommerce->cart->get_shipping_packages() as $package) { |
| 287 | 433 | if (!$method->is_available($package)) { continue(2); } |
| 288 | 434 | } |
| @@ -287,36 +433,48 @@ | ||
| 287 | 433 | if (!$method->is_available($package)) { continue(2); } |
| 288 | 434 | } |
| 289 | 435 | $method_type = $supported_methods[$method->id]; |
| 290 | 436 | if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); } |
| 291 | - $carrier = array('countries' => array()); | |
| 292 | 437 | if (!empty($method->carrier)) { $carrier['carrier'] = mb_strtoupper($method->carrier); } |
| 293 | - foreach ($zone['zone_locations'] as $location) { | |
| 294 | - if ($location->type === 'continent' && !empty($continents[$location->code])) { | |
| 295 | - $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]); | |
| 296 | - $carrier['countries'] = array_merge($carrier['countries'], $countries); | |
| 297 | - } else if ($location->type === 'state') { | |
| 298 | - list($country, $state) = explode(':', $location->code); | |
| 299 | - $carrier['countries'][] = $country; | |
| 300 | - } else { | |
| 301 | - $carrier['countries'][] = $location->code; | |
| 438 | + if (empty($zone['zone_locations'])) { | |
| 439 | + $carrier['countries'] = array_keys(WC()->countries->get_allowed_countries()); | |
| 440 | + } else { | |
| 441 | + foreach ($zone['zone_locations'] as $location) { | |
| 442 | + if ($location->type === 'continent' && !empty($continents[$location->code])) { | |
| 443 | + $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]); | |
| 444 | + $carrier['countries'] = array_merge($carrier['countries'], $countries); | |
| 445 | + } else if ($location->type === 'state') { | |
| 446 | + list($country, $state) = explode(':', $location->code); | |
| 447 | + $carrier['countries'][] = $country; | |
| 448 | + } else if ($location->type === 'country') { | |
| 449 | + $carrier['countries'][] = $location->code; | |
| 450 | + } | |
| 302 | 451 | } |
| 303 | 452 | } |
| 304 | 453 | $method_country_x[$method_type] = array_merge($method_country_x[$method_type], $carrier['countries']); |
| 305 | - $price = $method->get_rates_for_package($package); | |
| 306 | - $price = !empty($price[$method->id.':'.$method_key]) ? $price[$method->id.':'.$method_key]->cost : 0; | |
| 454 | + $prices = $method->get_rates_for_package($package); | |
| 455 | + $price = $free_shipping ? 0.00 : get_rate_with_taxes($method->id.':'.$method_key, $prices); | |
| 307 | 456 | $carrier['type'] = strtoupper($method_type); |
| 308 | 457 | $carrier['name'] = $method->title; |
| 309 | 458 | $carrier['methodId'] = $method->id . ':' . $method_key; |
| 310 | - $carrier['amount'] = round($price, 2); | |
| 459 | + $carrier['amount'] = sprintf("%.2f", round($price, 2)); | |
| 311 | 460 | $data['shipmentMethods'][] = $carrier; |
| 312 | 461 | } |
| 313 | 462 | } |
| 314 | 463 | } |
| 315 | - | |
| 316 | 464 | $MK = mk_get_api(); |
| 317 | 465 | $cart = $MK->createCart($data); |
| 318 | 466 | $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id)); |
| 467 | + | |
| 468 | + //add wp meta of the data provided to SCO. This way we can later check if the data values and so on were actually provided by us or the data has been manipulated by a mitm | |
| 469 | + $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array( | |
| 470 | + 'cart_id' => $cart->id."_provided_sco_data", | |
| 471 | + 'created' => time(), | |
| 472 | + 'modified' => time(), | |
| 473 | + 'status' => 0, | |
| 474 | + 'content' => json_encode($data) | |
| 475 | + )); | |
| 476 | + | |
| 319 | 477 | wp_redirect($cart->scoUrl); |
| 320 | 478 | exit; |
| 321 | 479 | } |
| 322 | 480 | die('no content'); |
| @@ -331,15 +489,39 @@ | ||
| 331 | 489 | $this->form_fields['active'] = array( |
| 332 | 490 | 'title' => __('Enable/Disable', 'wc_makecommerce_domain'), |
| 333 | 491 | 'type' => 'checkbox', |
| 334 | 492 | 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'), |
| 335 | - 'default' => 'no' | |
| 493 | + 'default' => 'yes' | |
| 336 | 494 | ); |
| 337 | - $this->form_fields['shop_tos_url'] = array( | |
| 338 | - 'title' => __('Shop ToS url', 'wc_makecommerce_domain'), | |
| 339 | - 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'), | |
| 340 | - 'type' => 'text', | |
| 341 | - ); | |
| 495 | + | |
| 496 | + $languages = array(); | |
| 497 | + if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) { | |
| 498 | + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0'); | |
| 499 | + } else if (function_exists('pll_languages_list')) { | |
| 500 | + $pl_locales = pll_languages_list(array('fields'=>'locale')); | |
| 501 | + foreach ($pl_locales as $key => $locale_pl ) { | |
| 502 | + $language = array('id' => $key, 'code' => $locale_pl) ; | |
| 503 | + $languages[$locale_pl] = $language; | |
| 504 | + } | |
| 505 | + } | |
| 506 | + | |
| 507 | + if (empty($languages)) { | |
| 508 | + $this->form_fields['shop_tos_url'] = array( | |
| 509 | + 'title' => __('Shop ToS url', 'wc_makecommerce_domain'), | |
| 510 | + 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'), | |
| 511 | + 'type' => 'text', | |
| 512 | + ); | |
| 513 | + } else { | |
| 514 | + foreach ($languages as $language_code=>$language) { | |
| 515 | + $shortLanguageCode = substr($language_code, 0, 2); | |
| 516 | + $this->form_fields['shop_tos_url_'.$shortLanguageCode] = array( | |
| 517 | + 'title' => __('Shop ToS url', 'wc_makecommerce_domain').sprintf(' (%s)', $shortLanguageCode), | |
| 518 | + 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'), | |
| 519 | + 'type' => 'text', | |
| 520 | + ); | |
| 521 | + } | |
| 522 | + } | |
| 523 | + | |
| 342 | 524 | $this->form_fields['hide_shipping_methods'] = array( |
| 343 | 525 | 'title' => __('Hide shipping methods block', 'wc_makecommerce_domain'), |
| 344 | 526 | 'type' => 'checkbox', |
| 345 | 527 | 'label' => __('Hide shipping methods block on cart page. This will make it look more clean and simple', 'wc_makecommerce_domain'), |
| @@ -346,9 +528,9 @@ | ||
| 346 | 528 | 'default' => 'no' |
| 347 | 529 | ); |
| 348 | 530 | } |
| 349 | 531 | public function is_available() { |
| 350 | - if ($this->settings['active'] == "yes") { | |
| 532 | + if ($this->settings['active'] == "yes" || empty($this->settings['active'])) { | |
| 351 | 533 | return true; |
| 352 | 534 | } |
| 353 | 535 | return false; |
| 354 | 536 | } |
| @@ -354,8 +536,26 @@ | ||
| 354 | 536 | } |
| 355 | 537 | |
| 356 | 538 | } |
| 357 | 539 | |
| 540 | +} | |
| 541 | + | |
| 542 | +function get_rate_without_taxes($method, $rate) { | |
| 543 | + if (empty($rate[$method])) { return 0; } | |
| 544 | + $rate = $rate[$method]; | |
| 545 | + $price = $rate->cost; | |
| 546 | + return $price; | |
| 547 | +} | |
| 548 | + | |
| 549 | +function get_rate_with_taxes($method, $rate) { | |
| 550 | + if (empty($rate[$method])) { return 0; } | |
| 551 | + $rate = $rate[$method]; | |
| 552 | + $price = $rate->cost; | |
| 553 | + if (empty($rate->taxes)) { return $price; } | |
| 554 | + foreach ($rate->taxes as $tax) { | |
| 555 | + $price += $tax; | |
| 556 | + } | |
| 557 | + return $price; | |
| 358 | 558 | } |
| 359 | 559 | |
| 360 | 560 | function woocommerce_payment_makecommerce_sc_add($methods) { |
| 361 | 561 | $methods[] = 'woocommerce_makecommerce_sc'; |