PlanOrder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\api\model\plus\live;
  3. use app\api\service\order\PaymentService;
  4. use app\api\service\order\paysuccess\type\PlanPaySuccessService;
  5. use app\common\enum\order\OrderPayTypeEnum;
  6. use app\common\enum\order\OrderTypeEnum;
  7. use app\common\exception\BaseException;
  8. use app\common\model\plus\live\PlanOrder as PlanOrderModel;
  9. use app\common\model\plus\live\Plan as PlanModel;
  10. /**
  11. * 礼物模型
  12. */
  13. class PlanOrder extends PlanOrderModel
  14. {
  15. /**
  16. * 创建订单
  17. */
  18. public function createOrder($user, $plan_id, $pay_type)
  19. {
  20. $plan = PlanModel::detail($plan_id);
  21. // 余额支付标记订单已支付
  22. if ($pay_type == OrderPayTypeEnum::BALANCE) {
  23. // 验证余额支付时用户余额是否满足
  24. if ($user['balance'] < $plan['money']) {
  25. $this->error = '用户余额不足,无法使用余额支付';
  26. return false;
  27. }
  28. }
  29. // 获取订单数据
  30. $data = [
  31. 'order_no' => 'GIFT' . $this->orderNo(),
  32. 'user_id' => $user['user_id'],
  33. 'plan_id' => $plan_id,
  34. 'plan_name' => $plan['plan_name'],
  35. 'pay_price' => $plan['money'],
  36. 'gift_money' => $plan['gift_money'],
  37. 'give_money' => $plan['give_money'],
  38. 'total_money' => sprintf('%.2f', $plan['gift_money'] + $plan['give_money']),
  39. 'app_id' => self::$app_id
  40. ];
  41. $status = $this->save($data);
  42. // 余额支付标记订单已支付
  43. if ($status && $pay_type == OrderPayTypeEnum::BALANCE) {
  44. $this->onPaymentByBalance($this['order_no']);
  45. }
  46. return $this['order_id'];
  47. }
  48. /**
  49. * 余额支付标记订单已支付
  50. */
  51. public function onPaymentByBalance($orderNo)
  52. {
  53. // 获取订单详情
  54. $PaySuccess = new PlanPaySuccessService($orderNo);
  55. // 发起余额支付
  56. $status = $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE);
  57. if (!$status) {
  58. $this->error = $PaySuccess->getError();
  59. }
  60. return $status;
  61. }
  62. /**
  63. * 待支付订单详情
  64. */
  65. public static function getPayDetail($orderNo)
  66. {
  67. $model = new static();
  68. return $model->where(['order_no' => $orderNo, 'pay_status' => 10])->with(['user'])->find();
  69. }
  70. /**
  71. * 订单详情
  72. */
  73. public static function getUserOrderDetail($order_id, $user_id)
  74. {
  75. $model = new static();
  76. $order = $model->where(['order_id' => $order_id, 'user_id' => $user_id])->find();
  77. if (empty($order)) {
  78. throw new BaseException(['msg' => '订单不存在']);
  79. }
  80. return $order;
  81. }
  82. /**
  83. * 构建支付请求的参数
  84. */
  85. public static function onOrderPayment($user, $order, $payType, $pay_source)
  86. {
  87. //如果来源是h5,首次不处理,payH5再处理
  88. if($pay_source == 'h5'){
  89. return [];
  90. }
  91. if ($payType == OrderPayTypeEnum::WECHAT) {
  92. return self::onPaymentByWechat($user, $order, $pay_source);
  93. }
  94. return [];
  95. }
  96. /**
  97. * 构建微信支付请求
  98. */
  99. protected static function onPaymentByWechat($user, $order, $pay_source)
  100. {
  101. return PaymentService::wechat(
  102. $user,
  103. [$order],
  104. OrderTypeEnum::PLAN,
  105. $pay_source
  106. );
  107. }
  108. }