DepositOrder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\Log;
  5. class DepositOrder extends Model
  6. {
  7. protected $guarded = [];
  8. const NO_TAG = 'Y';
  9. const STATUS_OK = 1;
  10. const STATUS_PAUSE = 0;
  11. public static $statusMaps = [
  12. self::STATUS_OK => '正常',
  13. self::STATUS_PAUSE => '暂停'
  14. ];
  15. const PAY_STATUS_OK = 1;
  16. const PAY_STATUS_NO = 0;
  17. public static $payStatusMaps = [
  18. self::PAY_STATUS_NO => '未支付',
  19. self::PAY_STATUS_OK => '已支付'
  20. ];
  21. const PAY_TYPE_NO = 0;
  22. const PAY_TYPE_WECHAT = 1;
  23. const PAY_TYPE_ACCOUNT = 2;
  24. const PAY_TYPE_ALIPAYMINI = 3;
  25. const PAY_TYPE_ALIPAYMINI_CREDIT = 4;
  26. public static $payTypeMaps = [
  27. self::PAY_TYPE_NO => '待支付',
  28. self::PAY_TYPE_WECHAT => '微信支付',
  29. self::PAY_TYPE_ACCOUNT => '余额支付',
  30. self::PAY_TYPE_ALIPAYMINI => '支付宝支付',
  31. self::PAY_TYPE_ALIPAYMINI_CREDIT => '支付宝免押金支付',
  32. ];
  33. const REFUND_OK = 1;
  34. const REFUND_NO = 0;
  35. public static $refundMaps = [
  36. self::REFUND_OK => '已退款',
  37. self::REFUND_NO => '未退款'
  38. ];
  39. /**
  40. * 生成订单号
  41. * @return bool|string
  42. * User: Mead
  43. */
  44. public static function makeNo($tag = null)
  45. {
  46. // 订单流水号前缀
  47. $prefix = $tag . self::NO_TAG . date('YmdHis');
  48. for ($i = 0; $i < 10; $i++) {
  49. // 随机生成 6 位的数字
  50. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  51. // 判断是否已经存在
  52. if (!static::query()->where('no', $no)->exists()) {
  53. return $no;
  54. }
  55. }
  56. Log::warning('find order no failed');
  57. return false;
  58. }
  59. /**
  60. * 支付回调
  61. * @return bool
  62. * Author: Mead
  63. */
  64. public function pay_order_callback()
  65. {
  66. // 修改用户押金状态
  67. User::where('id', $this->attributes['user_id'])->update([
  68. 'deposit_money' => $this->attributes['money'],
  69. 'deposit_type' => User::DEPOSIT_MONEY,
  70. 'is_deposit' => User::DEPOSIT_OK
  71. ]);
  72. $this->save();
  73. return true;
  74. }
  75. //支付宝资金预授权回调
  76. public function pay_order_shouquan_callback()
  77. {
  78. // 修改用户押金状态
  79. User::where('id', $this->attributes['user_id'])->update([
  80. 'deposit_money' => $this->attributes['money'],
  81. 'deposit_type' => User::DEPOSIT_ALIPAY_CARD,
  82. 'is_deposit' => User::DEPOSIT_OK
  83. ]);
  84. $this->save();
  85. return true;
  86. }
  87. /**
  88. * 退款回调
  89. * Author: Mead
  90. */
  91. public function refund_order_callback()
  92. {
  93. //商户平台直接退押金回调
  94. $user_id = $this->attributes['user_id'];
  95. $user = User::find($user_id);
  96. $re = CouponsUserBag::query()->where('user_id', $user_id)
  97. ->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)
  98. ->where('valid_end_time', '<=', Carbon::now()->toDateTimeString())
  99. ->where('status', CouponsUserBag::STATUS_OK)->exists();
  100. if ($re) {
  101. //是否有免押金劵
  102. User::where('id', $user->id)->update([
  103. 'is_new_user_coupons' => User::IS_NEW_USER_COUPONS_NO
  104. ]);
  105. }
  106. $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
  107. if (Carbon::now()->gte($deposit_expire_time)) {
  108. // 免押金卡已经过期
  109. User::where('id', $user_id)->update([
  110. 'deposit_money' => 0,
  111. 'is_deposit' => User::DEPOSIT_NO,
  112. 'deposit_type' => User::DEPOSIT_TYPE_NO
  113. ]);
  114. } else {
  115. // 免押金卡未过期
  116. User::where('id', $user_id)->update([
  117. 'deposit_money' => 0,
  118. 'is_deposit' => User::DEPOSIT_OK,
  119. 'deposit_type' => User::DEPOSIT_CARD
  120. ]);
  121. }
  122. //检查退款请求
  123. RefundLog::where('deposit_id', $this->attributes['id'])->update([
  124. 'pay_status' => RefundLog::PAY_STATUS_OK,
  125. 'pay_time' => date('Y-m-d H:i:s')
  126. ]);
  127. }
  128. }