Alipay.php 4.7 KB

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