218 lines
8.5 KiB
PHP
218 lines
8.5 KiB
PHP
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
/**
|
|
* Huayi-Score Payment Gateway.
|
|
* @class WC_Huayi_Score
|
|
* @extends WC_Payment_Gateway
|
|
* @package
|
|
* @author hitsword
|
|
*/
|
|
class WC_Huayi_Score extends WC_Payment_Gateway {
|
|
|
|
/**
|
|
* WC_Huayi_Score constructor.
|
|
* 初始化
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->id = 'huayi_score';
|
|
$this->icon = apply_filters('woocommerce_huayi_score_icon', '');
|
|
$this->has_fields = true;
|
|
$this->method_title = __( '积分支付', 'huayi_score' );
|
|
$this->method_description = __( '调用系统积分数据进行支付.', 'huayi_score' );
|
|
$this->supports = array('refunds');
|
|
|
|
// 加载配置.
|
|
$this->init_form_fields();
|
|
$this->init_settings();
|
|
|
|
// 定义用户设置的变量.
|
|
$this->title = $this->get_option( 'title' );
|
|
$this->description = $this->get_option( 'description' );
|
|
$this->instructions = $this->get_option( 'instructions', $this->description );
|
|
$this->apiurl = $this->get_option( 'apiurl' );
|
|
$this->token = $this->get_option( 'token' );
|
|
|
|
// Actions
|
|
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
|
|
add_action( 'woocommerce_thankyou_huayi_score', array( $this, 'thankyou_page' ) );
|
|
|
|
// Customer Emails
|
|
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
|
|
|
|
//引入积分SDK
|
|
require_once(plugin_basename('includes' . DIRECTORY_SEPARATOR . 'huayi-score-sdk.php'));
|
|
}
|
|
|
|
/**
|
|
* Initialise Gateway Settings Form Fields.
|
|
*/
|
|
public function init_form_fields()
|
|
{
|
|
$this->form_fields = array(
|
|
'enabled' => array(
|
|
'title' => __( '开/关', 'huayi_score' ),
|
|
'type' => 'checkbox',
|
|
'label' => __( '开启积分支付', 'huayi_score' ),
|
|
'default' => 'yes'
|
|
),
|
|
'apiurl' => array(
|
|
'title' => __( 'API地址', 'huayi_score' ),
|
|
'type' => 'text',
|
|
'description' => __( '对接API的地址.', 'huayi_score' ),
|
|
'default' => __( 'https://test.com', 'huayi_score' ),
|
|
'desc_tip' => true,
|
|
),
|
|
'token' => array(
|
|
'title' => __( 'Token', 'huayi_score' ),
|
|
'type' => 'text',
|
|
'description' => __( 'API Token.', 'huayi_score' ),
|
|
'default' => __( 'token', 'huayi_score' ),
|
|
'desc_tip' => true,
|
|
),
|
|
'title' => array(
|
|
'title' => __( '名称', 'huayi_score' ),
|
|
'type' => 'text',
|
|
'description' => __( '客户在结算时显示的支付方式名称.', 'huayi_score' ),
|
|
'default' => __( '积分支付', 'huayi_score' ),
|
|
'desc_tip' => true,
|
|
),
|
|
'description' => array(
|
|
'title' => __( '描述', 'huayi_score' ),
|
|
'type' => 'textarea',
|
|
'description' => __( '在订单结算时显示的支付接口描述.', 'huayi_score' ),
|
|
'default' => __( '使用积分结算.', 'huayi_score' ),
|
|
'desc_tip' => true,
|
|
),
|
|
'instructions' => array(
|
|
'title' => __( '说明', 'huayi_score' ),
|
|
'type' => 'textarea',
|
|
'description' => __( '说明将被添加到感谢页面.', 'huayi_score' ),
|
|
'default' => __( '使用积分结算.', 'huayi_score' ),
|
|
'desc_tip' => true,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Output for the order received page.
|
|
*/
|
|
public function thankyou_page()
|
|
{
|
|
if ( $this->instructions )
|
|
echo wpautop( wptexturize( $this->instructions ) );
|
|
}
|
|
|
|
/**
|
|
* Add content to the WC emails.
|
|
*
|
|
* @access public
|
|
* @param WC_Order $order
|
|
* @param bool $sent_to_admin
|
|
* @param bool $plain_text
|
|
*/
|
|
public function email_instructions( $order, $sent_to_admin, $plain_text = false )
|
|
{
|
|
if ( $this->instructions && ! $sent_to_admin && 'huayi_score' === $order->payment_method && $order->has_status( 'on-hold' ) ) {
|
|
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 发起支付
|
|
*
|
|
* @param int $order_id
|
|
* @return array
|
|
*/
|
|
public function process_payment( $order_id )
|
|
{
|
|
$order = wc_get_order( $order_id );//获取订单
|
|
$huayi_uid = get_user_meta( $order->get_user_id() , 'huayi_uid',true );//获取登录接口后的uid
|
|
// $huayi_openid = get_user_meta( $order->get_user_id() , 'huayi_openid',true );//获取登录接口后的openid
|
|
// $huayi_unionid = get_user_meta( $order->get_user_id() , 'huayi_unionid',true );//获取登录接口后的unionid
|
|
|
|
if ( $huayi_uid > 0 ) {
|
|
$huayiScoreSdk = new HuayiScoreSdk(array('apiurl'=>$this->apiurl,'token'=>$this->token));
|
|
$balanceResult = $huayiScoreSdk->getBalance($huayi_uid);//获取积分余额
|
|
if (!$balanceResult) {
|
|
wc_add_notice( __('查询积分失败:', 'huayi_score') . $huayiScoreSdk->getError(), 'error' );
|
|
return;
|
|
}
|
|
|
|
if ($balanceResult['data']['score'] >= $order->order_total ) {//积分够扣
|
|
$payResult = $huayiScoreSdk->pay($huayi_uid, $order_id, $order->order_total,get_bloginfo('name').'订单号:'.$order_id);//执行扣款
|
|
if (!$payResult) {
|
|
wc_add_notice( __('结算失败:', 'huayi_score') . $huayiScoreSdk->getError(), 'error' );
|
|
return;
|
|
}
|
|
|
|
//支付成功
|
|
if ( $payResult['data']['pay_status'] == 'success' ) {
|
|
|
|
//更新订单状态
|
|
//$order->update_status( 'on-hold', __( '等待积分支付', 'huayi_score' ) );//需要支付回调的接口才需要
|
|
|
|
//更新订单状态
|
|
$order->payment_complete();//支付成功
|
|
|
|
// 更新库存
|
|
$order->reduce_order_stock();
|
|
// 清空购物车
|
|
WC()->cart->empty_cart();
|
|
// 返回感谢页面
|
|
return array(
|
|
'result' => 'success',
|
|
'redirect' => $this->get_return_url( $order )
|
|
);
|
|
} else { //支付失败
|
|
wc_add_notice( __('结算失败:', 'huayi_score') . $payResult, 'error' );
|
|
return;
|
|
}
|
|
} else { //积分不足
|
|
$error_message = sprintf( __('积分余额不足.剩余%s积分.'), $balanceResult['data']['score']);
|
|
wc_add_notice( __('结算失败:', 'huayi_score') . $error_message, 'error' );
|
|
return;
|
|
}
|
|
} else {
|
|
$error_message = '用户资料故障,请联系客服。';
|
|
wc_add_notice( __('结算失败:', 'huayi_score') . $error_message, 'error' );
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 发起退还
|
|
* @param int $order_id
|
|
* @param float $amount
|
|
* @param string $reason
|
|
* @return bool True or false based on success, or a WP_Error object
|
|
*/
|
|
public function process_refund( $order_id, $amount = null, $reason = '' )
|
|
{
|
|
$order = wc_get_order( $order_id );
|
|
$huayi_uid = get_user_meta( $order->get_user_id() , 'huayi_uid',true );
|
|
|
|
if ( $huayi_uid > 0 ) {
|
|
$huayiScoreSdk = new HuayiScoreSdk(array('apiurl'=>$this->apiurl,'token'=>$this->token));
|
|
$result = $huayiScoreSdk->refund($huayi_uid,$amount,get_bloginfo('name').'退还订单号:'.$order_id.'.退还原因:'.$reason);//执行退还
|
|
|
|
if (!$result) {
|
|
wc_add_notice( __('退还失败:', 'huayi_score') . $huayiScoreSdk->getError(), 'error' );
|
|
return;
|
|
}
|
|
|
|
if ( $result['data']['refund_status'] == 'success' ) {
|
|
$order->add_order_note( sprintf( __( '退还时间: %s - 退还会员ID: %s', 'huayi_score' ), date("Y-m-d H:i:s"),$result['uid'] ) );
|
|
return true;
|
|
} else {
|
|
return new WP_Error( 'error', $result );
|
|
}
|
|
} else {
|
|
return new WP_Error( 'error', '用户信息错误' );
|
|
}
|
|
}
|
|
}
|