run(); } } if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) { register_activation_hook( __FILE__, 'activate_mc_9pay' ); register_deactivation_hook( __FILE__, 'deactivate_mc_9pay' ); require plugin_dir_path( __FILE__ ) . 'includes/class-mc-ninepay.php'; run_mc_9pay(); } if ( ! function_exists( 'mc_9pay_installed_notice' ) ) { /** * Installed 9pay notice * * @return void */ function mc_9pay_installed_notice() { if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) { $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 ( isset( $_REQUEST['result'], $_REQUEST['checksum'] ) ) { $result = sanitize_text_field( wp_unslash( $_REQUEST['result'] ) ); if ( ! is_string( $result ) ) { return; } $checksum = sanitize_text_field( wp_unslash( $_REQUEST['checksum'] ) ); if ( ! is_string( $checksum ) ) { return; } register_rest_route( 'nine-pay/v1', '/result-ipn', array( 'methods' => 'POST', 'callback' => function() use ( $result, $checksum ) { $handle_ipn = new NinePayIPN(); $handle_ipn->handle_ipn( array( 'result' => $result, 'checksum' => $checksum, ) ); }, ) ); } } ); if ( ! function_exists( 'ninepay_url_safe_b64_decode' ) ) { /** * Safe Url Base 64 decode * * @param string $input encoded String. * @return false|string */ 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 ) { $invoice_no = get_post_meta( $order->get_id(), '_invoice_no', true ); if ( $invoice_no ) { echo '

Mã yêu cầu:
'; echo esc_html( $invoice_no ); 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' ) ) { /** * 9Pay custom fields * * @param mixed $description Description. * @param mixed $payment_id Payment ID. * @return mixed|string */ 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; $config_lang = include 'includes/gateways/core/lang.php'; $lang = $settings['ninepay_lang']; $payment_method = ninepay_get_payment_method( $settings ); ob_start(); echo '
'; woocommerce_form_field( 'ninepay_payment_method', array( 'type' => 'select', 'label' => $config_lang[ $lang ]['description_payment_method'], 'class' => array( 'form-row-wide' ), 'required' => true, 'options' => $payment_method, ), '' ); echo '
'; $description .= ob_get_clean(); } return $description; } } /** * @param array $settings * @return array */ if ( ! function_exists( 'ninepay_get_payment_method' ) ) { /** * 9pay get Method * * @param array $settings Settings. * @return array */ function ninepay_get_payment_method( array $settings ) { $config_lang = include 'includes/gateways/core/lang.php'; $lang = $settings['ninepay_lang']; $result = array(); /*wallet*/ if ( $settings['ninepay_payment_method_wallet'] === 'yes' ) { $result[ NinePayConstance::METHOD_WALLET ] = $config_lang[ $lang ][ NinePayConstance::METHOD_WALLET ]; } /*atm*/ if ( $settings['ninepay_payment_method_atm'] === 'yes' ) { $result[ NinePayConstance::METHOD_ATM ] = $config_lang[ $lang ][ NinePayConstance::METHOD_ATM ]; } /*credit*/ if ( $settings['ninepay_payment_method_credit'] === 'yes' ) { $result[ NinePayConstance::METHOD_CREDIT ] = $config_lang[ $lang ][ NinePayConstance::METHOD_CREDIT ]; } /*collection*/ if ( $settings['ninepay_payment_method_collection'] === 'yes' ) { $result[ NinePayConstance::METHOD_COLLECTION ] = $config_lang[ $lang ][ NinePayConstance::METHOD_COLLECTION ]; } /*collection*/ if ( $settings['ninepay_payment_method_collection'] === 'yes' ) { $result[ NinePayConstance::METHOD_COLLECTION ] = $config_lang[ $lang ][ NinePayConstance::METHOD_COLLECTION ]; } /*collection*/ if ( $settings['ninepay_payment_method_installment'] === 'yes' ) { $result[ NinePayConstance::METHOD_INSTALLMENT ] = $config_lang[ $lang ][ NinePayConstance::METHOD_INSTALLMENT ]; } return $result; } } if ( ! function_exists( 'ninepay_get_foreign_exc_rate' ) ) { /** * 9Pay get foreign exec rate * * @param mixed $currency Currency. * @return mixed */ function ninepay_get_foreign_exc_rate( $currency ) { $config_file = include 'includes/gateways/core/config.php'; $ninepay = new NinePayGateway(); $payment_config = $ninepay->settings; $is_testing = $payment_config['is_testing']; if ( $is_testing === 'yes' ) { $host = $config_file['environment']['exc_rate_host_sand']; $forward_link = $config_file['environment']['x_forward_link_test']; } else { $host = $config_file['environment']['exc_rate_host_production']; $forward_link = $config_file['environment']['x_forward_link_production']; } $merchant_key = $payment_config['merchant_key']; $merchant_secret_key = $payment_config['merchant_secret_key']; $time = time(); $data = array( 'merchantKey' => $merchant_key, 'time' => $time, ); $message = MessageBuilder::instance() ->with( $time, $forward_link, 'POST' ) ->with_params( $data ) ->build(); $hmacs = new HMACSignature(); $signature = $hmacs->sign( $message, $merchant_secret_key ); $http_data = array( 'baseEncode' => base64_encode( wp_json_encode( $data, JSON_UNESCAPED_UNICODE ) ), 'signature' => $signature, ); $exec_rate_url = $host . '?' . http_build_query( $http_data ); $result = wp_remote_retrieve_body( wp_remote_get( $exec_rate_url ) ); $all_currencies = json_decode( $result, true ); if($currency === "VND") return 1; return $all_currencies[ $currency ]['rate']; } } if ( ! function_exists( 'ninepay_add_checkout_script' ) ) { /** * 9pay add checkout script * * @return void */ function ninepay_add_checkout_script() { global $woocommerce; $ninepay = new NinePayGateway(); $payment_config = $ninepay->settings; $language = get_locale(); $config_file = include 'includes/gateways/core/config.php'; $total_cart = $woocommerce->cart->total; $currency = get_option( 'woocommerce_currency' ); $exc_rate = ninepay_get_foreign_exc_rate( $currency ); $min_amount_wallet_vnd = $config_file['min_amount_wallet_vnd']; $max_amount_wallet_vnd = $config_file['max_amount_wallet_vnd']; $min_amount_atm_card_vnd = $config_file['min_amount_atm_card_vnd']; $max_amount_atm_card_vnd = $config_file['max_amount_atm_card_vnd']; $min_amount_credit_card_vnd = $config_file['min_amount_credit_card_vnd']; $max_amount_credit_card_vnd = $config_file['max_amount_credit_card_vnd']; $min_amount_collection_vnd = $config_file['min_amount_collection_vnd']; $max_amount_collection_vnd = $config_file['max_amount_collection_vnd']; $min_amount_bnpl_vnd = $config_file['min_amount_bnpl_vnd']; $max_amount_bnpl_vnd = $config_file['max_amount_bnpl_vnd']; $min_amount_intallment_vnd = $payment_config['ninepay_payment_method_installment_min_amount']; ?> session->get( 'chosen_payment_method' ) ) { $payment = new WC_Payment_Gateways(); $ninepay_payment_method = sanitize_text_field( wp_unslash( $_POST['ninepay_payment_method'] ) ); $config = $payment->get_available_payment_gateways()['ninepay-gateway']->settings; $config_lang = include 'includes/gateways/core/lang.php'; $lang = $config['ninepay_lang']; if(empty($_POST['post_data'])) { return; } $post_data = sanitize_text_field( wp_unslash( $_POST['post_data'] ) ); if ( is_null( $post_data ) ) { return; } parse_str( $post_data, $result ); $total_amount = $cart->cart_contents_total; $fee = !empty($ninepay_payment_method) ? ninepay_add_fee( $ninepay_payment_method, $total_amount, $config ) : 0; if ( $fee !== 0 ) { if ( $lang === NinePayConstance::LANG_VI ) { $txt_arg = 'Phí thanh toán qua ' . $config_lang[ $lang ][ $ninepay_payment_method ]; $cart->add_fee( $txt_arg, $fee, true ); } else { $txt_arg = 'Payment fee via ' . $config_lang[ $lang ][ $ninepay_payment_method ]; $cart->add_fee( $txt_arg, $fee, true ); } } } } } if ( ! function_exists( 'ninepay_add_fee' ) ) { /** * 9pay add fee * * @param mixed $payment_method Payment method. * @param mixed $amount Amount value. * @param array $config Config Object. * @return float|int */ function ninepay_add_fee( $payment_method, $amount, $config ) { switch ( $payment_method ) { case NinePayConstance::METHOD_WALLET: return ninepay_handle_charge( $amount, $config['ninepay_payment_method_wallet_fee_percent'], $config['ninepay_payment_method_wallet_fee_fixed'] ); case NinePayConstance::METHOD_ATM: return ninepay_handle_charge( $amount, $config['ninepay_payment_method_atm_fee_percent'], $config['ninepay_payment_method_atm_fee_fixed'] ); case NinePayConstance::METHOD_CREDIT: return ninepay_handle_charge( $amount, $config['ninepay_payment_method_credit_fee_percent'], $config['ninepay_payment_method_credit_fee_fixed'] ); case NinePayConstance::METHOD_COLLECTION: return ninepay_handle_charge( $amount, $config['ninepay_payment_method_collection_fee_percent'], $config['ninepay_payment_method_collection_fee_fixed'] ); case NinePayConstance::METHOD_BNPL: return ninepay_handle_charge( $amount, $config['ninepay_payment_method_bnpl_fee_percent'], $config['ninepay_payment_method_bnpl_fee_fixed'] ); case NinePayConstance::METHOD_INSTALLMENT: return ninepay_handle_charge( $amount, $config['ninepay_payment_method_installment_fee_percent'], $config['ninepay_payment_method_installment_fee_fixed'] ); default: return 0; } } } if ( ! function_exists( 'ninepay_handle_charge' ) ) { /** * 9pay handle charge * * @param mixed $amount Amount. * @param mixed $fee_percent Fee percent. * @param mixed $fee_fixed Fee fixed. * @return float */ function ninepay_handle_charge( $amount, $fee_percent, $fee_fixed ) { $fee_percent = empty( $fee_percent ) || ! is_numeric( $fee_percent ) ? 0 : $fee_percent; $fee_fixed = empty( $fee_fixed ) || ! is_numeric( $fee_fixed ) ? 0 : $fee_fixed; $result = $fee_fixed + ( $fee_percent * $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() ) : ?>