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

364 lines 16.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 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 $order->set_address($shipment_address, 'shipping');
105 //$order->update_shipping($item_id, array('method_title' => 'Mingi transport nii', 'cost' => 1.99));
106 $order->calculate_totals();
107 $order->remove_order_items('shipping');
108 if ($shipment_method) {
109 $package = $tmp_cart->get_shipping_packages();
110 if (!empty($package)) { $package = $package[0]; }
111 $price_int = array_pop($shipment_method->get_rates_for_package($package));
112 $price_int = !empty($price_int) ? $price_int->cost : 0;
113 $price = !empty($cart_info->shipmentMethod->amount) ? (double)$cart_info->shipmentMethod->amount : 0;
114 if ($price_int != $price) { error_log('Price does not match'); exit; }
115 $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);
116 $order->add_shipping($rate);
117 if (!empty($shipment_method->type) && $shipment_method->type === 'apt') {
118 $machine = mk_get_machine(strtolower($shipment_method->carrier), $shipment->destinationId);
119 if ($machine) {
120 update_post_meta($order->id, '_shipping_first_name', get_post_meta($order->id, '_billing_first_name', true));
121 update_post_meta($order->id, '_shipping_last_name', get_post_meta($order->id, '_billing_last_name', true));
122 update_post_meta($order->id, '_shipping_address_1', sanitize_text_field($machine['name']));
123 update_post_meta($order->id, '_shipping_address_2', sanitize_text_field($machine['address']));
124 update_post_meta($order->id, '_shipping_city', sanitize_text_field($machine['city']));
125 update_post_meta($order->id, '_shipping_postcode', '');
126 update_post_meta($order->id, '_parcel_machine', strtolower($shipment_method->carrier).'||'.$shipment->destinationId);
127 }
128 }
129 $order->calculate_totals();
130 }
131 update_post_meta($order->id, '_makecommerce_sc_cart_id', $cart_info->cartId);
132 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('status' => 1, 'order_id' => $order->id, 'modified' => time()), array('cart_id' => $cart_info->cartId));
133 $locale = get_locale();
134 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
135 $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale;
136 $response = array(
137 'cartId' => $cart_info->cartId,
138 'reference' => (string)$order->id,
139 'locale' => $locale,
140 'transactionUrl' => array(
141 'returnUrl' => array(
142 'url' => site_url('/?mc_cart_update=1'),
143 'method' => 'POST'
144 ),
145 'cancelUrl' => array(
146 'url' => site_url('/?mc_cart_update=2'),
147 'method' => 'POST'
148 ),
149 'notificationUrl' => array(
150 'url' => site_url('/?mc_cart_update=3'),
151 'method' => 'POST'
152 ),
153 )
154 );
155 header('Content-type: application/json');
156 echo json_encode($response);
157 exit;
158 }
159 if (intval(get_query_var('mc_calculate_shipment')) === 1) {
160 $cart_info = json_decode(file_get_contents('php://input'));
161 header('Content-type: application/json');
162 echo json_encode(array('amount' => 2.99));
163 }
164 if (intval(get_query_var('mc_cart_update')) > 0) {
165 $return_url = mk_check_payment();
166 if (intval(get_query_var('mc_cart_update')) === 3) {
167 echo json_encode(array('redirect' => $return_url));
168 } else {
169 wp_redirect($return_url);
170 }
171 exit;
172 }
173 }
174 function sc_cart_scripts() {
175 ?>
176 <script>
177 jQuery(document).on('updated_cart_totals', function(){
178 var rand = Math.round(Math.random() * 100000);
179 window.history.pushState({rand: rand}, "Rand "+rand, "./?r="+rand);
180 });
181 </script>
182 <?php
183 $mcsc = new woocommerce_makecommerce_sc();
184 $mcsc->before_cart();
185 }
186 add_filter('query_vars', 'sc_return_trigger');
187 add_action('template_redirect', 'sc_return_trigger_check');
188 add_action('woocommerce_before_cart', 'sc_cart_scripts');
189
190 function woocommerce_makecommerce_sc_init() {
191
192 load_plugin_textdomain('wc_makecommerce_sc_domain', false, dirname(plugin_basename(__FILE__)) . '/');
193
194 class woocommerce_makecommerce_sc extends WC_Payment_Gateway {
195 function __construct() {
196 $this->id = 'makecommerce_sc';
197 $this->method_title = __('SimpleCheckout (MC)', 'wc_makecommerce_sc_domain');
198 $this->init();
199 }
200 function init() {
201 $this->init_form_fields();
202 $this->init_settings();
203 add_action('woocommerce_before_checkout_form', array(&$this, 'take_over_checkout'), 10, 1);
204 add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
205 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
206 }
207 function before_cart() {
208 if (!$this->is_available()) { return; }
209 if (!empty($this->settings['hide_shipping_methods']) && $this->settings['hide_shipping_methods'] === 'yes') {
210 echo '<style>.shipping,.order-total{display:none;}</style>';
211 }
212 }
213 function install_tables() {
214 global $wpdb;
215 $charset_collate = $wpdb->get_charset_collate();
216 $table_name = $wpdb->prefix . "woocommerce_makecommerce_sc";
217 $sql = "CREATE TABLE `$table_name` (
218 `id` int(11) unsigned not null auto_increment,
219 `cart_id` varchar(64) not null default '',
220 `order_id` int(10) unsigned not null,
221 `created` int(10) unsigned not null,
222 `modified` int(10) unsigned not null,
223 `status` tinyint unsigned not null,
224 `content` mediumtext,
225 unique key id (`id`),
226 key cart_id (`cart_id`)
227 ) $charset_collate;";
228 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
229 dbDelta($sql);
230 }
231 function take_over_checkout($checkout) {
232 if (!$this->is_available()) {
233 return;
234 }
235 $this->install_tables();
236 global $woocommerce, $wpdb;
237 $data = array(); $qty = 0; $amount = 0;
238 $cart = $woocommerce->cart->get_cart();
239 foreach ($cart as $cart_item) {
240 $data[] = array('id' => $cart_item['product_id'], 'qty' => $cart_item['quantity']);
241 $qty += $cart_item['quantity'];
242 $amount += $cart_item['line_total'];
243 }
244 if (count($data) > 0) {
245 $wpdb->insert($wpdb->prefix.'woocommerce_makecommerce_sc', array(
246 'created' => time(),
247 'modified' => time(),
248 'status' => 0,
249 'content' => json_encode($data)
250 ));
251 $tmp_order_id = $wpdb->insert_id;
252 $cart_to_order_url = site_url('/?mc_cart_to_order=1');
253 $calculate_shipment_url = site_url('/?mc_calculate_shipment=1');
254
255 $locale = get_locale();
256 if ($locale) { $locale = explode('_', $locale); $locale = !empty($locale[1]) ? strtolower($locale[1]) : strtolower($locale[0]); }
257 $locale = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : $locale;
258 if (empty($locale)) { $locale = 'ee'; }
259 $tos_url = !empty($this->settings['shop_tos_url']) ? $this->settings['shop_tos_url'] : site_url();
260 $data = array(
261 'cartRef' => 'PreOrder '.$tmp_order_id,
262 'pluginUrls' => array(
263 'cartToOrder' => $cart_to_order_url,
264 'calculateShipment' => $calculate_shipment_url,
265 'tos' => $tos_url
266 ),
267 'amount' => round($amount, 2),
268 'currency' => 'EUR',
269 'sourceCountry' => 'EE',
270 'locale' => $locale,
271 'shipmentMethods' => array()
272 );
273 $package = $woocommerce->cart->get_shipping_packages();
274 if (!empty($package)) { $package = $package[0]; }
275 $zones = WC_Shipping_Zones::get_zones();
276 $continents = WC()->countries->get_continents();
277 $method_country_x = array();
278 $supported_methods = array('parcelmachine_omniva' => 'APT', 'parcelmachine_smartpost' => 'APT', 'parcelmachine_dpd' => 'APT', 'courier_omniva' => 'COU', 'courier_smartpost' => 'COU', 'local_pickup' => 'OTH', 'flat_rate' => 'COU');
279 foreach ($zones as $zone) {
280 foreach ($zone['shipping_methods'] as $method_key => $method) {
281 if (!empty($supported_methods[$method->id])) {
282 foreach ($woocommerce->cart->get_shipping_packages() as $package) {
283 if (!$method->is_available($package)) { continue(2); }
284 }
285 $method_type = $supported_methods[$method->id];
286 if (empty($method_country_x[$method_type])) { $method_country_x[$method_type] = array(); }
287 $carrier = array('countries' => array());
288 if (!empty($method->carrier)) { $carrier['carrier'] = mb_strtoupper($method->carrier); }
289 foreach ($zone['zone_locations'] as $location) {
290 if ($location->type === 'continent' && !empty($continents[$location->code])) {
291 $countries = array_diff($continents[$location->code]['countries'], $method_country_x[$method_type]);
292 $carrier['countries'] = array_merge($carrier['countries'], $countries);
293 } else if ($location->type === 'state') {
294 list($country, $state) = explode(':', $location->code);
295 $carrier['countries'][] = $country;
296 } else {
297 $carrier['countries'][] = $location->code;
298 }
299 }
300 $method_country_x[$method_type] = array_merge($method_country_x[$method_type], $carrier['countries']);
301 $price = $method->get_rates_for_package($package);
302 $price = !empty($price[$method->id.':'.$method_key]) ? $price[$method->id.':'.$method_key]->cost : 0;
303 $carrier['type'] = strtoupper($method_type);
304 $carrier['name'] = $method->title;
305 $carrier['methodId'] = $method->id . ':' . $method_key;
306 $carrier['amount'] = round($price, 2);
307 $data['shipmentMethods'][] = $carrier;
308 }
309 }
310 }
311
312 $MK = mk_get_api();
313 $cart = $MK->createCart($data);
314 $wpdb->update($wpdb->prefix.'woocommerce_makecommerce_sc', array('cart_id' => $cart->id), array('id' => $tmp_order_id));
315 wp_redirect($cart->scoUrl);
316 exit;
317 }
318 die('no content');
319 }
320 function init_form_fields() {
321 $this->form_fields = array();
322 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
323 $this->form_fields['scointro'] = array(
324 'type' => 'title',
325 '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'),
326 );
327 $this->form_fields['active'] = array(
328 'title' => __('Enable/Disable', 'wc_makecommerce_domain'),
329 'type' => 'checkbox',
330 'label' => __('Enable SimpleCheckout', 'wc_makecommerce_domain'),
331 'default' => 'no'
332 );
333 $this->form_fields['shop_tos_url'] = array(
334 'title' => __('Shop ToS url', 'wc_makecommerce_domain'),
335 'description' => __('paste here url of your shop "Terms and Conditions" page', 'wc_makecommerce_domain'),
336 'type' => 'text',
337 );
338 $this->form_fields['hide_shipping_methods'] = array(
339 'title' => __('Hide shipping methods block', 'wc_makecommerce_domain'),
340 'type' => 'checkbox',
341 'label' => __('Hide shipping methods block on cart page. This will make it look more clean and simple', 'wc_makecommerce_domain'),
342 'default' => 'no'
343 );
344 }
345 public function is_available() {
346 if ($this->settings['active'] == "yes") {
347 return true;
348 }
349 return false;
350 }
351
352 }
353
354 }
355
356 function woocommerce_payment_makecommerce_sc_add($methods) {
357 $methods[] = 'woocommerce_makecommerce_sc';
358 return $methods;
359 }
360 add_action('plugins_loaded', 'woocommerce_makecommerce_sc_init');
361 add_action('woocommerce_payment_gateways', 'woocommerce_payment_makecommerce_sc_add');
362
363
364