PayController.php 5.9 KB
<?php

namespace app\user\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 _initialize(){
        parent::_initialize();
        require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Api.php";
        require_once EXTEND_PATH . "WxpayAPI/example/WxPay.JsApiPay.php";
        require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Notify.php";
        require_once EXTEND_PATH . "WxpayAPI/lib/WxPay.Merch.php";
        require_once EXTEND_PATH . "WxpayAPI/example/log.php";
    }

    /**
     * 微信支付
     * @throws \WxPayException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        //todo 支付逻辑处理
        $order_sn=cmf_get_order_sn();
        $tools = new \JsApiPay();
        $user=cmf_get_current_user();
        if(empty($user)){
            $this->error('用户尚未登录');
        }
        $openId = $user['openid'];
        if(empty($openId)){
            $this->error('微信OPENID不能为空');
        }
        $real_price=0.01;//测试金额
        $total_fee=floatval($real_price)*100;
        if($total_fee<=0){
            $this->error('支付金额不能小于0');
        }

        $timeStamp = time();
        $randomStr = cmf_random_string(8);
        $arithmetic['timeStamp']= $timeStamp;
        $arithmetic['randomStr']= $randomStr;
        $arithmetic['orderSn']= $order_sn;
        $signature = arithmetic($arithmetic);
        $notifyParam=array('t'=>$timeStamp,'r'=>$randomStr,'s'=>$signature,'o'=>$order_sn);
        $url=url('portal/pay/notify',$notifyParam,true,true);
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("服务器续费");
        $input->SetAttach($order_sn);
        $input->SetOut_trade_no($order_sn);
        $input->SetTotal_fee($total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($total_fee);
        $input->SetNotify_url($url);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);

        $order = \WxPayApi::unifiedOrder($input);

        if(!empty($order['return_code'])){
            if($order['return_code']=='FAIL'){
                $this->error('支付参数错误','',$order);
            }
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $this->success('获取支付参数成功','',json_decode($jsApiParameters,true));
    }

    /**
     * 支付回调
     */
    public function notify(){
        $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){
            $data['code'] = 40002;
            $data['msg'] = '签名验证失败';
            Log::write('签名验证失败'.$param['o']);
            exit;
        }
        $notify = new \WxPayNotify();
        $notify->Handle(false);
        $xml = file_get_contents("php://input");
        $base = new \WxPayResults();
        $data = $base->FromXml($xml);
        if($base->CheckSign() == true){
            if ($data["return_code"] == "FAIL") {
                Log::write('支付失败1','error');
            }elseif($data["result_code"] == "FAIL"){
                Log::write('支付失败2','error');
            }else{
                //todo 回调处理
            }
        }
    }

    /**
     * 退款
     * @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']);
        }

    }

}