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

368 lines 16.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 foreach (json_decode($tmp_order->content) as $item_row) {
28 $tmp_cart->add_to_cart($item_row->id, $item_row->qty);
29 }
30 if ($tmp_order->order_id) {
31 $order = new WC_Order($tmp_order->order_id);
32 } else {
33 $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);
36 }
37 }
38 $payment_method = new woocommerce_makecommerce();
39 $order->set_payment_method($payment_method);
40 $billing_address = array(
41 'first_name' => $cart_info->customer->firstname,
42 'last_name' => $cart_info->customer->lastname,
43 'email' => $cart_info->customer->email,
44 'phone' => $cart_info->customer->phone,
45 );
46 $shipping_address = $billing_address;
47 $shipment_method = false;
48 if (!empty($cart_info->invoiceAddress)) {
49 $invoice = $cart_info->invoiceAddress;
50 $billing_address['company'] = '';
51 if (!empty($invoice->firstname)) { $billing_address['first_name'] = $invoice->firstname; }
52 if (!empty($invoice->lastname)) { $billing_address['last_name'] = $invoice->lastname; }
53 if (!empty($invoice->country)) { $billing_address['country'] = $invoice->country; }
54 if (!empty($invoice->county)) { $billing_address['state'] = $invoice->county; }
55 if (!empty($invoice->city)) { $billing_address['city'] = $invoice->city; }
56 if (!empty($invoice->street1)) { $billing_address['address_1'] = $invoice->street1; }
57 if (!empty($invoice->street2)) { $billing_address['address_2'] = $invoice->street2; }
58 if (!empty($invoice->postalCode)) { $billing_address['postcode'] = $invoice->postalCode; }
59 if (!empty($invoice->legalName)) { $billing_address['company'] .= $invoice->legalName; }
60 if (!empty($invoice->registryCode)) { $billing_address['company'] .= ' ' . $invoice->registryCode; }
61 if (!empty($invoice->vatNum)) { $billing_address['company'] .= ' ' . $invoice->vatNum; }
62 }
63 if (!empty($cart_info->shipmentAddress)) {
64 $shipment = $cart_info->shipmentAddress;
65 if (!empty($shipment->country)) { $shipment_address['country'] = $shipment->country; }
66 if (!empty($shipment->county)) { $shipment_address['state'] = $shipment->county; }
67 if (!empty($shipment->city)) { $shipment_address['city'] = $shipment->city; }
68 if (!empty($shipment->street1)) { $shipment_address['address_1'] = $shipment->street1; }
69 if (!empty($shipment->street2)) { $shipment_address['address_2'] = $shipment->street2; }
70 if (!empty($shipment->postalCode)) { $shipment_address['postcode'] = $shipment->postalCode; }
71 if (!empty($shipment->firstname)) { $shipment_address['first_name'] = $shipment->firstname; }
72 if (!empty($shipment->lastname)) { $shipment_address['last_name'] = $shipment->lastname; }
73 }
74 if (!empty($cart_info->shipmentMethod)) {
75 $shipment_details = $cart_info->shipmentMethod;
76 $zones = WC_Shipping_Zones::get_zones();
77 $continents = WC()->countries->get_continents();
78 $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU');
79 foreach ($zones as $zone) {
80 if ($shipment_method) { continue; }
81 foreach ($zone['shipping_methods'] as $method) {
82 if ($shipment_method) { continue; }
83 if (empty($supported_methods[$method->id])) { continue; }
84 if ($supported_methods[$method->id] === $shipment_details->type && (empty($shipment_details->carrier) || strtoupper($method->carrier) === $shipment_details->carrier)) {
85 foreach ($zone['zone_locations'] as $location) {
86 if ($location->type === 'continent' && !empty($continents[$location->code])) {
87 if (in_array($shipment->country, $continents[$location->code]['countries'])) {
88 $shipment_method = $method;
89 }
90 } else if ($location->type === 'state') {
91 list($country, $state) = explode(':', $location->code);
92 if ($country === $shipment->country) {
93 $shipment_method = $method;
94 }
95 } else if ($shipment->country === $location->code) {
96 $shipment_method = $method;
97 }
98 }
99 }
100 }
101 }
102 }
103 $order->set_address($billing_address, 'billing');
104 if (empty($shipment_address)) {
105 $order->set_address($billing_address, 'shipping');
106 } else {
107 $order->set_address($shipment_address, 'shipping');
108 }
109 //$order->update_shipping($item_id, array('method_title' => 'Mingi transport nii', 'cost' => 1.99));
110 $order->calculate_totals();
111 $order->remove_order_items('shipping');
112 if ($shipment_method) {
113 $package = $tmp_cart->get_shipping_packages();
114 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;
117 $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);
121 if (!empty($shipment_method->type) && $shipment_method->type === 'apt') {
122 $machine = mk_get_machine(strtolower($shipment_method->carrier), $shipment->destinationId);
123 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);
131 }
132 }
133 $order->calculate_totals();
134 }
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;
140 $response = array(
141 'cartId' => $cart_info->cartId,
142 'reference' => (string)$order->id,
143 'locale' => $locale,
144 'transactionUrl' => array(
145 'returnUrl' => array(
146 'url' => site_url('/?mc_cart_update=1'),
147 'method' => 'POST'
148 ),
149 'cancelUrl' => array(
150 'url' => site_url('/?mc_cart_update=2'),
151 'method' => 'POST'
152 ),
153 'notificationUrl' => array(
154 'url' => site_url('/?mc_cart_update=3'),
155 'method' => 'POST'
156 ),
157 )
158 );
159 header('Content-type: application/json');
160 echo json_encode($response);
161 exit;
162 }
163 if (intval(get_query_var('mc_calculate_shipment')) === 1) {
164 $cart_info = json_decode(file_get_contents('php://input'));
165 header('Content-type: application/json');
166 echo json_encode(array('amount' => 2.99));
167 }
168 if (intval(get_query_var('mc_cart_update')) > 0) {
169 $return_url = mk_check_payment();
170 if (intval(get_query_var('mc_cart_update')) === 3) {
171 echo json_encode(array('redirect' => $return_url));
172 } else {
173 wp_redirect($return_url);
174 }
175 exit;
176 }
177 }
178 function sc_cart_scripts() {
179 ?>
180 <script>
181 jQuery(document).on('updated_cart_totals', function(){
182 var rand = Math.round(Math.random() * 100000);
183 window.history.pushState({rand: rand}, "Rand "+rand, "./?r="+rand);
184 });
185 </script>
186 <?php
187 $mcsc = new woocommerce_makecommerce_sc();
188 $mcsc->before_cart();
189 }
190 add_filter('query_vars', 'sc_return_trigger');
191 add_action('template_redirect', 'sc_return_trigger_check');
192 add_action('woocommerce_before_cart', 'sc_cart_scripts');
193
194 function woocommerce_makecommerce_sc_init() {
195
196 load_plugin_textdomain('wc_makecommerce_sc_domain', false, dirname(plugin_basename(__FILE__)) . '/');
197
198 class woocommerce_makecommerce_sc extends WC_Payment_Gateway {
199 function __construct() {
200 $this->id = 'makecommerce_sc';
201 $this->method_title = __('SimpleCheckout (MC)', 'wc_makecommerce_sc_domain');
202 $this->init();
203 }
204 function init() {
205 $this->init_form_fields();
206 $this->init_settings();
207 add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1);
208 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
209 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
210 }
211 function before_cart() {
212 if (!$this->is_available()) { return; }
213 if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') {
214 echo '<style>.shipping,.order-total{display:none;}</style>';
215 }
216 }
217 function install_tables() {
218 global $wpdb;
219 $charset_collate = $wpdb->get_charset_collate();
220 $table_name = $wpdb->prefix . "woocommerce_makecommerce_sc";
221 $sql = "CREATE TABLE `$table_name` (
222 `id` int(11) unsigned not null auto_increment,
223 `cart_id` varchar(64) not null default '',
224 `order_id` int(10) unsigned not null,
225 `created` int(10) unsigned not null,
226 `modified` int(10) unsigned not null,
227 `status` tinyint unsigned not null,
228 `content` mediumtext,
229 unique key id (`id`),
230 key cart_id (`cart_id`)
231 ) $charset_collate;";
232 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
233 dbDelta($sql);
234 }
235 function take_over_checkout($checkout) {
236 if (!$this->is_available()) {
237 return;
238 }
239 $this->install_tables();
240 global $woocommerce, $wpdb;
241 $data = array(); $qty = 0; $amount = 0;
242 $cart = $woocommerce->cart->get_cart();
243 foreach ($cart as $cart_item) {
244 $data[] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity']);
245 $qty += $cart_item['quantity'];
246 $amount += $cart_item['line_total'];
247 }
248 if (count($data) > 0) {
249 $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array(
250 'created' => time(),
251 'modified' => time(),
252 'status' => 0,
253 'content' => json_encode($data)
254 ));
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 $locale = get_locale();
260 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
261 $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();
264 $data = array(
265 'cartRef' => 'PreOrder '.$tmp_order_id,
266 'pluginUrls' => array(
267 'cartToOrder' => $cart_to_order_url,
268 'calculateShipment' => $calculate_shipment_url,
269 'tos' => $tos_url
270 ),
271 'amount' => round($amount, 2),
272 'currency' => 'EUR',
273 'sourceCountry' => 'EE',
274 'locale' => $locale,
275 'shipmentMethods' => array()
276 );
277 $package = $woocommerce->cart->get_shipping_packages();
278 if (!empty($package)) { $package = $package[0]; }
279 $zones = WC_Shipping_Zones::get_zones();
280 $continents = WC()->countries->get_continents();
281 $method_country_x = array();
282 $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 foreach ($zones as $zone) {
284 foreach ($zone['shipping_methods'] as $method_key => $method) {
285 if (!empty($supported_methods[$method->id])) {
286 foreach ($woocommerce->cart->get_shipping_packages() as $package) {
287 if (!$method->is_available($package)) { continue(2); }
288 }
289 $method_type = $supported_methods[$method->id];
290 if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); }
291 $carrier = array('countries' => array());
292 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;
302 }
303 }
304 $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;
307 $carrier['type'] = strtoupper($method_type);
308 $carrier['name'] = $method->title;
309 $carrier['methodId'] = $method->id . ':' . $method_key;
310 $carrier['amount'] = round($price, 2);
311 $data['shipmentMethods'][] = $carrier;
312 }
313 }
314 }
315
316 $MK = mk_get_api();
317 $cart = $MK->createCart($data);
318 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id));
319 wp_redirect($cart->scoUrl);
320 exit;
321 }
322 die('no content');
323 }
324 function init_form_fields() {
325 $this->form_fields = array();
326 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
327 $this->form_fields['scointro'] = array(
328 'type' => 'title',
329 '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'),
330 );
331 $this->form_fields['active'] = array(
332 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
333 'type' => 'checkbox',
334 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'),
335 'default' => 'no'
336 );
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 );
342 $this->form_fields['hide_shipping_methods'] = array(
343 'title' => __('Hide shipping methods block', 'wc_makecommerce_domain'),
344 'type' => 'checkbox',
345 'label' => __('Hide shipping methods block on cart page. This will make it look more clean and simple', 'wc_makecommerce_domain'),
346 'default' => 'no'
347 );
348 }
349 public function is_available() {
350 if ($this->settings['active'] == "yes") {
351 return true;
352 }
353 return false;
354 }
355
356 }
357
358 }
359
360 function woocommerce_payment_makecommerce_sc_add($methods) {
361 $methods[] = 'woocommerce_makecommerce_sc';
362 return $methods;
363 }
364 add_action('plugins_loaded', 'woocommerce_makecommerce_sc_init');
365 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_sc_add');
366
367
368