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' ), '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'=>'1','token'=>'1')); $balanceResult = $huayiScoreSdk->getBalance($huayi_uid);//获取积分余额 TODO wc_add_notice( __('结算失败:', 'huayi_score') . $balanceResult, 'error' ); return;//debug; if ($balanceResult >= $order->order_total ) {//积分够扣 $payResult = $huayiScoreSdk->pay($huayi_uid,$order->order_total,'订单号:'.$order_id);//执行扣款 TODO //支付成功 if ( $payResult['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 = '支付接口出错 - ' . $payResult; 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 ) { $huayiScoreSdk = new HuayiScoreSdk(array('apiurl'=>'1','token'=>'1')); $result = $huayiScoreSdk->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', '用户信息错误' ); } } }