Payment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. $model = new BillModel();
  58. $bill = $model->getDetail(['id'=>$params['bill_id']]);
  59. if (!$bill || $bill['status'] !== 0) {
  60. return $this->error('订单已支付');
  61. }
  62. if (!$model->where('id',$params['bill_id'])->update(['pay_type'=>$params['pay_type'],'platform'=>$params['platform']])){
  63. return $this->error('操作失败');
  64. }
  65. // 拉起支付 余额支付
  66. $log_type = Wallet::BILL_LOG_TYPE[$bill['table_name']];
  67. $remark = Wallet::log_type[$log_type];
  68. if ($params['pay_type'] == 'wallet') {
  69. Db::startTrans();
  70. //钱包更新
  71. $walletService = new Wallet();
  72. if (!$walletService->change($user_id, -$bill['pay_amount'], 'money', $log_type, $remark, $bill['table_name'], $bill['table_id'])) {
  73. Db::rollback();
  74. return $this->error($walletService->getMessage());
  75. }
  76. // 支付成功,更改支付状态
  77. $paymentBusiness = new PaymentBusiness();
  78. if (!$paymentBusiness->deal($bill['order_no'])){
  79. Db::rollback();
  80. return $this->error($paymentBusiness->getMessage());
  81. }
  82. Db::commit();
  83. return $this->success('支付成功');
  84. }
  85. // 第三方支付下单
  86. $params = [
  87. 'type' => $params['pay_type'],
  88. 'orderid' => $bill['order_no'],
  89. 'title' => $remark,
  90. 'amount' => $bill['pay_amount'],
  91. 'method' => $params['platform'],
  92. 'notifyurl' => config('pay_notify_url') . "/api/notify/bill/pay_type/{$params['pay_type']}",
  93. 'returnurl' => '',
  94. ];
  95. // 如果是小程序则需要添加 openid
  96. if ($params['pay_type'] == 'wechat' && $params['platform'] == 'miniapp') {
  97. $params['openid'] = $this->auth->mini_openid;
  98. }
  99. $res = Service::submitOrder($params);
  100. if ($params['pay_type'] == 'wechat') {
  101. return $this->success('下单成功', json_decode($res, true));
  102. }
  103. return $this->success('下单成功', $res);
  104. }
  105. }