%2$s
', esc_attr( $class ), esc_html( $message ) ); }); } global $mkDbTable; $mkDbTable = 'mc_banklinks'; function mc_install() { global $wpdb; global $mkDbTable; $tableName = $wpdb->prefix . $mkDbTable; $charset = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $tableName ( id mediumint(9) NOT NULL AUTO_INCREMENT, type varchar(10) NOT NULL, country char(2) NOT NULL, name varchar(25) NOT NULL, url varchar(250) NOT NULL, logo_url varchar(250), min_amount mediumint(9), max_amount mediumint(9), UNIQUE KEY id (id) ) $charset;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } register_activation_hook( __FILE__, 'mc_install' ); // On uninstall remove scheduled hook function mc_uninstall() { if (wp_next_scheduled ( 'mc_banklinks_update_cron' )) { wp_clear_scheduled_hook('mc_banklinks_update_cron'); } } register_deactivation_hook(__FILE__, 'mc_uninstall'); // WP schedule callable action add_action('mc_banklinks_update_cron', 'update_mc_banklinks'); // Assign schedule if not set, update compatible no need to reactivate, format('U') php 5.2 compatible // Assigns task at 23.59.59 by local time, task runs if time has passed and somebody navigates the site if (! wp_next_scheduled ( 'mc_banklinks_update_cron' )) { $date = new DateTime(); $date->setTime(rand(0,23),rand(0,59),00); $timestamp = intval($date->format('U')); //$local = 3600 * intval(get_option('gmt_offset')); //wp_schedule_event($timestamp - $local, 'twicedaily', 'mc_banklinks_update_cron'); wp_schedule_event($timestamp, 'twicedaily', 'mc_banklinks_update_cron'); } if (!class_exists('wc_makecommerce_domain')) { require_once('includes/vendor/autoload.php'); require_once('includes/Maksekeskus.php'); } function mk_check_payment() { global $woocommerce; $api = mk_get_api(); //set the correct language if (isset($_GET["lang1"])) { //set polylang language if (function_exists('pll_current_language')) { //get the correct locale name from polylang $polylangLangs = pll_the_languages(array('raw' => 1)); if (isset($polylangLangs[$_GET["lang1"]])) { $locale = str_replace("-", "_", $polylangLangs[$_GET["lang1"]]["locale"]); } //first change WP language, this triggers some polylang hooks switch_to_locale($locale); //second, change WPML language?!?! this ensures the correct landingpage (works with polylang) do_action('wpml_switch_language', $_GET["lang1"]); } else { if (function_exists('icl_object_id')) { //weirdly enough icl_object ID exists in this point of the request. It doesn't in other locations... //set wpml language do_action('wpml_switch_language', $_GET["lang1"]); } } } $returnUrl = home_url(); $request = stripslashes_deep($_POST); if ($api->verifyMac($request)) { $data = $api->extractRequestData($request); $reference = false; if (!empty($data['error']) ) { wc_add_notice(__('Payment failed', 'wc_makecommerce_domain'), 'error'); return $returnUrl; } if (!empty($data['message_type']) ) { if ($data['message_type'] == 'payment_return') { $transactionId = $data['transaction']; $reference = $data['reference']; $paymentStatus = $data['status']; } if ($data['message_type'] == 'token_return') { if (!empty($data['transaction']['reference'])) { $reference = $data['transaction']['reference']; } $paymentStatus = $data['transaction']['status']; $transactionId = $data['transaction']['id']; // for API backward compatibility - if we didn't find reference in the mesage, or the transaction state vas still PENDING // we'll fetch the transaction data from API, and make a payment request if needed // this all can be removed after 2017-jan if (!$reference or $paymentStatus == 'PENDING') { $transaction = $api->getTransaction($transactionId); if (!empty($transaction->reference)) { $reference = $transaction->reference; } $paymentStatus = $transaction->status; if ($paymentStatus == 'PENDING') { $paymentRequest = array( 'amount' => (string)sprintf("%.2f", $transaction->amount), 'currency' => $transaction->currency, 'token' => $data['token']['id'] ); $payment = $api->createPayment($transactionId, $paymentRequest); // TODO: validate $paymentStatus = $payment->transaction->status; } } } } } // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved if (!$reference) { return $returnUrl; } $order = new WC_Order($reference); // if we din't find the Order, we send user to shop landing-page. TODO: could be improved if (!$order->get_id()) { return $returnUrl; } $returnUrl = $order->get_checkout_order_received_url(); if ($paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED') { //get transaction to update the payment method. $transaction = $api->getTransaction($transactionId); //make sure the payment method is the same for simplecheckout and normal version. $paymentMethod = ""; if (isset($transaction->type) && isset($transaction->method)) { if ($transaction->type == "card") { $paymentMethod = "card_".$transaction->method; } else { if (isset($transaction->country)) { $paymentMethod = $transaction->country."_".$transaction->method; } } } //update _makecommerce_preselected_method update_post_meta($order->get_id(), '_makecommerce_preselected_method', $paymentMethod); } switch($paymentStatus) { case 'CANCELLED': $order->update_status( 'cancelled' ); wc_add_notice(__('Payment transaction cancelled', 'wc_makecommerce_domain'), 'error'); $returnUrl = wc_get_cart_url(); break; case 'COMPLETED': $orderNote = array(); $orderNote[] = __('Transaction ID', 'wc_makecommerce_domain') . ': '.$transactionId.''; $orderNote[] = __('Payment option', 'wc_makecommerce_domain') . ': ' . get_post_meta($order->get_id(), '_makecommerce_preselected_method', true); $order->add_order_note(implode("\r\n", $orderNote)); if (!empty($data['token']) && !empty($data['token']['multiuse'])) { update_post_meta($order->get_id(), '_makecommerce_payment_token', $data['token']['id']); update_post_meta($order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until']); } if (isset($_GET['lang1'])) { $order->update_meta_data('wpml_language', $_GET["lang1"]); } $order->payment_complete( $transactionId ); $woocommerce->cart->empty_cart(); break; } return $returnUrl; } $MKAPI = false; function mk_get_api() { global $MKAPI; if ($MKAPI) { //return $MKAPI; } $mk_api_type = get_option('mk_api_type', false); if (!$mk_api_type) { return false; } if ($mk_api_type !== 'live') { $key_prefix = $mk_api_type.'_'; } else { $key_prefix = ''; } $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', ''); $mk_public_key = get_option('mk_'.$key_prefix.'public_key', ''); $mk_private_key = get_option('mk_'.$key_prefix.'private_key', ''); if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) { return false; } $MKAPI = new \Maksekeskus\Maksekeskus($mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true); return $MKAPI; } function mk_is_api_set() { $mk_api_type = get_option('mk_api_type', false); if (!$mk_api_type) { return false; } if ($mk_api_type !== 'live') { $key_prefix = $mk_api_type.'_'; } else { $key_prefix = ''; } $mk_shop_id = get_option('mk_'.$key_prefix.'shop_id', ''); $mk_public_key = get_option('mk_'.$key_prefix.'public_key', ''); $mk_private_key = get_option('mk_'.$key_prefix.'private_key', ''); if (!$mk_shop_id || !$mk_public_key || !$mk_shop_id) { return false; } return true; } function mk_admin_error() { printf( '%2$s