commit
a7d1340eec
|
|
@ -0,0 +1,183 @@
|
|||
<?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');
|
||||
|
||||
// Load the settings.
|
||||
$this->init_form_fields();
|
||||
$this->init_settings();
|
||||
|
||||
// Define user set variables
|
||||
$this->title = $this->get_option( 'title' );
|
||||
$this->description = $this->get_option( 'description' );
|
||||
$this->instructions = $this->get_option( 'instructions', $this->description );
|
||||
|
||||
// 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 );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'
|
||||
),
|
||||
'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 ) {
|
||||
$scoreBalance = huayi_score_balance($huayi_uid);//获取积分余额 TODO
|
||||
if ($scoreBalance >= $order->order_total ) {//积分够扣
|
||||
$scorePay = huayi_score_pay($huayi_uid,$order->order_total,'订单号:'.$order_id);//执行扣款 TODO
|
||||
|
||||
//支付成功
|
||||
if ( $scorePay['uid'] > 0 ) {//TODO 修正状态判断
|
||||
|
||||
//更新订单状态
|
||||
//$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 { //支付失败
|
||||
$error_message = '支付接口出错 - ' . $scorePay;
|
||||
wc_add_notice( __('结算失败:', 'huayi_score') . $error_message, 'error' );
|
||||
return;
|
||||
}
|
||||
} else { //积分不足
|
||||
$error_message = '积分余额不足.';
|
||||
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 ) {
|
||||
$result = huayi_score_refund($huayi_uid,$amount,'退款订单号:'.$order_id.'.退款原因:'.$reason);//执行退款
|
||||
|
||||
if ( $result['uid'] > 0 ) {//TODO 修正状态判断
|
||||
$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', '用户信息错误' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
/*
|
||||
* Plugin Name: Huayi-Score For WooCommerce
|
||||
* Plugin URI: http://www.huayizhiyun.com
|
||||
* Description:
|
||||
* Version: 1.0.0
|
||||
* Author: HITSword
|
||||
* Author URI: http://www.huayizhiyun.com
|
||||
* Requires at least: 3.9
|
||||
* Tested up to: 4.0
|
||||
*
|
||||
* Text Domain: huayi
|
||||
* Domain Path: /lang/
|
||||
*/
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
/**
|
||||
* 初始化插件
|
||||
*/
|
||||
function wc_huayi_score_gateway_init()
|
||||
{
|
||||
if( !class_exists('WC_Payment_Gateway') ) return;
|
||||
|
||||
load_plugin_textdomain( 'huayi_score', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
|
||||
|
||||
require_once( plugin_basename( 'class-wc-huayi-score.php' ) );
|
||||
add_filter('woocommerce_payment_gateways', 'woocommerce_huayi_score_add_gateway' );
|
||||
//add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wc_huayi_score_plugin_edit_link' );
|
||||
}
|
||||
|
||||
add_action( 'plugins_loaded', 'wc_huayi_score_gateway_init' );
|
||||
|
||||
/**
|
||||
* Add the gateway to WooCommerce
|
||||
*
|
||||
* @access public
|
||||
* @param array $methods
|
||||
* @package WooCommerce/Classes/Payment
|
||||
* @return array
|
||||
*/
|
||||
function woocommerce_huayi_score_add_gateway( $methods )
|
||||
{
|
||||
$methods[] = 'WC_Huayi_Score';
|
||||
return $methods;
|
||||
}
|
||||
Loading…
Reference in New Issue