PayDetailService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\service\order;
  3. use app\model\PayDetail;
  4. use support\Db;
  5. class PayDetailService
  6. {
  7. public static function checkPaying()
  8. {
  9. Db::beginTransaction();
  10. try {
  11. $timeUnix = strtotime("-30 minutes");
  12. $payDetails = PayDetail::where('pay_status', 'WAITING')
  13. ->where('pay_addtimes', '<', $timeUnix)
  14. ->get();
  15. foreach ($payDetails as $payDetail) {
  16. // 订单主表
  17. PayDetail::where('pay_id', $payDetail->pay_id)->update([
  18. 'pay_status' => 'CANCEL',
  19. ]);
  20. }
  21. Db::commit();
  22. } catch (\Exception $e) {
  23. Db::rollBack();
  24. }
  25. }
  26. /**
  27. * @Desc 搜索指定时间的付款订单ID
  28. * @Author Gorden
  29. * @Date 2024/10/24 15:12
  30. *
  31. * @param $timeStart
  32. * @param $timeEnd
  33. * @return array
  34. */
  35. public static function getPayOrderId($timeStart, $timeEnd, $category = [])
  36. {
  37. $payDetails = PayDetail::whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) >= ? ', [$timeStart])
  38. ->whereRaw('CAST(UNIX_TIMESTAMP(pay_paytimes) as SIGNED) <= ? ', [$timeEnd])
  39. ->where('pay_status', 'SUCCESS')
  40. ->when(!empty($category), function ($query) use ($category) {
  41. $query->whereIn('pay_category', $category);
  42. })
  43. ->select('join_pay_object_json')
  44. ->get()
  45. ->toArray();
  46. $orderIds = [];
  47. foreach ($payDetails as $detail) {
  48. if (!empty($detail['join_pay_object_json']) && is_json($detail['join_pay_object_json'])) {
  49. $payObjectJson = json_decode($detail['join_pay_object_json'], true);
  50. if (!empty($payObjectJson['order_id']) && !in_array($payObjectJson['order_id'], $orderIds)) {
  51. $orderIds[] = $payObjectJson['order_id'];
  52. }
  53. }
  54. }
  55. return $orderIds;
  56. }
  57. /**
  58. * @Desc 获取支付方式
  59. * @Author Gorden
  60. * @Date 2024/10/24 16:12
  61. *
  62. * @param $groupId
  63. * @param $orderId
  64. * @return array
  65. */
  66. public static function getPayWay($groupId, $orderId)
  67. {
  68. $payDetails = PayDetail::where('join_pay_order_id', $groupId)
  69. ->where(function ($query) use ($orderId) {
  70. $query->whereJsonContains('join_pay_object_json->order_id', $orderId)
  71. ->orWhere('join_pay_object_json', '')
  72. ->orWhereNull('join_pay_object_json');
  73. })->where('pay_category', '<>', 'REFUND')
  74. ->where('pay_prepayid','<>','')
  75. ->where('pay_status', 'SUCCESS')
  76. ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json')
  77. ->get()
  78. ->toArray();
  79. $data = [];
  80. if (count($payDetails) > 1) {
  81. $data['payWay'] = "组合支付";
  82. }
  83. foreach ($payDetails as $detail) {
  84. $data['pay_paytimes'] = $detail['pay_paytimes'];
  85. $data['pay_status'] = $detail['pay_status'];
  86. $data['pay_amount'] = $detail['pay_amount'];
  87. $data['pay_category'] = $detail['pay_category'];
  88. // $payWay = '';
  89. // if (in_array($detail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY', 'OFFLINE'])) {
  90. // $payWay = $detail['pay_prepayid'];
  91. // } else {
  92. // $prepayid = explode('-', $detail['pay_prepayid']);
  93. // if (!empty($prepayid[1]) && in_array($prepayid[1], ['CASH', 'WELFARE', 'QRCODE', 'NONE'])) {
  94. // $payWay = $prepayid[1];
  95. // }
  96. // }
  97. $payWay = self::getPayWayByPrepayId($detail['pay_prepayid']);
  98. if (empty($payWay)) {
  99. $data['payWay'] = '';
  100. return $data;
  101. }
  102. $payWay = OrderService::$payWay[$payWay];
  103. if (!empty($data['payWay']) && $data['payWay'] == '组合支付') {
  104. $data['payWayDesc'] = $payWay . '(¥' . $detail['pay_amount'] . '),';
  105. } else {
  106. $data['payWay'] = $payWay;
  107. }
  108. }
  109. return $data;
  110. }
  111. /**
  112. * @Desc 根据prepayId 获取 支付方式
  113. * @Author Gorden
  114. * @Date 2024/10/28 14:04
  115. *
  116. * @param $prepayId
  117. * @return mixed|string
  118. */
  119. public static function getPayWayByPrepayId($prepayId)
  120. {
  121. $payWay = '';
  122. if (in_array($prepayId, ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY', 'OFFLINE'])) {
  123. $payWay = $prepayId;
  124. } else {
  125. $prepayIdArr = explode('-', $prepayId);
  126. if (!empty($prepayIdArr[1]) && in_array($prepayIdArr[1], ['CASH', 'WELFARE', 'QRCODE', 'NONE'])) {
  127. $payWay = $prepayIdArr[1];
  128. }
  129. }
  130. return $payWay;
  131. }
  132. }