123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Models;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Log;
- class CardRidingOrder extends Model
- {
- public $table = 'card_riding_orders';
- protected $fillable = ['no', 'area_id', 'user_id', 'riding_card_id', 'money', 'discount', 'pay_money', 'pay_type', 'pay_status', 'pay_time', 'status'];
- const NO_TAG = 'R';
- const PAY_TYPE_WECHAT = 1;
- const PAY_TYPE_ACCOUNT = 2;
- const PAY_TYPE_ADMIN_GIVE = 3;
- const PAY_TYPE_INVITE_NEW_USER_GIVE = 4;
- public static $payTypeMaps = [
- self::PAY_TYPE_WECHAT => '微信支付',
- self::PAY_TYPE_ACCOUNT => '余额支付',
- self::PAY_TYPE_ADMIN_GIVE => '后台赠送',
- self::PAY_TYPE_INVITE_NEW_USER_GIVE => '邀请新用户赠送'
- ];
- const PAY_STATUS_OK = 1;
- const PAY_STATUS_NO = 0;
- public static $payStatusMaps = [
- self::PAY_STATUS_OK => '支付成功',
- self::PAY_STATUS_NO => '支付失败',
- ];
- const STATUS_OK = 1;
- const STATUS_NO = 0;
- public static $statusMaps = [
- self::STATUS_OK => '支付成功',
- self::STATUS_NO => '支付失败'
- ];
- /**
- * 生成订单号
- * @return bool|string
- * User: Mead
- */
- public static function makeNo()
- {
- // 订单流水号前缀
- $prefix = config('bike.no_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;
- }
- public function pay_order_callback()
- {
- $order = $this->attributes;
- //添加钱包记录
- WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_CARD_RIDING_ORDER, $this->attributes['area_id'], $this->attributes['id'], CardRidingOrder::class);
- WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_CARD_RIDING_ORDER, $this->attributes['area_id'], $this->attributes['id'], CardRidingOrder::class);
- //修改订单记录
- $this->pay_status = CardRidingOrder::PAY_STATUS_OK;
- $this->pay_time = now();
- $this->pay_type = CardRidingOrder::PAY_TYPE_WECHAT;
- $this->status = CardRidingOrder::STATUS_OK;
- $this->save();
- //支付成功给用户发卡
- $cardRiding = CardRiding::find($this->riding_card_id);
- $take_effect_time = Carbon::now()->format('Y-m-d H:i:s');
- $expiration_time = Carbon::now()->addDays($cardRiding->effective_days)->format('Y-m-d H:i:s');
- $data = [
- 'user_id' => $this->attributes['user_id'],
- 'riding_card_id' => $this->attributes['riding_card_id'],
- 'card_riding_order_id' => $this->attributes['id'],
- 'take_effect_time' => $take_effect_time,
- 'deduction_money' => $cardRiding->deduction_money,
- 'expiration_time' => $expiration_time,
- 'is_limit_times' => $cardRiding->is_limit_times,
- 'can_ridding_times' => $cardRiding->times,
- 'day_can_ridding_times' => $cardRiding->day_can_ridding_times,
- 'status' => CardRidingUserBags::STATUS_OK
- ];
- // Log::info($data);
- CardRidingUserBags::firstOrCreate(['card_riding_order_id' => $this->attributes['id']], $data);
- return true;
- }
- }
|