jingcai-php/app/Model/Customer/CustomerRecharge.php

69 lines
1.7 KiB
PHP
Executable File

<?php
namespace App\Model\Customer;
use App\Enums\BoolEnum;
use App\Enums\PayState;
use App\Exceptions\JingCaiException;
use App\Model\BaseModel;
use App\Model\Order;
use App\Model\Seller\ShopPayChannel;
use App\Model\Trade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CustomerRecharge extends BaseModel
{
public function trade()
{
return $this->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();
}
}
}