run(); } } if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { register_activation_hook( __FILE__, 'activate_mc_9pay' ); register_deactivation_hook( __FILE__, 'deactivate_mc_9pay' ); require plugin_dir_path( __FILE__ ) . 'includes/class-mc-quetma.php'; run_mc_9pay(); } if (!function_exists('mc_9pay_installed_notice')) { function mc_9pay_installed_notice() { if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){ $class = 'notice notice-error'; $message = __( 'Plugin cổng thanh toán 9PAY cần Woocommerce kích hoạt trước khi sử dụng. Vui lòng kiểm tra Woocommerce', 'qr_auto' ); printf( '

%2$s

', esc_attr( $class ), esc_html( $message ) ); } } } add_action( 'admin_notices', 'mc_9pay_installed_notice' ); /*[CORE] Handle IPN*/ add_filter('rest_url_prefix', function(){ return 'api'; }); add_action('rest_api_init', function(){ if (!is_string($_REQUEST['result'])) { return; } $result = ($_REQUEST['result']); if (!is_string($_REQUEST['checksum'])) { return; } $checksum = ($_REQUEST['checksum']); register_rest_route('nine-pay/v1', '/result-ipn', array( 'methods' => 'POST', 'callback' => function() use ($result, $checksum){ $handleIPN = new NinePayIPN; $handleIPN->handleIPN([ 'result' => $result, 'checksum' => $checksum ]); } )); }); /** * [CORE] * @param $input * @return false|string */ if (!function_exists('ninepay_url_safe_b64_decode')) { function ninepay_url_safe_b64_decode($input) { $remainder = \strlen($input) % 4; if ($remainder) { $padlen = 4 - $remainder; $input .= \str_repeat('=', $padlen); } return \base64_decode(\strtr($input, '-_', '+/')); } } /*[ADMIN] Handle show invoice_no*/ add_action( 'woocommerce_admin_order_data_after_billing_address', function($order){ $invoiceNo = get_post_meta( $order->get_id(), '_invoice_no', true ); if($invoiceNo) { echo '

'.__('Mã yêu cầu').':
'; echo esc_html($invoiceNo); echo '

'; } }, 10, 1 ); /*[THEME] handle show payment method*/ add_filter( 'woocommerce_gateway_description', 'ninepay_gateway_custom_fields', 20, 2 ); if (!function_exists('ninepay_gateway_custom_fields')) { function ninepay_gateway_custom_fields( $description, $payment_id ){ if( 'ninepay-gateway' === $payment_id ){ $payment = new WC_Payment_Gateways(); $settings = $payment->get_available_payment_gateways()['ninepay-gateway']->settings; $configLang = include('includes/gateways/core/lang.php'); $lang = $settings['ninepay_lang']; $paymentMethod = ninepay_get_payment_method($settings); ob_start(); // Start buffering echo '
'; woocommerce_form_field( 'ninepay_payment_method', array( 'type' => 'select', 'label' => __($configLang[$lang]['description_payment_method'], "woocommerce"), 'class' => array('form-row-wide'), 'required' => true, 'options' => $paymentMethod ), ''); echo '
'; $description .= ob_get_clean(); // Append buffered content } return $description; } } /** * @param array $settings * @return array */ if (!function_exists('ninepay_get_payment_method')) { function ninepay_get_payment_method(array $settings) { $configLang = include('includes/gateways/core/lang.php'); $lang = $settings['ninepay_lang']; $result = []; /*wallet*/ if($settings['ninepay_payment_method_wallet'] === 'yes') { $result[NinePayConstance::METHOD_WALLET] = __($configLang[$lang][NinePayConstance::METHOD_WALLET], "woocommerce"); } /*atm*/ if($settings['ninepay_payment_method_atm'] === 'yes') { $result[NinePayConstance::METHOD_ATM] = __($configLang[$lang][NinePayConstance::METHOD_ATM], "woocommerce"); } /*credit*/ if($settings['ninepay_payment_method_credit'] === 'yes') { $result[NinePayConstance::METHOD_CREDIT] = __($configLang[$lang][NinePayConstance::METHOD_CREDIT], "woocommerce"); } /*collection*/ if($settings['ninepay_payment_method_collection'] === 'yes') { $result[NinePayConstance::METHOD_COLLECTION] = __($configLang[$lang][NinePayConstance::METHOD_COLLECTION], "woocommerce"); } return $result; } } if (!function_exists('ninepay_get_foreign_exc_rate')) { function ninepay_get_foreign_exc_rate($currency){ $configFile = include('includes/gateways/core/config.php'); $ninePay = new NinePayGateway(); $paymentConfig = $ninePay->settings; $isTesting = $paymentConfig['is_testing']; if ($isTesting === 'yes') { $host = $configFile['environment']['exc_rate_host_sand']; $forwardLink = $configFile['environment']['x_forward_link_test']; } else { $host = $configFile['environment']['exc_rate_host_production']; $forwardLink = $configFile['environment']['x_forward_link_production']; } $merchantKey = $paymentConfig['merchant_key']; $merchantSecretKey = $paymentConfig['merchant_secret_key']; $time = time(); $data = [ 'merchantKey' => $merchantKey, 'time' => $time, ]; $message = MessageBuilder::instance() ->with($time, $forwardLink, 'POST') ->withParams($data) ->build(); $hmacs = new HMACSignature(); $signature = $hmacs->sign($message, $merchantSecretKey); $httpData = [ 'baseEncode' => base64_encode(json_encode($data, JSON_UNESCAPED_UNICODE)), 'signature' => $signature, ]; $excRateUrl = $host . '?' . http_build_query($httpData); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $excRateUrl); $result=curl_exec($ch); curl_close($ch); $allCurrencies = json_decode($result, true); return $allCurrencies[$currency]['rate']; } } if (!function_exists('ninepay_add_checkout_script')) { function ninepay_add_checkout_script() { global $woocommerce; $language = get_locale(); $configFile = include('includes/gateways/core/config.php'); $totalCart = $woocommerce->cart->total; $currency = get_option('woocommerce_currency'); $excRate = ninepay_get_foreign_exc_rate($currency); $minAmountWalletVnd = $configFile['min_amount_wallet_vnd']; $maxAmountWalletVnd = $configFile['max_amount_wallet_vnd']; $minAmountAtmCardVnd = $configFile['min_amount_atm_card_vnd']; $maxAmountAtmCardVnd = $configFile['max_amount_atm_card_vnd']; $minAmountCreditCardVnd = $configFile['min_amount_credit_card_vnd']; $maxAmountCreditCardVnd = $configFile['max_amount_credit_card_vnd']; $minAmountCollectionVnd = $configFile['min_amount_collection_vnd']; $maxAmountCollectionVnd = $configFile['max_amount_collection_vnd']; ?> session->get('chosen_payment_method')){ $payment = new WC_Payment_Gateways(); $config = $payment->get_available_payment_gateways()['ninepay-gateway']->settings; $configLang = include('includes/gateways/core/lang.php'); $lang = $config['ninepay_lang']; if (is_null($_POST['post_data'])) { return; } $postData = ($_POST['post_data']); parse_str($postData, $result); $totalAmount = $cart->cart_contents_total; $fee = ninepay_add_fee($ninepay_payment_method, $totalAmount, $config); if($fee != 0) { if($lang === NinePayConstance::LANG_VI) { $cart->add_fee( __('Phí thanh toán qua '. $configLang[$lang][$ninepay_payment_method], "woocommerce"), $fee, true); } else { $cart->add_fee( __('Payment fee via '. $configLang[$lang][$ninepay_payment_method], "woocommerce"), $fee, true); } } } } } /** * [THEME] [CORE] * @param $paymentMethod * @param $amount * @param $config * @return mixed */ if (!function_exists('ninepay_add_fee')) { function ninepay_add_fee($paymentMethod, $amount, $config) { switch ($paymentMethod) { case NinePayConstance::METHOD_WALLET: return ninepay_handle_charge($amount, $config['ninepay_payment_method_wallet_fee_percent'], $config['ninepay_payment_method_wallet_fee_fixed']); break; case NinePayConstance::METHOD_ATM: return ninepay_handle_charge($amount, $config['ninepay_payment_method_atm_fee_percent'], $config['ninepay_payment_method_atm_fee_fixed']); break; case NinePayConstance::METHOD_CREDIT: return ninepay_handle_charge($amount, $config['ninepay_payment_method_credit_fee_percent'], $config['ninepay_payment_method_credit_fee_fixed']); break; case NinePayConstance::METHOD_COLLECTION: return ninepay_handle_charge($amount, $config['ninepay_payment_method_collection_fee_percent'], $config['ninepay_payment_method_collection_fee_fixed']); break; default: return 0; } } } /** * @param $amount * @param $feePercent * @param $feeFixed * @return float */ if (!function_exists('ninepay_handle_charge')) { function ninepay_handle_charge($amount, $feePercent, $feeFixed) { $feePercent = empty($feePercent) || !is_numeric($feePercent) ? 0 : $feePercent; $feeFixed = empty($feeFixed) || !is_numeric($feeFixed) ? 0 : $feeFixed; $result = $feeFixed + ($feePercent * $amount/100); return round($result, 2); } } /*[THEME] Reload cart when choice payment gateway or payment method*/ add_action( 'wp_footer', function(){ if(is_checkout() && ! is_wc_endpoint_url()): ?>