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;
}
}
возвращает ноль при использовании в приведенном ниже коде:
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-> deduct () и $ this-> доход () к началу транзакции, они проходят, пока DB :: table ('транзакции') завершается неудачей.
Заранее спасибо!