Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/PaymentsController.php
Show First 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | protected static function mandateValidate(Request $request, Wallet $wallet) | ||||
return $v->errors()->toArray(); | return $v->errors()->toArray(); | ||||
} | } | ||||
$amount = (int) ($request->amount * 100); | $amount = (int) ($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 = Payment::MIN_AMOUNT; | $min = $wallet->getMinMandateAmount(); | ||||
$label = 'minamount'; | $label = 'minamount'; | ||||
if (($plan = $wallet->plan()) && $plan->months >= 1) { | |||||
$planCost = $plan->cost() * $plan->months; | |||||
if ($planCost > $min) { | |||||
$min = $planCost; | |||||
} | |||||
} | |||||
if ($wallet->balance < 0 && $wallet->balance < $min * -1) { | if ($wallet->balance < 0 && $wallet->balance < $min * -1) { | ||||
$min = $wallet->balance * -1; | $min = $wallet->balance * -1; | ||||
$label = 'minamountdebt'; | $label = 'minamountdebt'; | ||||
} | } | ||||
if ($amount < $min) { | if ($amount < $min) { | ||||
return ['amount' => \trans("validation.{$label}", ['amount' => $wallet->money($min)])]; | return ['amount' => \trans("validation.{$label}", ['amount' => $wallet->money($min)])]; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 212 Lines • ▼ Show 20 Lines | class PaymentsController extends Controller | ||||
public static function walletMandate(Wallet $wallet): array | public static function walletMandate(Wallet $wallet): array | ||||
{ | { | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
$settings = $wallet->getSettings(['mandate_disabled', 'mandate_balance', 'mandate_amount']); | $settings = $wallet->getSettings(['mandate_disabled', 'mandate_balance', 'mandate_amount']); | ||||
// Get the Mandate info | // Get the Mandate info | ||||
$mandate = (array) $provider->getMandate($wallet); | $mandate = (array) $provider->getMandate($wallet); | ||||
$mandate['amount'] = $mandate['minAmount'] = (int) ceil(Payment::MIN_AMOUNT / 100); | $mandate['amount'] = $mandate['minAmount'] = round($wallet->getMinMandateAmount() / 100, 2); | ||||
$mandate['balance'] = 0; | $mandate['balance'] = 0; | ||||
$mandate['isDisabled'] = !empty($mandate['id']) && $settings['mandate_disabled']; | $mandate['isDisabled'] = !empty($mandate['id']) && $settings['mandate_disabled']; | ||||
$mandate['isValid'] = !empty($mandate['isValid']); | $mandate['isValid'] = !empty($mandate['isValid']); | ||||
foreach (['amount', 'balance'] as $key) { | foreach (['amount', 'balance'] as $key) { | ||||
if (($value = $settings["mandate_{$key}"]) !== null) { | if (($value = $settings["mandate_{$key}"]) !== null) { | ||||
$mandate[$key] = $value; | $mandate[$key] = $value; | ||||
} | } | ||||
} | } | ||||
// If this is a multi-month plan, we calculate the expected amount to be payed. | |||||
if (($plan = $wallet->plan()) && $plan->months >= 1) { | |||||
$planCost = round($plan->cost() * $plan->months / 100, 2); | |||||
if ($planCost > $mandate['minAmount']) { | |||||
$mandate['minAmount'] = $planCost; | |||||
} | |||||
} | |||||
// Unrestrict the wallet owner if mandate is valid | // Unrestrict the wallet owner if mandate is valid | ||||
if (!empty($mandate['isValid']) && $wallet->owner->isRestricted()) { | if (!empty($mandate['isValid']) && $wallet->owner->isRestricted()) { | ||||
$wallet->owner->unrestrict(); | $wallet->owner->unrestrict(); | ||||
} | } | ||||
return $mandate; | return $mandate; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 141 Lines • Show Last 20 Lines |