Unionpay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Unionpay extends Confirmpay
  12. {
  13. protected $noNeedLogin = ['callback','pay'];
  14. protected $model = null;
  15. protected $payrecordModel = null;
  16. protected $orderModel = null;
  17. protected $balanceorderModel = null;
  18. protected $safetyorderModel = null;
  19. protected $companycourseorderModel = null;
  20. protected $resitorderModel = null;
  21. protected $paytype = 3;//银联支付
  22. public function initialize()
  23. {
  24. parent::initialize();
  25. $this->model = new \app\extend\unionpay\Unionpay;
  26. $this->payrecordModel = new \app\common\model\pay\Payrecord;
  27. $this->orderModel = new \app\common\model\order\Courseorder;
  28. $this->balanceorderModel = new \app\common\model\order\Balanceorder;
  29. $this->safetyorderModel = new \app\common\model\order\Safetyorder;
  30. $this->companycourseorderModel = new \app\common\model\order\Companycourseorder;
  31. $this->resitorderModel = new \app\common\model\order\Resitorder;
  32. $this->ptypeList = $this->payrecordModel->getPtypeList();
  33. }
  34. /**
  35. * @title 创建支付流水并统一下单
  36. * @desc 说明
  37. * @method GET/POST
  38. * @param {string} {oid} {} {订单号}
  39. * @param {int} {ptype} {0} {订单类型:1=课程订单,2=余额充值,3=企业安全教育,4=企业批量购买课程,5=补考缴费}
  40. * @param {string} {client} {page} {客户端:app=手机端,web=PC端}
  41. * @return {type} {name} {default} {desc}
  42. * @author Rock
  43. */
  44. public function pay($oid="",$ptype=0,$client="web")
  45. {
  46. if(empty($oid))ShowJson(1,"缺少必要参数");
  47. if(empty($ptype))ShowJson(1,"缺少必要参数");
  48. $poid = CreateOrderID();
  49. $ptypeText = !empty($this->ptyleList[$ptype])?$this->ptyleList[$ptype]:"";
  50. if($ptype==1){//课程订单支付
  51. $where = [];
  52. $where['oid'] = ['=',$oid];
  53. $where['status'] = ['=',1];
  54. $orderInfo = $this->orderModel->where($where)->find();
  55. if(empty($orderInfo))ShowJson(1,"没有找到待支付订单");
  56. $amount = $orderInfo->pay_amount;
  57. $msg = "购买订单".$oid."支付";
  58. }elseif($ptype==2){//余额充值
  59. $where = [];
  60. $where['oid'] = ['=',$oid];
  61. $where['status'] = ['=',1];
  62. $orderInfo = $this->balanceorderModel->where($where)->find();
  63. $amount = $orderInfo->pay_amount;
  64. $msg = "充值订单".$oid."支付";
  65. }elseif($ptype==3){//企业安全教育课程
  66. $where = [];
  67. $where['oid'] = ['=',$oid];
  68. $where['status'] = ['=',1];
  69. $orderInfo = $this->safetyorderModel->where($where)->find();
  70. $amount = $orderInfo->pay_amount;
  71. $msg = "企业安全教育订单".$oid."支付";
  72. }elseif($ptype==4){
  73. $where = [];
  74. $where['oid'] = ['=',$oid];
  75. $where['status'] = ['=',1];
  76. $orderInfo = $this->companycourseorderModel->where($where)->find();
  77. $amount = $orderInfo->pay_amount;
  78. $msg = "企业批量购买课程订单".$oid."支付";
  79. }elseif($ptype==5){
  80. $where = [];
  81. $where['oid'] = ['=',$oid];
  82. $where['status'] = ['=',1];
  83. $orderInfo = $this->resitorderModel->where($where)->find();
  84. $amount = $orderInfo->pay_amount;
  85. $msg = "补考缴费订单".$oid."支付";
  86. }
  87. //创建支付订单
  88. $insertData = [
  89. 'oid' => $oid,
  90. 'uid' => $orderInfo->uid,
  91. 'type' => $this->paytype,
  92. 'msg' => $msg,
  93. 'amount'=> $amount,
  94. 'status'=> 0,
  95. 'ptype' => $ptype,
  96. 'poid' => $poid
  97. ];
  98. if(empty($insertData['amount'])){
  99. ShowJson(1,"支付金额小于0.01元");
  100. }
  101. $res = $this->payrecordModel->create($insertData);
  102. //调用支付宝支付统一下单接口,返回供支付宝客户端支付需要的参数
  103. $data = $this->model->pay($poid,$amount,$client);
  104. ShowJson(0,'支付请求已发起',$data);
  105. }
  106. //银联支付回调
  107. public function callback()
  108. {
  109. $data = $this->request->request();
  110. //支付成功或交易完成
  111. if($this->model->callback_validate($data)){
  112. WLog('Unionpay_Callback','回调校验成功!:'.array2string($result));
  113. $poid = $data['orderId'];
  114. $tran_id = $data['queryId'];
  115. if($this->ConfirmPay($poid,$tran_id)){
  116. WLog("Unionpay_Callback","支付成功,业务处理成功");
  117. }else{
  118. WLog("Unionpay_Callback","支付成功,业务处理失败");
  119. }
  120. }
  121. echo "ok";
  122. }
  123. }