'Knet','credit' => 'Credit','Bookeey' => 'Bookeey', 'amex' => 'AMEX', 'APPLEPAY' => 'Apple Pay');
add_action('plugins_loaded', 'init_wc_gateway_bookeey', 0);
function init_wc_gateway_bookeey()
{
if (! class_exists('WC_Payment_Gateway')) {
return;
}
load_plugin_textdomain('wc-bookeey-paymentgateway', false, dirname(plugin_basename(__FILE__)) . '/lang');
add_action('woocommerce_cart_calculate_fees','bookeey_add_custom_fee',20,1);
function bookeey_add_custom_fee($posted_data) {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_gateway = $woocommerce->session->get( 'chosen_payment_method' );
if ($chosen_gateway == 'wc_gateway_bookeey') {
$bookeeyObj = new wc_gateway_bookeey();
$bookeeyObjArray = (array) $bookeeyObj;
$enableProcessingFee = $bookeeyObj->enable_processing_fee;
$processingFeeTitle = $bookeeyObj->processing_fee_title;
if ($enableProcessingFee) {
$amount = $woocommerce->cart->cart_contents_total;
$feeType = $bookeeyObjArray[strtolower($_COOKIE['cardbutton']).'_fee_type'];
$feeValue = $bookeeyObjArray[strtolower($_COOKIE['cardbutton']).'_processing_fee'];
if (!empty($feeType) && !empty($feeValue)) {
switch ($feeType) {
case 'fixed':
$woocommerce->cart->add_fee($processingFeeTitle, $feeValue);
break;
case 'percentage':
$woocommerce->cart->add_fee($processingFeeTitle, $amount * ($feeValue/100));
break;
case 'combination':
$combinationValue = explode(',',$feeValue);
if (count($combinationValue) == 2) {
$percentageValue = $combinationValue[0];
$fixedValue = $combinationValue[1];
$woocommerce->cart->add_fee($processingFeeTitle, ($amount * ($percentageValue/100)) + $fixedValue);
}
break;
default:
break;
}
}
}
}
}
add_action( 'woocommerce_after_checkout_form', 'bookeey_refresh_checkout_on_payment_methods_change' );
function bookeey_refresh_checkout_on_payment_methods_change(){
wc_enqueue_js( "
$( 'form.checkout' ).on( 'change', 'input[name^=\'payment_method\']', function() {
$('body').trigger('update_checkout');
});
");
}
class wc_gateway_bookeey extends WC_Payment_Gateway
{
public function __construct()
{
global $woocommerce;
$this->id = 'wc_gateway_bookeey';
$this->method_title = __('Bookeey Payment', 'wc-bookeey-paymentgateway');
$this->method_description = __('Pay with Bookeey Payment.');
$this->has_fields = false;
$default_card_type_options = array
(
'VISA' => 'VISA',
'MC' => 'MasterCard',
'AMEX' => 'American Express'
);
$this->card_type_options = apply_filters('wc_gateway_bookeey_card_types', $default_card_type_options);
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
$this->private_key = $this->settings['private_key'];
$this->mode = $this->settings['mode'];
$this->merchant_id = $this->settings['merchant_id'];
$this->sub_merchant_id = $this->settings['sub_merchant_id'];
$this->redirecting_message = $this->settings['redirecting_message'];
$this->active_payment_options = $this->settings['payment_options'];
$this->default_payment_option = $this->settings['default_payment_option'];
$this->autocomplete_orders = $this->settings['autocomplete_orders'];
$this->enable_processing_fee = $this->settings['enable_processing_fee'];
$this->processing_fee_title = $this->settings['processing_fee_title'];
$this->knet_fee_type = $this->settings['knet_fee_type'];
$this->knet_processing_fee = $this->settings['knet_processing_fee'];
$this->credit_fee_type = $this->settings['credit_fee_type'];
$this->credit_processing_fee = $this->settings['credit_processing_fee'];
$this->bookeey_fee_type = $this->settings['bookeey_fee_type'];
$this->bookeey_processing_fee = $this->settings['bookeey_processing_fee'];
$this->amex_fee_type = $this->settings['amex_fee_type'];
$this->amex_processing_fee = $this->settings['amex_processing_fee'];
$this->applepay_fee_type = $this->settings['applepay_fee_type'];
$this->applepay_processing_fee = $this->settings['applepay_processing_fee'];
// Actions
add_action('init', array( $this, 'successful_request' ));
add_action('woocommerce_api_wc_gateway_bookeey', array( $this, 'successful_request' ));
add_action('woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ));
add_action('woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ));
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ));
}
/**
* get_icon function.
*
* @access public
* @return string
*/
/* public function get_icon()
{
global $woocommerce;
$icon = '';
if ($this->icon) {
// default behavior
$icon = '';
} elseif ($this->cardtypes) {
// display icons for the selected card types
foreach ($this->cardtypes as $cardtype) {
if (file_exists(plugin_dir_path(__FILE__) . '/images/card-' . strtolower($cardtype) . '.png')) {
$icon .= '
';
}
}
}
return apply_filters('woocommerce_gateway_icon', $icon, $this->id);
} */
/**
* Initialise Gateway Settings Form Fields
*/
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'wc-bookeey-paymentgateway'),
'type' => 'checkbox',
'label' => __('Enable Bookeey Payment', 'wc-bookeey-paymentgateway'),
'default' => 'yes'
),
'mode' => array(
'title' => __('Mode', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'test' => 'Testmode',
'live' => 'Livemode'
),
'description' => __('Select Test or Live mode.', 'wc-bookeey-paymentgateway'),
'default' => 'test'
),
'title' => array(
'title' => __('Title', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'wc-bookeey-paymentgateway'),
'default' => __('Bookeey Payment', 'wc-bookeey-paymentgateway')
),
'description' => array(
'title' => __('Description', 'wc-bookeey-paymentgateway'),
'type' => 'textarea',
'description' => __('This controls the description which the user sees during checkout.', 'wc-bookeey-paymentgateway'),
'default' => __("Pay with Bookeey payment", 'wc-bookeey-paymentgateway')
),
'redirecting_message' => array(
'title' => __('Redirecting Message', 'wc-bookeey-paymentgateway'),
'type' => 'textarea',
'description' => __('Message displayed once the buyer finish the order and is redirected to bookeey.', 'wc-bookeey-paymentgateway'),
'default' => __("Thank you for your order. We are now redirecting to Bookeey Payment Page to finish your order.", 'wc-bookeey-paymentgateway')
),
'merchant_id' => array(
'title' => __('Merchant Id', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Please enter your Merchant Id provided by bookeey.', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'private_key' => array(
'title' => __('Merchant Key', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Please enter your Merchant Key provided by bookeey.', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'sub_merchant_id' => array(
'title' => __('Sub Merchant Id', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Note: Use this setting for single sub-merchant only.
Please enter your Sub Merchant Id provided by bookeey. Leave it empty if you do not have any sub-merchant.', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'payment_options' => array(
'title' => __('Payment Options', 'wc-bookeey-paymentgateway'),
'type' => 'multiselect',
'options' => PAYMENT_OPTIONS,
'description' => __('Select Payment Options to enable.', 'wc-bookeey-paymentgateway'),
'default' => 'knet'
),
'default_payment_option' => array(
'title' => __('Default Payment Option', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => PAYMENT_OPTIONS,
'description' => __('Select Payment Option to be selected by default.', 'wc-bookeey-paymentgateway'),
'default' => 'knet'
),
'autocomplete_orders' => array(
'title' => __('Auto Complete Orders', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'0' => 'No',
'1' => 'Yes'
),
'description' => __('Choosing Yes will automatically change the new order status to Completed.', 'wc-bookeey-paymentgateway'),
'default' => '0'
),
'enable_processing_fee' => array(
'title' => __('Enable Processing Fee', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'0' => 'No',
'1' => 'Yes'
),
'description' => __('Choosing Yes will add the below-configured processing fee in the checkout.', 'wc-bookeey-paymentgateway'),
'default' => '0'
),
'processing_fee_title' => array(
'title' => __('Processing Fee Title', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('This is the processing fee title which the user sees during checkout.', 'wc-bookeey-paymentgateway'),
'default' => __('Fee', 'wc-bookeey-paymentgateway')
),
'knet_fee_type' => array(
'title' => __('KNET Fee Type', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'no' => 'No Charge',
'fixed' => 'Fixed',
'percentage' => 'Percentage',
'combination' => 'Percentage, Fixed'
),
'description' => __('Choose KNET Processing Fee Type.', 'wc-bookeey-paymentgateway'),
'default' => 'fixed'
),
'knet_processing_fee' => array(
'title' => __('KNET Processing Fee', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Enter KNET Processing Fee here.
For Fixed type, enter fixed value. (Ex: To add 1.5 fixed amount just enter 1.5)
For Percentage type, enter percentage value. (Ex: To add 5% just enter 5)
For Percentage, Fixed type, enter percentage value and fixed value separated by comma (,). (Ex: To add 2.5% and 5 fixed amount just enter 2.5,5)', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'credit_fee_type' => array(
'title' => __('Credit Card Fee Type', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'no' => 'No Charge',
'fixed' => 'Fixed',
'percentage' => 'Percentage',
'combination' => 'Percentage, Fixed'
),
'description' => __('Choose Credit Card Processing Fee Type.', 'wc-bookeey-paymentgateway'),
'default' => 'fixed'
),
'credit_processing_fee' => array(
'title' => __('Credit Card Processing Fee', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Enter Credit Card Processing Fee here.
For Fixed type, enter fixed value. (Ex: To add 1.5 fixed amount just enter 1.5)
For Percentage type, enter percentage value. (Ex: To add 5% just enter 5)
For Percentage, Fixed type, enter percentage value and fixed value separated by comma (,). (Ex: To add 2.5% and 5 fixed amount just enter 2.5,5)', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'bookeey_fee_type' => array(
'title' => __('Bookeey Fee Type', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'no' => 'No Charge',
'fixed' => 'Fixed',
'percentage' => 'Percentage',
'combination' => 'Percentage, Fixed'
),
'description' => __('Choose Bookeey Processing Fee Type.', 'wc-bookeey-paymentgateway'),
'default' => 'fixed'
),
'bookeey_processing_fee' => array(
'title' => __('Bookeey Processing Fee', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Enter Bookeey Processing Fee here.
For Fixed type, enter fixed value. (Ex: To add 1.5 fixed amount just enter 1.5)
For Percentage type, enter percentage value. (Ex: To add 5% just enter 5)
For Percentage, Fixed type, enter percentage value and fixed value separated by comma (,). (Ex: To add 2.5% and 5 fixed amount just enter 2.5,5)', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'amex_fee_type' => array(
'title' => __('AMEX Fee Type', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'no' => 'No Charge',
'fixed' => 'Fixed',
'percentage' => 'Percentage',
'combination' => 'Percentage, Fixed'
),
'description' => __('Choose AMEX Processing Fee Type.', 'wc-bookeey-paymentgateway'),
'default' => 'fixed'
),
'amex_processing_fee' => array(
'title' => __('AMEX Processing Fee', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Enter AMEX Processing Fee here.
For Fixed type, enter fixed value. (Ex: To add 1.5 fixed amount just enter 1.5)
For Percentage type, enter percentage value. (Ex: To add 5% just enter 5)
For Percentage, Fixed type, enter percentage value and fixed value separated by comma (,). (Ex: To add 2.5% and 5 fixed amount just enter 2.5,5)', 'wc-bookeey-paymentgateway'),
'default' => ''
),
'applepay_fee_type' => array(
'title' => __('Apple Pay Fee Type', 'wc-bookeey-paymentgateway'),
'type' => 'select',
'options' => array(
'no' => 'No Charge',
'fixed' => 'Fixed',
'percentage' => 'Percentage',
'combination' => 'Percentage, Fixed'
),
'description' => __('Choose Apple Pay Processing Fee Type.', 'wc-bookeey-paymentgateway'),
'default' => 'fixed'
),
'applepay_processing_fee' => array(
'title' => __('Apple Pay Processing Fee', 'wc-bookeey-paymentgateway'),
'type' => 'text',
'description' => __('Enter Apple Pay Processing Fee here.
For Fixed type, enter fixed value. (Ex: To add 1.5 fixed amount just enter 1.5)
For Percentage type, enter percentage value. (Ex: To add 5% just enter 5)
For Percentage, Fixed type, enter percentage value and fixed value separated by comma (,). (Ex: To add 2.5% and 5 fixed amount just enter 2.5,5)', 'wc-bookeey-paymentgateway'),
'default' => ''
)
);
} // End init_form_fields()
/**
* Not payment fields, but show the description of the payment.
**/
public function payment_fields()
{
if ($this->description) {
echo wpautop(wptexturize($this->description));
} ?>
default_payment_option;
setcookie("cardbutton", $checkedOption, time() + 60 * 60,"/");
setcookie("pymcardbutton", $checkedOption, time() + 60 * 60,"/");
}else{
$checkedOption = $_COOKIE['cardbutton'];
setcookie("pymcardbutton", $checkedOption, time() + 60 * 60,"/");
}
foreach ($this->active_payment_options as $option) { ?>
get_total(); if(!empty($_COOKIE['cardbutton'])){ $credit = $_COOKIE['cardbutton']; } else{ $credit = 'credit'; } update_post_meta($order_id, 'payment_type', $credit); $order->set_payment_method_title("Bookeey Payment (".$credit.")"); $order->save(); // New Post Params for New API $errorMessage = ""; $txnDtl = array( array( "SubMerchUID" => $single_sub_mid, "Txn_AMT" => $amt ) ); $txnHdr = array( "PayFor" => "ECom", "Txn_HDR" => $rndnum, "PayMethod" => $credit, "BKY_Txn_UID" => "", "Merch_Txn_UID" => $order_id, "hashMac" => $hashed ); $appInfo = array( "APPTyp" => "", "OS" => $osType.' - '.$browser, "DevcType" => $deviceType, "IPAddrs" => $ipAddress, "Country" => "", "AppVer" => $appVersion, "UsrSessID" => $sessionToken, "APIVer" => $apiVersion ); $pyrDtl = array( "Pyr_MPhone" => $payerPhone, "Pyr_Name" => $payerName ); $merchDtl = array( "BKY_PRDENUM" => "ECom", "FURL" => $fu, "MerchUID" => $mid, "SURL" => $su ); $moreDtl = array( "Cust_Data1" => "", "Cust_Data3" => "", "Cust_Data2" => "" ); $postParams['Do_TxnDtl'] = $txnDtl; $postParams['Do_TxnHdr'] = $txnHdr; $postParams['Do_Appinfo'] = $appInfo; $postParams['Do_PyrDtl'] = $pyrDtl; $postParams['Do_MerchDtl'] = $merchDtl; $postParams['DBRqst'] = "PY_ECom"; $postParams['Do_MoreDtl'] = $moreDtl; $ch = curl_init(); $headers = array( 'Accept: application/json', 'Content-Type: application/json', ); $paymentLogFile = "bookeey_payment"; $logRequestContent['Order_id'] = $logResponseContent['Order_id'] = $logDecisionContent['Order_id'] = $order_id; $logRequestContent['API_url'] = $gateway_url; $logRequestContent['Request'] = $postParams; bookeey_logs("**** Payment API Request ****\n".json_encode($logRequestContent)."\n**** Payment API Request End ****\n",$paymentLogFile); curl_setopt($ch, CURLOPT_URL,$gateway_url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postParams)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $serverOutput = curl_exec($ch); $decodeOutput = json_decode($serverOutput, true); curl_close ($ch); $logResponseContent['API_url'] = $gateway_url; $logResponseContent['Response'] = $decodeOutput; bookeey_logs("**** Payment API Response ****\n".json_encode($logResponseContent)."\n**** Payment API Response End ****\n",$paymentLogFile); if (isset($decodeOutput['PayUrl'])) { if ($decodeOutput['PayUrl'] == '') { $errorMessage = $decodeOutput['ErrorMessage']; }else{ // echo "Pay URL: ".$decodeOutput['PayUrl']; update_post_meta($order_id, 'merchantid', $order_id); $order->update_status('pending'); $logDecisionContent['Decision'] = "Redirecting to Payment Gateway"; bookeey_logs("**** Payment Decision ****\n".json_encode($logDecisionContent)."\n**** Payment Decision End ****\n",$paymentLogFile); header("Location: ".$decodeOutput['PayUrl']); } }else if(isset($decodeOutput['Message'])){ $errorMessage = $decodeOutput['Message']; }else{ $errorMessage = "Payment Url not available."; } if ($errorMessage != "") { $order->update_status('pending'); $logDecisionContent['Decision'] = "API Failure. Redirecting to failure page."; $logDecisionContent['Error'] = $errorMessage; bookeey_logs("**** Payment Decision ****\n".json_encode($logDecisionContent)."\n**** Payment Decision End ****\n",$paymentLogFile); wc_add_notice(sprintf(__('Transaction Failed. Error Message: %s', 'wc-bookeey-paymentgateway'), $errorMessage), $notice_type = 'error'); unset($_COOKIE['orderid']); wp_safe_redirect( wc_get_page_permalink( 'cart' ) ); } } /** * Process the payment and return the result **/ public function process_payment($order_id) { $order = new WC_Order($order_id); setcookie("orderid", $order_id, time() + 60 * 60,"/"); return array( 'result' => 'success', 'redirect' => $order->get_checkout_payment_url(true) ); } /** * receipt_page **/ public function receipt_page($order) { echo '
'.__('Thank you for your order, please click the button below to pay with Bookeey Payment.', 'wc-bookeey-paymentgateway').'
'; echo $this->generate_bookeey_form($order); } /** * Successful Payment! **/ public function successful_request($order_id) { global $woocommerce,$wpdb; $paymentLogFile = "bookeey_payment"; $logContent['Order_id'] = intval($_GET['merchantTxnId']); $logContent['Response'] = $_GET; if(isset($_GET['finalstatus']) && base64_decode($_GET['finalstatus']) == 'success'){ bookeey_logs("**** Returned from Payment Gateway ****\n",$paymentLogFile); bookeey_logs("Order Id: ".$logContent['Order_id'],$paymentLogFile); bookeey_logs("=== Response ===\n".json_encode($logContent['Response'])."\n",$paymentLogFile); bookeey_logs("=== Payment Status Check Start ===",$paymentLogFile); $bookeeyObj = new wc_gateway_bookeey(); $paymentMode = $bookeeyObj->mode; if ($paymentMode) { if ($paymentMode == 'test') { $requeryUrl = 'https://demo.bookeey.com/pgapi/api/payment/paymentstatus'; } elseif ($paymentMode == 'live') { $requeryUrl = 'https://pg.bookeey.com/internalapi/api/payment/paymentstatus'; } $orderIds[] = intval($_GET['merchantTxnId']); if (count($orderIds) > 0) { $mid = $bookeeyObj->merchant_id; $secretKey = $bookeeyObj->private_key; $data = "$mid|$secretKey"; $hashed = hash('sha512', $data); $postParams['Mid'] = $mid; $postParams['MerchantTxnRefNo'] = $orderIds; $postParams['HashMac'] = $hashed; bookeey_logs("--Post Params",$paymentLogFile); bookeey_logs("----".json_encode($postParams),$paymentLogFile); $ch = curl_init(); $headers = array( 'Accept: application/json', 'Content-Type: application/json', ); curl_setopt($ch, CURLOPT_URL,$requeryUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postParams)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $serverOutput = curl_exec($ch); $decodeOutput = json_decode($serverOutput, true); curl_close ($ch); bookeey_logs("--Requery API URL - ".json_encode($requeryUrl),$paymentLogFile); bookeey_logs("--Requery API Output",$paymentLogFile); bookeey_logs("----".json_encode($decodeOutput),$paymentLogFile); if (count($decodeOutput['PaymentStatus']) > 0) { foreach ($decodeOutput['PaymentStatus'] as $key => $data) { if($data['StatusDescription']=='Transaction Success'){ bookeey_logs("------ Payment Status of Order #".$data['MerchantTxnRefNo']." is - ".$data['StatusDescription'],$paymentLogFile); $finalStatus = true; } } }else{ bookeey_logs("------ Payment Status of Order #".intval($_GET['merchantTxnId'])." is - ".$data['StatusDescription'],$paymentLogFile); $finalStatus = false; } }else{ bookeey_logs("-- Order Id not available",$paymentLogFile); $finalStatus = false; } }else{ bookeey_logs("--Payment Mode not found.",$paymentLogFile); $finalStatus = false; } bookeey_logs("=== Payment Status Check End ===\n",$paymentLogFile); }else { $finalStatus = false; } if (isset($_GET['finalstatus'])) { if(!$finalStatus){ $order_id = $_COOKIE['orderid']; $params['ResponseMessage'] = $_GET['errorMessage']; $order = new WC_Order($order_id); if (!empty($order)) { $order->update_status( 'failed' ); } bookeey_logs("Payment Failed. Redirecting to failure page.\n",$paymentLogFile); wc_add_notice(sprintf(__('Transaction Failed. The Error Message was %s', 'wc-bookeey-paymentgateway'), $params['ResponseMessage']), $notice_type = 'error'); wp_safe_redirect( wc_get_page_permalink( 'cart' ) ); //Transacción declinada. //wc_add_notice(sprintf(__('Transaction Failed. The Error Message was %s', 'wc-bookeey-paymentgateway'), $params['ResponseMessage']), $notice_type = 'error'); //wp_redirect(get_permalink(get_option('woocommerce_checkout_page_id'))); unset($_COOKIE['orderid']); exit; }else{ $params['txnId'] = $_GET['txnId']; $params['merchantTxnId'] = $_GET['merchantTxnId']; $post_values = ""; foreach ($params as $key => $value) { $post_values .= $value; } // $post_values .= $this->private_key; // $localHash = $this->encryptAndEncode($post_values); $meta = $wpdb->get_results("SELECT post_id FROM `".$wpdb->postmeta."` WHERE meta_key='merchantid' AND meta_value='".$params['merchantTxnId']."'"); if ($params['txnId']) { $currentDateTime = current_time('F d, Y h:i a'); $orderNote = "Bookeey Payment Successful.\nTransaction ID: ".$params['txnId']."\nPayment Method: ".$_COOKIE['cardbutton']."\nPaid on ".strval($currentDateTime); $neworder = $meta['0']->post_id; //Transacción Aceptada. $order = new WC_Order($neworder); $order->add_order_note(sprintf(__($orderNote))); $order->payment_complete(); update_post_meta($neworder, 'transaction', $params['txnId']); update_post_meta($neworder, 'payment_type', $_COOKIE['cardbutton']); if ($this->autocomplete_orders) { $order->update_status('completed'); } $order->reduce_order_stock(); // Remove cart WC()->cart->empty_cart(); bookeey_logs("Payment Successful. Redirecting to success page.\n",$paymentLogFile); bookeey_logs("**** Returned from Payment Gateway End ****\n",$paymentLogFile); wp_redirect($this->get_return_url($order)); unset($_COOKIE['cardbutton']); exit; } } } } private function encryptAndEncode($strIn) { //The encryption required by bookeey is SHA-512 $result = mb_convert_encoding($strIn, 'UTF-16LE', 'ASCII'); $result = hash('sha512', $result); return $result; } private function force_ssl($url) { if ('yes' == get_option('woocommerce_force_ssl_checkout')) { $url = str_replace('http:', 'https:', $url); } return $url; } } /** * Add the gateway to WooCommerce **/ function add_bookeey_gateway($methods) { $methods[] = 'wc_gateway_bookeey'; return $methods; } add_filter('woocommerce_payment_gateways', 'add_bookeey_gateway'); } function add_this_script_footer(){ wp_enqueue_script( 'bookeey-js', plugins_url( '/js/bookeey.js', __FILE__ )); wp_enqueue_style( 'bookeey-css', plugins_url( 'css/bookeey.css', __FILE__ ) ); } add_action('wp_footer', 'add_this_script_footer'); add_action( 'wp_ajax_woo_get_ajax_data', 'digit_checkout_radio_choice_set_session' ); add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'digit_checkout_radio_choice_set_session' ); function digit_checkout_radio_choice_set_session() { if ( isset($_POST['radio']) ){ $radio = $_POST['radio']; setcookie("cardbutton", $_POST['radio'], time() + 60 * 60,"/"); setcookie("pymcardbutton", $_POST['radio'], time() + 60 * 60,"/"); } die(); } function digit_startSession() { if(!isset($_COOKIE["cardbutton"])) { setcookie("cardbutton", "_", time() + 60 * 60,"/"); setcookie("pymcardbutton", "_", time() + 60 * 60,"/"); } } add_action('init', 'digit_startSession', 1); function bookeey_curl_before_request($curlhandle){ session_write_close(); } add_action('requests-curl.before_request','bookeey_curl_before_request', 9999); add_filter( 'woocommerce_get_order_item_totals', 'custom_woocommerce_get_order_item_totals' ); function custom_woocommerce_get_order_item_totals( $totals ) { unset( $totals['payment_method'] ); return $totals; } add_action( 'woocommerce_email_after_order_table', 'wc_add_payment_type_to_emails', 15, 2 ); function wc_add_payment_type_to_emails( $order, $is_admin_email ) { $paymentTitle = $order->get_payment_method_title(); echo '' . $paymentTitle . '
'; /* if(!empty($_SESSION['cardbutton'])){ echo '' . $_SESSION['cardbutton'] . '
'; }else{ echo 'credit
'; } */ } add_action( 'woocommerce_before_checkout_form', 'skyverge_add_checkout_content', 12 ); function skyverge_add_checkout_content() { ?> id, '_payment_method', true ); if($method != 'wc_gateway_bookeey') return; $transaction = get_post_meta( $order->id, 'transaction', true ); echo ''.__( 'Transaction ID').': ' . $transaction . '
'; } /****************This code for update order status*************/ /** * New Cron Schedule */ add_filter( 'cron_schedules', 'add_cron_interval' ); function add_cron_interval( $schedules ) { $schedules['every_thirty_minutes'] = array( 'interval' => 1800, 'display' => esc_html__( 'Every Thirty Minute' ), ); return $schedules; } /** * Activate cron on plugin activation */ register_activation_hook(__FILE__, 'bookeey_cron_activate'); function bookeey_cron_activate() { if (!wp_next_scheduled('bookeey_cron_hook')) { wp_schedule_event(time(), 'every_thirty_minutes', 'bookeey_cron_hook'); } } /** * Deactivate cron on plugin deactivation */ register_deactivation_hook(__FILE__, 'bookeey_cron_deactivate'); function bookeey_cron_deactivate() { wp_clear_scheduled_hook('bookeey_cron_hook'); } add_action('bookeey_cron_hook', 'bookeey_requery'); /** * Bookeey Logs Generation Function */ function bookeey_logs($message,$logfilename) { try{ if(is_array($message)) { $message = json_encode($message); } $file = fopen($logfilename.".log","a"); echo fwrite($file, "\n" . current_time('Y-m-d h:i:s') . " :: " . $message); fclose($file); }catch(Exception $e){ } } function bookeey_requery() { $logfile = "bookeey_requery"; bookeey_logs("********Bookeey Requery Cron Started********",$logfile); $bookeeyObj = new wc_gateway_bookeey(); $paymentMode = $bookeeyObj->mode; if ($paymentMode) { if ($paymentMode == 'test') { $requeryUrl = 'https://demo.bookeey.com/pgapi/api/payment/paymentstatus'; } elseif ($paymentMode == 'live') { $requeryUrl = 'https://pg.bookeey.com/internalapi/api/payment/paymentstatus'; } $query = new WC_Order_Query( array( 'limit' => -1, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'ids', 'payment_method' => 'wc_gateway_bookeey', 'date_query' => array( array( 'after' => '24 hours ago' ) ) )); $orders = $query->get_orders(); $orderIds = array(); foreach ($orders as $key => $value) { $order = wc_get_order( $value ); $order_id = $order->get_id(); // Get the order ID $parent_id = $order->get_parent_id(); // Get the parent order ID (for subscriptions…) $user_id = $order->get_user_id(); // Get the costumer ID $user = $order->get_user(); // Get the WP_User object $order_status = $order->get_status(); if($order_status=='pending'){ $order_data = $order->get_data(); $txid = get_post_meta($order_id, 'merchantid',true); if ($order_id == $txid) { $orderIds[] = $order_id; } } } if (count($orderIds) > 0) { $mid = $bookeeyObj->merchant_id; $secretKey = $bookeeyObj->private_key; $data = "$mid|$secretKey"; $hashed = hash('sha512', $data); $postParams['Mid'] = $mid; $postParams['MerchantTxnRefNo'] = $orderIds; $postParams['HashMac'] = $hashed; bookeey_logs("--Post Params",$logfile); bookeey_logs("----".json_encode($postParams),$logfile); $ch = curl_init(); $headers = array( 'Accept: application/json', 'Content-Type: application/json', ); curl_setopt($ch, CURLOPT_URL,$requeryUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postParams)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $serverOutput = curl_exec($ch); $decodeOutput = json_decode($serverOutput, true); curl_close ($ch); bookeey_logs("--Requery API URL - ".$requeryUrl,$logfile); bookeey_logs("--Requery API Output",$logfile); bookeey_logs("----".json_encode($decodeOutput),$logfile); if (count($decodeOutput['PaymentStatus']) > 0) { foreach ($decodeOutput['PaymentStatus'] as $key => $data) { if($data['StatusDescription']=='Transaction Success'){ $order = wc_get_order($data['MerchantTxnRefNo']); $order_id = $order->get_id(); update_post_meta($order_id, 'transaction', $data['PaymentId']); $order->update_status('processing'); if ($bookeeyObj->autocomplete_orders) { $order->update_status('completed'); } $order->reduce_order_stock(); bookeey_logs("------ Order #".$order_id." updated",$logfile); } } } }else{ bookeey_logs("--No Orders available for requery",$logfile); } }else{ bookeey_logs("--Payment Mode not found.",$logfile); } bookeey_logs("********Bookeey Requery Cron Finished********",$logfile); wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } // add_action( 'wp_enqueue_scripts', 'my_enqueue' ); // add_action( 'woocommerce_thankyou', 'custom_content_thankyou', 10, 1 ); /* function custom_content_thankyou( $order_id ) { echo '