PaymentService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Services\V1_1;
  3. use App\Generated\Exceptions\CustomErrorMessageException;
  4. use App\Generated\Exceptions\ForbiddenException;
  5. use App\Generated\Exceptions\NotFoundException;
  6. use App\Generated\Exceptions\SystemErrorException;
  7. use App\Generated\V1_1\Messages\Payment\FetchAppWechatPayParamsMessage;
  8. use App\Generated\V1_1\Messages\Payment\GetCouponDetailMessage;
  9. use App\Generated\V1_1\Messages\Payment\GetCouponListMessage;
  10. use App\Http\Controllers\Core\Auth;
  11. use App\Models\CouponModel;
  12. use App\Models\OrderModel;
  13. use App\Services\Pay\CouponService;
  14. class PaymentService
  15. {
  16. public function getCouponDetail(GetCouponDetailMessage $message)
  17. {
  18. $couponId = $message->getCouponId();
  19. $goodsId = $message->getGoodsId();
  20. $uid = Auth::auth();
  21. $couponService = new CouponService();
  22. $coupon = $couponService
  23. ->setUid($uid)
  24. ->setCouponId($couponId)
  25. ->getCoupon();
  26. if (!$coupon) {
  27. throw new NotFoundException('优惠券不存在或已过期');
  28. }
  29. // 查看状态
  30. if ($coupon->looked_at == 0) {
  31. $coupon->looked_at = time();
  32. $coupon->save();
  33. }
  34. if ($goodsId) {
  35. $couponService->setGoodsId($goodsId)->getSettlementAmount();
  36. $coupon->total_amount = $couponService->total_amount;
  37. $coupon->settlement_coupon_amount = $couponService->coupon_amount;
  38. $coupon->settlement_total_amount = $couponService->settlement_total_amount;
  39. }
  40. $message->setResponse($coupon);
  41. }
  42. public function getCouponList(GetCouponListMessage $message)
  43. {
  44. $type = $message->getType();
  45. $goodsScope = $message->getGoodsScope();
  46. $perPage = $message->getPerPage() ?: 20;
  47. $uid = Auth::auth();
  48. $coupons = CouponModel::existing($uid)
  49. ->when($type, function ($query) use ($type) {
  50. return $query->where('type', $type);
  51. })
  52. ->when($goodsScope, function ($query) use ($goodsScope) {
  53. return $query->where('goods_scope', $goodsScope);
  54. })
  55. ->orderBy('get_at', 'asc')
  56. ->paginate($perPage);
  57. CouponModel::whereIn('id', $coupons->pluck('id'))->where('looked_at', 0)->update(['looked_at' => time()]);
  58. $message->setResponse($coupons);
  59. }
  60. /**
  61. * @param FetchAppWechatPayParamsMessage $message
  62. * @throws CustomErrorMessageException
  63. * @throws ForbiddenException
  64. * @throws NotFoundException
  65. * @throws SystemErrorException
  66. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  67. */
  68. public function fetchAppWechatPayParams(FetchAppWechatPayParamsMessage $message)
  69. {
  70. $orderId = $message->getOrderId();
  71. $uid = Auth::auth();
  72. $order = OrderModel::find($orderId);
  73. if (!$order) {
  74. throw new NotFoundException('订单号错误或者不存在');
  75. }
  76. if ($order->trade_state != 0) {
  77. throw new NotFoundException('订单失效' . json_encode($order->toArray()));
  78. }
  79. if ($uid != $order->uid) {
  80. throw new ForbiddenException('用户标识不一致');
  81. }
  82. $pay_url = config('api.pay_url') . "/payments/app/{$order->out_trade_no}";
  83. $result = \Curl::to($pay_url)->asJsonResponse()->get();
  84. if (isset($result->code) && $result->code == 200) {
  85. $message->setResponse($result->data);
  86. } else {
  87. if (isset($result->code)) {
  88. throw new CustomErrorMessageException($result->message ?? '系统异常');
  89. }
  90. throw new SystemErrorException('系统异常');
  91. }
  92. }
  93. }