Laravel私有方法返回null,但带有变量,但返回带有硬编码int的正确响应


private function subtractCharges($amount){
if($amount < 50000){
// charge 3% only
$sum = $amount - (0.03 * $amount);
return $sum;
} elseif($amount >50000 && $amount < 250000){
// charge 3% + 50 naira
$sum = $amount - ((0.03 * $amount) + 5000);
return $sum;
} elseif ($amount > 300000){
// charge 3% + 150 capped at 3000
$charge = ((0.03 * $amount) + 15000);
$sum = $charge > 300000 ? $amount - 300000 : $amount - $charge;
return $sum;
}
}

在以下代码中使用时返回null:

public function giveFromWallet($org_id, $amount, $name, $purpose){
// check if sufficient funds in wallet to give
$amountKobo = $amount * 100;
if ($this->balance() >= $amountKobo) {
// deduct the product->price from user wallet
if(DB::transaction(function () use ($org_id, $amountKobo, $name, $purpose) {
// deposit to the user
DB::table('transactions')->insert([
'organisation_id' => $org_id,
'user_id' => auth()->user()->id,
'admin_id' => 'nill',
'amount' => $this->subtractCharges($amountKobo),//error happens here
'user_amount' => $amountKobo,
'name' => auth()->user()->name,
'email' => auth()->user()->email,
'purpose' => $purpose,
'type' => 'Deposit',
'auth_code' => 'nill',
'created_at' => Carbon::now()
]);
// add revenue/earnings
$this->revenue($this->calculateRevenue($amountKobo), auth()->user()->id, $org_id, $name);
// deduct the amount from the user's account
$this->deduct(auth()->user()->id, $amountKobo, $name, $purpose);
}, 1)){
return back()->with('status', 'You have given '.$this->toNaira($amountKobo).' to '.$name);
}else{
return back()->with('error', 'Transaction incomplete, contact admin');
};
} else {
return back()->with('error', "Transaction failed, Insufficient funds!");
}
}

此外,如果失败,事务应该反转,但当我将$this->reduce(($sthis->revenu((移动到事务的顶部时,它们会在

DB::table('transactions'(提前感谢!

如果subtractCharges()函数中的$amount等于50000,或者介于250000和300000之间,则函数不会返回任何内容(或null(。也许这就是问题所在?

最新更新