PayController.php 2.7 KB
<?php

namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use think\Log;
use think\Validate;

/**
 * 微信支付,退款,提现DEMO
 * Class PayController
 * @package app\portal\controller
 */
class PayController extends HomeBaseController
{

    /**
     * 微信支付
     */
    public function index(){

    }

    /**
     * 支付回调
     */
    public function notify(){

    }

    /**
     * 退款
     * @throws \WxPayException
     */
    public function refund(){
        $param           = $this->request->param();
        $signature = $param['s'];
        $arithmetic['timeStamp']= $param['t'];
        $arithmetic['randomStr']= $param['r'];
        $arithmetic['orderSn']= $param['o'];
        $str = arithmetic($arithmetic);
        if($str != $signature){
            $this->error('签名验证失败');
        }

        /*微信退款*/
        require_once EXTEND_PATH."WxpayAPI/lib/WxPay.Api.php";
        require_once EXTEND_PATH."WxpayAPI/example/log.php";
        if(isset($info["transaction_id"]) && empty($info["transaction_id"])){
            $transaction_id = $info["transaction_id"];
            $total_fee = $info["real_price"]*100;
            $refund_fee = $info["real_price"]*100;
            $input = new \WxPayRefund();
            $input->SetTransaction_id($transaction_id);
            $input->SetTotal_fee($total_fee);
            $input->SetRefund_fee($refund_fee);
            $input->SetOut_refund_no(\WxPayConfig::MCHID.date("YmdHis"));
            $input->SetOp_user_id(\WxPayConfig::MCHID);
            $ret=\WxPayApi::refund($input);
            if($ret['result_code']=='SUCCESS'){
                //todo 退款成功处理
                $this->success('退款成功');
            }else{
                $this->error("退款失败:".$ret['err_code_des']);
            }
        }else{
            $this->error('缺少退款标识');
        }

    }

    /**
     * 提现
     */
    public function withdraw_cash(){
        $param           = $this->request->param();
        $signature = $param['s'];
        $arithmetic['timeStamp']= $param['t'];
        $arithmetic['randomStr']= $param['r'];
        $arithmetic['orderSn']= $param['o'];
        $str = arithmetic($arithmetic);
        if($str != $signature){
            $this->error('签名验证失败');
        }
        $price=1;
        $openid='';

        $merch=new \MerchPay();
        $trade_no = cmf_get_order_sn();
        $res=$merch->pay($openid,$trade_no,$price,'提现');
        if($res['result_code']=='SUCCESS'){
            //todo 提现成功处理
        }else{
            $this->error('操作失败:'.$res['return_msg']);
        }

    }

}