OrderManager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Managers;
  3. use App\Generated\Exceptions\CustomErrorMessageException;
  4. use App\Jobs\OrderCloseJob;
  5. use App\Models\CouponModel;
  6. use App\Models\Fpdx\PairModel;
  7. use App\Models\OrderModel;
  8. use GuzzleHttp\Client;
  9. class OrderManager
  10. {
  11. protected $orderModel;
  12. protected $couponModel;
  13. protected $httpClient;
  14. public function __construct()
  15. {
  16. $this->httpClient = new Client();
  17. $this->orderModel = new OrderModel();
  18. $this->couponModel = new CouponModel();
  19. }
  20. /**
  21. * 72小时付费报名
  22. * @param $uid
  23. * @param array $attach
  24. * @param int $couponId
  25. * @return int
  26. * @throws CustomErrorMessageException
  27. * @throws \GuzzleHttp\Exception\GuzzleException
  28. */
  29. public function unifyOrderByPair($uid, array $attach, $couponId = 0)
  30. {
  31. $goodsId = 3;
  32. $goods = $this->orderModel::GOODS[$goodsId];
  33. $totalFee = $goods['total_fee'];
  34. // 计算优惠券
  35. if ($couponId) {
  36. $coupon = $this->couponModel->find($couponId);
  37. $settlement_amount = $coupon->getSettlementAmount($goodsId);
  38. $totalFee = $settlement_amount['settlement_total_amount'];
  39. }
  40. $attribute = [
  41. 'uid' => $uid,
  42. 'body' => "72小时恋爱体验报名",
  43. 'total_fee' => $totalFee,
  44. 'notify_url' => config('api.msy_url') . "/api/core/pay/notify/fpdx",
  45. 'attach' => json_encode($attach)
  46. ];
  47. $prepayId = $this->unify($attribute);
  48. $order = $this->orderModel->create([
  49. 'uid' => $uid,
  50. 'out_trade_no' => $prepayId,
  51. 'type' => $goodsId,
  52. 'total_amount' => $goods['total_fee'],
  53. 'total_fee' => $totalFee,
  54. 'attach_data' => json_encode($attach),
  55. 'coupon_id' => $couponId,
  56. ]);
  57. // 使用
  58. if ($couponId) {
  59. $coupon->use($goodsId, $order->id);
  60. }
  61. OrderCloseJob::dispatch($order->id)->onQueue('{order:close}')->delay(now()->addMinute(5));
  62. if ($totalFee == 0) {
  63. $params = [
  64. 'out_trade_no' => $prepayId,
  65. ];
  66. $uri = config('api.pay_url') . "/payments/notify/koudai";
  67. $this->httpClient->request("POST", $uri, [
  68. 'form_params' => $params
  69. ]);
  70. return 0;
  71. }
  72. return $order->id;
  73. }
  74. /**
  75. * 匹配补缴下单
  76. * @param PairModel $pair
  77. * @return OrderModel|\Illuminate\Database\Eloquent\Model
  78. * @throws CustomErrorMessageException
  79. * @throws \GuzzleHttp\Exception\GuzzleException
  80. */
  81. public function unifyOrderByScore(PairModel $pair)
  82. {
  83. $goods = $this->orderModel::GOODS[3];
  84. $totalFee = $goods['total_fee'] - $pair->score;
  85. $attach = ['pair_id' => $pair->id,'uid' => $pair->uid, 'state' => 103];
  86. $attribute = [
  87. 'uid' => $pair->uid,
  88. 'body' => "72小时恋爱体验报名",
  89. 'total_fee' => $totalFee,
  90. 'notify_url' => config('api.msy_url') . "/api/core/pay/notify/fpdx",
  91. 'attach' => json_encode($attach)
  92. ];
  93. $prepayId = $this->unify($attribute);
  94. $order = $this->orderModel->create([
  95. 'uid' => $pair->uid,
  96. 'out_trade_no' => $prepayId,
  97. 'type' => $goods['goods_id'],
  98. 'total_amount' => $totalFee,
  99. 'total_fee' => $totalFee,
  100. 'attach_data' => json_encode($attach)
  101. ]);
  102. return $order;
  103. }
  104. /**
  105. * 下单
  106. * @param array $attribute
  107. * @return mixed
  108. * @throws CustomErrorMessageException
  109. * @throws \GuzzleHttp\Exception\GuzzleException
  110. */
  111. private function unify(array $attribute)
  112. {
  113. $uri = config('api.pay_url') . "/payments/unify";
  114. $client = $this->httpClient->request("POST", $uri, [
  115. 'json' => $attribute
  116. ]);
  117. $response = json_decode($client->getBody()->getContents(), true);
  118. if (!isset($response['code']) || $response['code'] != 200) {
  119. throw new CustomErrorMessageException($response->message ?? "发起支付失败");
  120. }
  121. return $response['data']['order_id'];
  122. }
  123. }