DepositOrder.php 3.1 KB

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