Payment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\common\business\PaymentBusiness;
  5. use app\common\controller\Api;
  6. use app\common\model\BillModel;
  7. use app\common\model\HotelModel;
  8. use app\common\model\HotelOrderModel;
  9. use app\common\model\HotelRoomModel;
  10. use app\common\model\PayOrderModel;
  11. use app\common\model\UniversityEventModel;
  12. use app\common\model\Wallet;
  13. use app\utils\CurlUtil;
  14. use app\utils\Service\Tencent\TencentIm;
  15. use think\Db;
  16. /**
  17. * 老年大学 活动板块
  18. */
  19. class Payment extends Api
  20. {
  21. protected $noNeedLogin = [''];
  22. protected $noNeedRight = ['*'];
  23. /**
  24. * order_no 下级的订单表 订单号
  25. * order_type 订单来源:hotel_order=酒店订单;hotel_canteen_order=餐厅订单;university_event_apply=活动订单;offline_shop_order=线下订单
  26. * @return true
  27. */
  28. public function page()
  29. {
  30. $user_id = $this->auth->id;
  31. $params = $this->request->param();
  32. if (empty($params['order_no']) || empty($params['order_type'])) {
  33. return $this->error('请选择订单');
  34. }
  35. $tableName = $params['order_type'];
  36. $orderNo = $params['order_no'];
  37. $paymentBusiness = new PaymentBusiness();
  38. if (!$paymentBusiness->createOrder($user_id,$orderNo,$tableName)){
  39. return $this->error($paymentBusiness->getMessage(),$paymentBusiness->getData());
  40. }
  41. // 支付订单ID
  42. $bill_id = $paymentBusiness->getData()['bill_id'];
  43. // 获取用户余额
  44. $wallet = (new Wallet())->getWallet($user_id);
  45. return $this->success('获取成功',['bill_id'=>$bill_id,'wallet'=>$wallet]);
  46. }
  47. public function submit()
  48. {
  49. $user_id = $this->auth->id;
  50. $params = $this->request->param();
  51. if (empty($params['bill_id'])) {
  52. return $this->error('请选择订单');
  53. }
  54. if (empty($params['pay_type']) || empty($params['platform'])) {
  55. return $this->error('请选择支付方式');
  56. }
  57. $payType = $params['pay_type'];
  58. $platform = $params['platform'];
  59. $model = new BillModel();
  60. $bill = $model->getDetail(['id'=>$params['bill_id']]);
  61. if (!$bill || $bill['status'] !== 0) {
  62. return $this->error('订单已支付');
  63. }
  64. if (!$model->where('id',$bill['id'])->update(['pay_type'=>$payType,'platform'=>$platform])){
  65. return $this->error('操作失败');
  66. }
  67. // 拉起支付 余额支付
  68. $log_type = Wallet::BILL_LOG_TYPE[$bill['table_name']];
  69. $remark = Wallet::log_type[$log_type];
  70. if ($params['pay_type'] == 'wallet') {
  71. Db::startTrans();
  72. //钱包更新
  73. $walletService = new Wallet();
  74. if (!$walletService->change($user_id, -$bill['pay_amount'], 'money', $log_type, $remark, $bill['table_name'], $bill['table_id'])) {
  75. Db::rollback();
  76. return $this->error($walletService->getMessage());
  77. }
  78. // 支付成功,更改支付状态
  79. $paymentBusiness = new PaymentBusiness();
  80. if (!$paymentBusiness->deal($bill['order_no'])){
  81. Db::rollback();
  82. return $this->error($paymentBusiness->getMessage());
  83. }
  84. Db::commit();
  85. return $this->success('支付成功');
  86. }
  87. // 第三方支付下单
  88. $params = [
  89. 'type' => $payType,
  90. 'orderid' => $bill['order_no'],
  91. 'title' => $remark,
  92. 'amount' => $bill['pay_amount'],
  93. 'method' => $platform,
  94. 'notifyurl' => config('pay_notify_url') . "/api/notify/bill/pay_type/{$payType}",
  95. 'returnurl' => '',
  96. ];
  97. // 如果是小程序则需要添加 openid
  98. if ($payType == 'wechat' && $platform == 'miniapp') {
  99. $params['openid'] = $this->auth->mini_openid;
  100. }
  101. $res = Service::submitOrder($params);
  102. if ($payType == 'wechat') {
  103. return $this->success('下单成功', json_decode($res, true));
  104. }
  105. return $this->success('下单成功', $res);
  106. }
  107. }