DepositCardOrder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/4/30 15:32
  12. */
  13. namespace App\Models;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\Log;
  16. class DepositCardOrder extends Model
  17. {
  18. protected $table = 'deposit_card_orders';
  19. // protected $fillable = ['no', 'area_id', 'user_id', 'deposit_cards_id', 'money', 'effective_days', 'discount', 'pay_money', 'pay_type', 'pay_status', 'pay_time', 'status'];
  20. protected $guarded = [];
  21. const NO_TAG = 'M';
  22. const PAY_TYPE_WECHAT = 1;
  23. const PAY_TYPE_ACCOUNT = 2;
  24. const PAY_TYPE_ADMIN_GIVE = 3;
  25. const PAY_TYPE_INVITE_NEW_USER_GIVE = 4;
  26. const PAY_TYPE_ALIPAYMINI = 5;
  27. public static $payTypeMaps = [
  28. self::PAY_TYPE_WECHAT => '微信支付',
  29. self::PAY_TYPE_ACCOUNT => '余额支付',
  30. self::PAY_TYPE_ADMIN_GIVE => '后台赠送',
  31. self::PAY_TYPE_INVITE_NEW_USER_GIVE => '邀请新用户赠送',
  32. self::PAY_TYPE_ALIPAYMINI => '支付宝支付',
  33. ];
  34. const PAY_STATUS_OK = 1;
  35. const PAY_STATUS_NO = 0;
  36. public static $payStatusMaps = [
  37. self::PAY_STATUS_OK => '支付成功',
  38. self::PAY_STATUS_NO => '支付失败',
  39. ];
  40. const STATUS_OK = 1;
  41. const STATUS_NO = 0;
  42. public static $statusMaps = [
  43. self::STATUS_OK => '支付成功',
  44. self::STATUS_NO => '支付失败'
  45. ];
  46. /**
  47. * 生成订单号
  48. * @return bool|string
  49. * User: Mead
  50. */
  51. public static function makeNo($tag = null)
  52. {
  53. // 订单流水号前缀
  54. $prefix = $tag . self::NO_TAG . date('YmdHis');
  55. for ($i = 0; $i < 10; $i++) {
  56. // 随机生成 6 位的数字
  57. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  58. // 判断是否已经存在
  59. if (!static::query()->where('no', $no)->exists()) {
  60. return $no;
  61. }
  62. }
  63. Log::warning('find order no failed');
  64. return false;
  65. }
  66. public function pay_order_callback()
  67. {
  68. //添加钱包记录
  69. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_CARD_DEPOSIT_ORDER, $this->attributes['area_id'], $this->attributes['id'], DepositCardOrder::class);
  70. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_CARD_DEPOSIT_ORDER, $this->attributes['area_id'], $this->attributes['id'], DepositCardOrder::class);
  71. // 修改用户押金状态
  72. $user = User::find($this->attributes['user_id']);
  73. $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
  74. if (Carbon::now()->gte($deposit_expire_time)) {
  75. $time = Carbon::now();
  76. } else {
  77. $time = $deposit_expire_time;
  78. }
  79. //判断用户是否已经缴纳押金
  80. if ((int)$user->is_deposit === User::DEPOSIT_OK && (int)$user->deposit_type === User::DEPOSIT_MONEY) {
  81. //已交押金
  82. User::where('id', $this->attributes['user_id'])->update([
  83. 'deposit_expire_time' => $time->addDays($this->attributes['effective_days'])
  84. ]);
  85. } else {
  86. //未交押金
  87. User::where('id', $this->attributes['user_id'])->update([
  88. 'deposit_expire_time' => $time->addDays($this->attributes['effective_days']),
  89. 'deposit_type' => User::DEPOSIT_CARD,
  90. 'is_deposit' => User::DEPOSIT_OK
  91. ]);
  92. }
  93. $this->save();
  94. return true;
  95. }
  96. }