123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace App\Models;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Log;
- class DepositOrder extends Model
- {
- protected $guarded = [];
- const NO_TAG = 'Y';
- const STATUS_OK = 1;
- const STATUS_PAUSE = 0;
- public static $statusMaps = [
- self::STATUS_OK => '正常',
- self::STATUS_PAUSE => '暂停'
- ];
- const PAY_STATUS_OK = 1;
- const PAY_STATUS_NO = 0;
- public static $payStatusMaps = [
- self::PAY_STATUS_NO => '未支付',
- self::PAY_STATUS_OK => '已支付'
- ];
- const PAY_TYPE_NO = 0;
- const PAY_TYPE_WECHAT = 1;
- const PAY_TYPE_ACCOUNT = 2;
- const PAY_TYPE_ALIPAYMINI = 3;
- const PAY_TYPE_ALIPAYMINI_CREDIT = 4;
- public static $payTypeMaps = [
- self::PAY_TYPE_NO => '待支付',
- self::PAY_TYPE_WECHAT => '微信支付',
- self::PAY_TYPE_ACCOUNT => '余额支付',
- self::PAY_TYPE_ALIPAYMINI => '支付宝支付',
- self::PAY_TYPE_ALIPAYMINI_CREDIT => '支付宝免押金支付',
- ];
- const REFUND_OK = 1;
- const REFUND_NO = 0;
- public static $refundMaps = [
- self::REFUND_OK => '已退款',
- self::REFUND_NO => '未退款'
- ];
- /**
- * 生成订单号
- * @return bool|string
- * User: Mead
- */
- public static function makeNo($tag = null)
- {
- // 订单流水号前缀
- $prefix = $tag . self::NO_TAG . date('YmdHis');
- for ($i = 0; $i < 10; $i++) {
- // 随机生成 6 位的数字
- $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
- // 判断是否已经存在
- if (!static::query()->where('no', $no)->exists()) {
- return $no;
- }
- }
- Log::warning('find order no failed');
- return false;
- }
- /**
- * 支付回调
- * @return bool
- * Author: Mead
- */
- public function pay_order_callback()
- {
- // 修改用户押金状态
- User::where('id', $this->attributes['user_id'])->update([
- 'deposit_money' => $this->attributes['money'],
- 'deposit_type' => User::DEPOSIT_MONEY,
- 'is_deposit' => User::DEPOSIT_OK
- ]);
- $this->save();
- return true;
- }
- //支付宝资金预授权回调
- public function pay_order_shouquan_callback()
- {
- // 修改用户押金状态
- User::where('id', $this->attributes['user_id'])->update([
- 'deposit_money' => $this->attributes['money'],
- 'deposit_type' => User::DEPOSIT_ALIPAY_CARD,
- 'is_deposit' => User::DEPOSIT_OK
- ]);
- $this->save();
- return true;
- }
- /**
- * 退款回调
- * Author: Mead
- */
- public function refund_order_callback()
- {
- //商户平台直接退押金回调
- $user_id = $this->attributes['user_id'];
- $user = User::find($user_id);
- $re = CouponsUserBag::query()->where('user_id', $user_id)
- ->where('coupon_type', CouponsUserBag::COUPON_TYPE_DEPOSIT_FREE)
- ->where('valid_end_time', '<=', Carbon::now()->toDateTimeString())
- ->where('status', CouponsUserBag::STATUS_OK)->exists();
- if ($re) {
- //是否有免押金劵
- User::where('id', $user->id)->update([
- 'is_new_user_coupons' => User::IS_NEW_USER_COUPONS_NO
- ]);
- }
- $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
- if (Carbon::now()->gte($deposit_expire_time)) {
- // 免押金卡已经过期
- User::where('id', $user_id)->update([
- 'deposit_money' => 0,
- 'is_deposit' => User::DEPOSIT_NO,
- 'deposit_type' => User::DEPOSIT_TYPE_NO
- ]);
- } else {
- // 免押金卡未过期
- User::where('id', $user_id)->update([
- 'deposit_money' => 0,
- 'is_deposit' => User::DEPOSIT_OK,
- 'deposit_type' => User::DEPOSIT_CARD
- ]);
- }
- //检查退款请求
- RefundLog::where('deposit_id', $this->attributes['id'])->update([
- 'pay_status' => RefundLog::PAY_STATUS_OK,
- 'pay_time' => date('Y-m-d H:i:s')
- ]);
- }
- }
|