hasOne(Trade::class, 'recharge_id', 'id')->where('role', Trade::ROLE_CUSTOMER); } public function shopPayChannel() { return $this->hasOne(ShopPayChannel::class, 'id', 'pay_channel_id'); } public function order() { return $this->hasOne(Order::class, 'id', 'order_id'); } public function scopeSn($query, $rechargeSn) { return $this->where('recharge_sn', $rechargeSn); } public function isCompleted() { return $this->pay_state == PayState::SUCCESS; } public function isSuccess() { return $this->pay_state == PayState::SUCCESS && bc_equal($this->pay_money, $this->received_money); } /** * 设置订单为处理中 * @return void */ public function setPending() { DB::beginTransaction(); try { CustomerRecharge::where('id', $this->id) ->where('pay_state', PayState::UNPAID) ->update(['pay_state' => PayState::PENDING]); if ($this->order_id) { Order::where('id', $this->order_id) ->where('pay_state', PayState::UNPAID) ->update(['pay_state' => PayState::PENDING]); } DB::commit(); } catch (\Exception $exception) { DB::rollBack(); } } }