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( '
';
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()):
?>