Wxpay.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\admin\controller\base\pay;
  3. /**
  4. * @title 微信支付控制器
  5. * @desc
  6. * @author Rock
  7. * @version 1.0
  8. * @icon fa fa-cny
  9. */
  10. use app\admin\controller\Base;
  11. class Wxpay extends Confirmpay
  12. {
  13. protected $noNeedLogin = ['callback','refund','test'];
  14. protected $noNeedAuth = ['pay'];
  15. protected $model = null;
  16. protected $WxminModel = null;
  17. protected $payrecordModel = null;
  18. protected $orderModel = null;
  19. protected $paytype = 2;//微信支付
  20. protected $ptypeList;
  21. public function initialize()
  22. {
  23. parent::initialize();
  24. $this->model = new \weixin\Wxpay;
  25. $this->WxminModel = new \weixin\Wxmin;
  26. $this->payrecordModel = new \app\common\model\base\pay\Payrecord;
  27. $this->orderModel = new \app\common\model\ticket\Order;
  28. $this->ptypeList = $this->payrecordModel->getPtypeList();
  29. }
  30. /**
  31. * @title 创建支付流水并统一下单
  32. * @desc 说明
  33. * @method GET/POST
  34. * @param {string} {oid} {} {订单号}
  35. * @param {int} {ptype} {0} {订单类型,1=站务系统购票,2=定制客运购票}
  36. * @param {string} {code} {} {微信授权临时凭证}
  37. * @return {type} {name} {default} {desc}
  38. * @author Rock
  39. */
  40. public function pay($code="",$oid="",$ptype=0)
  41. {
  42. if(empty($code) || empty($oid) || empty($ptype)){
  43. ShowJson(1,"缺少必要参数");
  44. }
  45. //获取OPENID
  46. $openidInfo = $this->WxminModel->getopenid($code);
  47. if($openidInfo['code']==1){
  48. $openid = $openidInfo['data']['openid'];
  49. }else{
  50. return res($openidInfo['code'],$openidInfo['msg']);
  51. }
  52. $poid = CreateOrderID();
  53. $ptypeText = !empty($this->ptyleList[$ptype])?$this->ptyleList[$ptype]:"";
  54. $where = [];
  55. $where['orderNo'] = ['=',$oid];
  56. $where['status'] = ['=',1];//车票订单待支付
  57. $orderInfo = $this->orderModel->where($where)->find();
  58. if(empty($orderInfo))ShowJson(1,"没有找到待支付订单");
  59. $amount = $orderInfo->actmoney;
  60. if(empty($amount)){
  61. ShowJson(1,"支付金额小于0.01元");
  62. }
  63. if($ptype==1){
  64. $msg = "站务系统订单".$oid."支付";
  65. }elseif($ptype==2){
  66. $msg = "定制客运订单".$oid."支付";
  67. }
  68. //创建支付订单
  69. $insertData = [
  70. 'oid' => $oid,
  71. 'uid' => $this->userinfo['user_id'],
  72. 'type' => $this->paytype,
  73. 'msg' => $msg,
  74. 'amount'=> $amount,
  75. 'status'=> 0,//支付订单待支付
  76. 'ptype' => $ptype,
  77. 'poid' => $poid
  78. ];
  79. $res = $this->payrecordModel->save($insertData);
  80. //调用微信支付统一下单接口,返回供小程序端支付需要的参数
  81. $data = $this->model->JsApiPay($poid,$amount,$openid,$ptypeText."订单".$oid."支付");
  82. ShowJson(0,'支付请求已发起',$data);
  83. }
  84. //微信支付回调
  85. public function callback()
  86. {
  87. $postStr = file_get_contents("php://input");
  88. $result=XmlToArray($postStr);
  89. $res = null;
  90. if($this->model->Callback_SignCheck($result)){
  91. WLog('Wxpay_Callback','回调校验成功!:'.array2string($result));
  92. //支付订单本地流水号
  93. $poid=$result['out_trade_no'];
  94. //支付交易号
  95. $tran_id=$result['transaction_id'];
  96. //支付通道类型
  97. $trade_type=$result['trade_type'];
  98. //调用支付确认(成功支付后的业务逻辑)
  99. if($this->ConfirmPay($poid,$tran_id,$trade_type)){
  100. WLog('Wxpay_Callback','确认支付成功! ');
  101. $xmlRes = $this->model->Return_Result('SUCCESS','ok');
  102. }else{
  103. WLog('Wxpay_Callback','确认支付失败!');
  104. $xmlRes = $this->model->Return_Result('FAIL','fail');
  105. }
  106. return response()->data($xmlRes)->contentType('text/xml');
  107. }else{
  108. WLog('Wxpay_Callback','回调校验失败!:'.array2string($result));
  109. $xmlRes = $this->model->Return_Result('FAIL','fail');
  110. return response()->data($xmlRes)->contentType('text/xml');
  111. }
  112. }
  113. public function refund($tran_id='',$amount=0,$refund=0,$msg = '开发测试')
  114. {
  115. $res = $this->model->Refund($tran_id,$refund,$amount,$msg);
  116. return json($res);
  117. }
  118. }