Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/PaymentsController.php
Show First 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | public function mandateCreate(Request $request) | ||||
'description' => Tenant::getConfig($user->tenant_id, 'app.name') | 'description' => Tenant::getConfig($user->tenant_id, 'app.name') | ||||
. ' ' . self::trans('app.mandate-description-suffix'), | . ' ' . self::trans('app.mandate-description-suffix'), | ||||
'methodId' => $request->methodId ?: PaymentProvider::METHOD_CREDITCARD, | 'methodId' => $request->methodId ?: PaymentProvider::METHOD_CREDITCARD, | ||||
]; | ]; | ||||
// Normally the auto-payment setup operation is 0, if the balance is below the threshold | // Normally the auto-payment setup operation is 0, if the balance is below the threshold | ||||
// we'll top-up the wallet with the configured auto-payment amount | // we'll top-up the wallet with the configured auto-payment amount | ||||
if ($wallet->balance < intval($request->balance * 100)) { | if ($wallet->balance < round($request->balance * 100)) { | ||||
$mandate['amount'] = intval($request->amount * 100); | $mandate['amount'] = (int) round($request->amount * 100); | ||||
self::addTax($wallet, $mandate); | self::addTax($wallet, $mandate); | ||||
} | } | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
$result = $provider->createMandate($wallet, $mandate); | $result = $provider->createMandate($wallet, $mandate); | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | public function mandateUpdate(Request $request) | ||||
'mandate_amount' => $request->amount, | 'mandate_amount' => $request->amount, | ||||
'mandate_balance' => $request->balance, | 'mandate_balance' => $request->balance, | ||||
// Re-enable the mandate to give it a chance to charge again | // Re-enable the mandate to give it a chance to charge again | ||||
// after it has been disabled (e.g. because the mandate amount was too small) | // after it has been disabled (e.g. because the mandate amount was too small) | ||||
'mandate_disabled' => null, | 'mandate_disabled' => null, | ||||
]); | ]); | ||||
// Trigger auto-payment if the balance is below the threshold | // Trigger auto-payment if the balance is below the threshold | ||||
if ($wallet->balance < intval($request->balance * 100)) { | if ($wallet->balance < round($request->balance * 100)) { | ||||
\App\Jobs\WalletCharge::dispatch($wallet); | \App\Jobs\WalletCharge::dispatch($wallet); | ||||
} | } | ||||
$result = self::walletMandate($wallet); | $result = self::walletMandate($wallet); | ||||
$result['status'] = 'success'; | $result['status'] = 'success'; | ||||
$result['message'] = self::trans('app.mandate-update-success'); | $result['message'] = self::trans('app.mandate-update-success'); | ||||
return response()->json($result); | return response()->json($result); | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | protected static function mandateValidate(Request $request, Wallet $wallet) | ||||
$v = Validator::make($request->all(), $rules); | $v = Validator::make($request->all(), $rules); | ||||
// TODO: allow comma as a decimal point? | // TODO: allow comma as a decimal point? | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return $v->errors()->toArray(); | return $v->errors()->toArray(); | ||||
} | } | ||||
$amount = (int) ($request->amount * 100); | $amount = (int) round($request->amount * 100); | ||||
// Validate the minimum value | // Validate the minimum value | ||||
// It has to be at least minimum payment amount and must cover current debt, | // It has to be at least minimum payment amount and must cover current debt, | ||||
// and must be more than a yearly/monthly payment (according to the plan) | // and must be more than a yearly/monthly payment (according to the plan) | ||||
$min = $wallet->getMinMandateAmount(); | $min = $wallet->getMinMandateAmount(); | ||||
$label = 'minamount'; | $label = 'minamount'; | ||||
if ($wallet->balance < 0 && $wallet->balance < $min * -1) { | if ($wallet->balance < 0 && $wallet->balance < $min * -1) { | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | public function store(Request $request) | ||||
$v = Validator::make($request->all(), $rules); | $v = Validator::make($request->all(), $rules); | ||||
// TODO: allow comma as a decimal point? | // TODO: allow comma as a decimal point? | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | ||||
} | } | ||||
$amount = (int) ($request->amount * 100); | $amount = (int) round($request->amount * 100); | ||||
// Validate the minimum value | // Validate the minimum value | ||||
if ($amount < Payment::MIN_AMOUNT) { | if ($amount < Payment::MIN_AMOUNT) { | ||||
$min = $wallet->money(Payment::MIN_AMOUNT); | $min = $wallet->money(Payment::MIN_AMOUNT); | ||||
$errors = ['amount' => self::trans('validation.minamount', ['amount' => $min])]; | $errors = ['amount' => self::trans('validation.minamount', ['amount' => $min])]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | public static function topUpWallet(Wallet $wallet): bool | ||||
\Log::debug("Requested top-up for wallet {$wallet->id}"); | \Log::debug("Requested top-up for wallet {$wallet->id}"); | ||||
if (!empty($settings['mandate_disabled'])) { | if (!empty($settings['mandate_disabled'])) { | ||||
\Log::debug("Top-up for wallet {$wallet->id}: mandate disabled"); | \Log::debug("Top-up for wallet {$wallet->id}: mandate disabled"); | ||||
return false; | return false; | ||||
} | } | ||||
$min_balance = (int) (floatval($settings['mandate_balance']) * 100); | $min_balance = (int) round(floatval($settings['mandate_balance']) * 100); | ||||
$amount = (int) (floatval($settings['mandate_amount']) * 100); | $amount = (int) round(floatval($settings['mandate_amount']) * 100); | ||||
// The wallet balance is greater than the auto-payment threshold | // The wallet balance is greater than the auto-payment threshold | ||||
if ($wallet->balance >= $min_balance) { | if ($wallet->balance >= $min_balance) { | ||||
// Do nothing | // Do nothing | ||||
return false; | return false; | ||||
} | } | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
▲ Show 20 Lines • Show All 206 Lines • Show Last 20 Lines |