File: /var/www/html/breadsecret.com/wp-content/plugins/qfpay-for-woocommerce/class-wc-gateway-qfpay.php
<?php
if (!defined('ABSPATH')) {
exit;
}
require_once(QFPAY_PLUGIN_DIR.'/vendor/autoload.php');
class WC_Gateway_QFPay extends WC_Payment_Gateway {
public static $log_enabled = false;
public static $log = false;
public $title = "QFPay Secure Checkout";
public $description = '';
protected $payment_method = '';
protected $pm = '';
protected $appcode;
protected $appkey;
public $paymentIcons;
public function __construct()
{
$class_name = get_class($this);
$this->pm = 'qfpay';
$this->id = strtolower($this->pm);
$this->icon = 'https://sdk.qfapi.com/images/logo.png';
$this->has_fields = false;
$method_title = $this->getMethodTitle();
$this->order_button_text = __('Pay Now', 'woocommerce');
$this->method_title = $method_title;
$this->method_description = __('Allow customers to easily checkout with QFPay Secure Checkout', 'woocommerce');
$this->supports = array(
'products',
'refunds'
);
// Load the settings
$this->init_form_fields();
$this->init_settings();
$this->init_qfpay_setting();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
self::$log_enabled = $this->debug;
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_filter('woocommerce_gateway_icon', array($this, 'custom_payment_gateway_icons'), 10, 2 );
require_once('includes/class-wc-gateway-qfpay-notification-handler.php');
new WC_Gateway_QFPay_Notification_Handler();
}
public function init_form_fields()
{
//$method_title = $this->getMethodTitle();
$this->form_fields = array(
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => $this->method_title,
'desc_tip' => true,
),
'appcode' => array(
'title' => __('App Code','woocommerce'),
'type' => 'text',
'description' => __('Get you App Code from QFPay','woocommerce'),
'default' => '',
'desc_tip' => true,
'placeholder' => __('Required','woocommerce')
),
'appkey' => array(
'title' => __('App Key','woocommerce'),
'type' => 'text',
'description' => __('Get you App Key from QFPay','woocommerce'),
'default' => '',
'desc_tip' => true,
'placeholder' => __('Required','woocommerce')
),
'paymenticons' => array(
'title' => __('Payment Logos', 'woocommerce'),
'type' => 'multiselect',
'description' => __('Display payment icons in the checkout page', 'woocommerce'),
'desc_tip' => true,
'css' => 'height: 10rem;',
'options' => $this->getPaymentIcons()
),
);
}
protected function getMethodTitle()
{
$method_title = 'QFPay';
if ($this->title) {
$method_title = $this->title;
} else {
$method_title = __($this->pm, 'woocommerce');
$index = strrpos($this->payment_method, '_');
if ($index && substr($this->payment_method, $index + 1) == substr($method_title, strlen($method_title) - 2)) {
$method_title = substr($method_title, 0, strlen($method_title) - 2);
}
}
return $method_title;
}
public function getPaymentIcons()
{
$paymentMethods = [
'visa' => __('Visa', 'woocommerce'),
'mastercard' => __('Master Card', 'woocommerce'),
'wechatpay' => __('Wechat Pay', 'woocommerce'),
'alipayhk' => __('Alipay HK', 'woocommerce'),
'alipaycn' => __('Alipay CN', 'woocommerce'),
'upop' => __('UnionPay', 'woocommerce'),
'quickpass' => __('QuickPass', 'woocommerce'),
'payme' => __('PayMe', 'woocommerce'),
'fps' => __('FPS', 'woocommerce'),
'applepay' => __('Apple Pay', 'woocommerce'),
];
return $paymentMethods;
}
public function custom_payment_gateway_icons($icon, $id)
{
$icons = $this->getPaymentIcons();
if($id === 'qfpay') {
$icon = '';
if ($this->paymentIcons) {
foreach ($this->paymentIcons as $paymentIcon) {
$icon .= ' <img src="' . QFPAY_PLUGIN_URL . 'assets/images/'.$paymentIcon.'.svg" alt="' . esc_attr( $icons[$paymentIcon] ) . '" title="' . esc_attr( $icons[$paymentIcon] ) . '" />';
}
}
}
return $icon;
}
protected function init_qfpay_setting()
{
$this->appcode = $this->get_option('appcode');
$this->appkey = $this->get_option('appkey');
$this->paymentIcons = $this->get_option('paymenticons');
$this->debug = 'yes' === $this->get_option('debug', 'no');
}
public function get_appcode()
{
return $this->appcode;
}
public function get_appkey()
{
return $this->appkey;
}
public static function log($message)
{
if (self::$log_enabled)
{
if (empty(self::$log))
{
self::$log = new WC_Logger();
}
self::$log->add('QFPay', $message);
}
}
public function get_pmid()
{
return $this->payment_method;
}
public function process_payment($order_id)
{
error_log('start process_payment');
require_once('includes/class-wc-gateway-qfpay-request.php');
$order = wc_get_order($order_id);
$qfpay_request = new WC_Gateway_QFPay_Request($this);
$checkout_url = $qfpay_request->get_checkout_url($order, $this->get_return_url($order));
error_log('Checkout url:'.$checkout_url);
return array(
'result' => 'success',
'redirect' => $checkout_url
);
}
public function process_refund($order_id, $amount = null, $reason = '')
{
error_log('start process_refund');
$order = wc_get_order($order_id);
$orig_syssn = $order->get_meta('syssn', true);
error_log('order id:'.$order_id.', '.'original syssn:'.$orig_syssn.',refund amount:'.$amount);
$total_order_amount = (float)strip_tags(trim($order->get_total()));
$total_order_amount_value = number_format($total_order_amount, 2, '.', '');
$amount = (float)strip_tags(trim($amount));
$amount_value = number_format($amount, 2, '.', '');
$out_trade_no = $order_id . '_' . time();
if ($amount_value <=0) {
throw new Exception(__('Refund failed, the refund amount should be greater than 0.', 'woocommerce'));
}
error_log('order id:'.$order_id.',refund amount value:'.$amount_value.',total order amount value:'.$total_order_amount_value);
if ($amount_value > $total_order_amount_value) {
throw new Exception(__('Refund failed, the refund amount should be less than or equal to the remaining refund amount value.', 'woocommerce'));
}
if (empty($orig_syssn)) {
throw new Exception(__('Refund failed, the original syssn does not exist, please go to QFPay Portal for checking.', 'woocommerce'));
}
require_once('includes/class-wc-gateway-qfpay-request.php');
$qfpay_refund_request = new WC_Gateway_QFPay_Request($this);
//create refund record in order metadata
$refund_order_meta_key = 'refund_order_'.$out_trade_no;
error_log('refund_order_meta_key:'.$refund_order_meta_key);
//create refund request to QFPay
$result = $qfpay_refund_request->refund($orig_syssn, $order_id, $amount, $out_trade_no);
if (($result['respcd'] === '1143') || ($result['respcd'] === '1145')) {
$order->update_meta_data($refund_order_meta_key, 'processing');
//if received 1143/1145, wait for refund success notification
$processing_note = 'Refund Order is processing in QFPay<br/>' . 'Refund Merchant Order ID: <strong>'. $out_trade_no . '</strong><br/>';
$order->add_order_note($processing_note);
$order->save();
throw new Exception(__('Your refund is currently being processed. Please check back later in your order details page', 'woocommerce'));
}
if (!($result['respcd'] === '0000')) {
$order->update_meta_data($refund_order_meta_key, 'fail');
$order->save();
throw new Exception(__('Refund failed, error code:'.$result['respcd'].', error message:'.$result['resperr'], 'woocommerce'));
}
$order->update_meta_data($refund_order_meta_key, 'success');
$order->save();
error_log('refund finish');
return true;
}
}
?>