123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace app\admin\controller\base\pay;
- /**
- * @title 微信支付控制器
- * @desc
- * @author Rock
- * @version 1.0
- * @icon fa fa-cny
- */
- use app\admin\controller\Base;
- class Wxpay extends Confirmpay
- {
- protected $noNeedLogin = ['callback','refund','test'];
- protected $noNeedAuth = ['pay'];
- protected $model = null;
- protected $WxminModel = null;
- protected $payrecordModel = null;
- protected $orderModel = null;
- protected $paytype = 2;//微信支付
- protected $ptypeList;
- public function initialize()
- {
- parent::initialize();
- $this->model = new \weixin\Wxpay;
- $this->WxminModel = new \weixin\Wxmin;
- $this->payrecordModel = new \app\common\model\base\pay\Payrecord;
- $this->orderModel = new \app\common\model\ticket\Order;
- $this->ptypeList = $this->payrecordModel->getPtypeList();
- }
- /**
- * @title 创建支付流水并统一下单
- * @desc 说明
- * @method GET/POST
- * @param {string} {oid} {} {订单号}
- * @param {int} {ptype} {0} {订单类型,1=站务系统购票,2=定制客运购票}
- * @param {string} {code} {} {微信授权临时凭证}
- * @return {type} {name} {default} {desc}
- * @author Rock
- */
- public function pay($code="",$oid="",$ptype=0)
- {
- if(empty($code) || empty($oid) || empty($ptype)){
- ShowJson(1,"缺少必要参数");
- }
- //获取OPENID
- $openidInfo = $this->WxminModel->getopenid($code);
- if($openidInfo['code']==1){
- $openid = $openidInfo['data']['openid'];
- }else{
- return res($openidInfo['code'],$openidInfo['msg']);
- }
- $poid = CreateOrderID();
- $ptypeText = !empty($this->ptyleList[$ptype])?$this->ptyleList[$ptype]:"";
- $where = [];
- $where['orderNo'] = ['=',$oid];
- $where['status'] = ['=',1];//车票订单待支付
- $orderInfo = $this->orderModel->where($where)->find();
- if(empty($orderInfo))ShowJson(1,"没有找到待支付订单");
- $amount = $orderInfo->actmoney;
- if(empty($amount)){
- ShowJson(1,"支付金额小于0.01元");
- }
- if($ptype==1){
- $msg = "站务系统订单".$oid."支付";
- }elseif($ptype==2){
- $msg = "定制客运订单".$oid."支付";
- }
- //创建支付订单
- $insertData = [
- 'oid' => $oid,
- 'uid' => $this->userinfo['user_id'],
- 'type' => $this->paytype,
- 'msg' => $msg,
- 'amount'=> $amount,
- 'status'=> 0,//支付订单待支付
- 'ptype' => $ptype,
- 'poid' => $poid
- ];
- $res = $this->payrecordModel->save($insertData);
- //调用微信支付统一下单接口,返回供小程序端支付需要的参数
- $data = $this->model->JsApiPay($poid,$amount,$openid,$ptypeText."订单".$oid."支付");
- ShowJson(0,'支付请求已发起',$data);
- }
- //微信支付回调
- public function callback()
- {
- $postStr = file_get_contents("php://input");
- $result=XmlToArray($postStr);
- $res = null;
- if($this->model->Callback_SignCheck($result)){
- WLog('Wxpay_Callback','回调校验成功!:'.array2string($result));
- //支付订单本地流水号
- $poid=$result['out_trade_no'];
- //支付交易号
- $tran_id=$result['transaction_id'];
- //支付通道类型
- $trade_type=$result['trade_type'];
- //调用支付确认(成功支付后的业务逻辑)
- if($this->ConfirmPay($poid,$tran_id,$trade_type)){
- WLog('Wxpay_Callback','确认支付成功! ');
- $xmlRes = $this->model->Return_Result('SUCCESS','ok');
- }else{
- WLog('Wxpay_Callback','确认支付失败!');
- $xmlRes = $this->model->Return_Result('FAIL','fail');
- }
- return response()->data($xmlRes)->contentType('text/xml');
- }else{
- WLog('Wxpay_Callback','回调校验失败!:'.array2string($result));
- $xmlRes = $this->model->Return_Result('FAIL','fail');
- return response()->data($xmlRes)->contentType('text/xml');
- }
- }
- public function refund($tran_id='',$amount=0,$refund=0,$msg = '开发测试')
- {
- $res = $this->model->Refund($tran_id,$refund,$amount,$msg);
- return json($res);
- }
- }
|