Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/PaymentsController.php
Show First 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | public function mandateCreate(Request $request) | ||||
// Validate the minimum value | // Validate the minimum value | ||||
if ($amount < PaymentProvider::MIN_AMOUNT) { | if ($amount < PaymentProvider::MIN_AMOUNT) { | ||||
$min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | $min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | ||||
$errors = ['amount' => \trans('validation.minamount', ['amount' => $min])]; | $errors = ['amount' => \trans('validation.minamount', ['amount' => $min])]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
$wallet->setSetting('mandate_amount', $request->amount); | $wallet->setSettings([ | ||||
$wallet->setSetting('mandate_balance', $request->balance); | 'mandate_amount' => $request->amount, | ||||
'mandate_balance' => $request->balance, | |||||
]); | |||||
$request = [ | $request = [ | ||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'description' => \config('app.name') . ' Auto-Payment Setup', | 'description' => \config('app.name') . ' Auto-Payment Setup', | ||||
]; | ]; | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
Show All 16 Lines | public function mandateDelete() | ||||
// TODO: Wallet selection | // TODO: Wallet selection | ||||
$wallet = $user->wallets->first(); | $wallet = $user->wallets->first(); | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
$provider->deleteMandate($wallet); | $provider->deleteMandate($wallet); | ||||
$wallet->setSetting('mandate_disabled', null); | |||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => \trans('app.mandate-delete-success'), | 'message' => \trans('app.mandate-delete-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Update a new auto-payment mandate. | * Update a new auto-payment mandate. | ||||
Show All 27 Lines | public function mandateUpdate(Request $request) | ||||
// Validate the minimum value | // Validate the minimum value | ||||
if ($amount < PaymentProvider::MIN_AMOUNT) { | if ($amount < PaymentProvider::MIN_AMOUNT) { | ||||
$min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | $min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | ||||
$errors = ['amount' => \trans('validation.minamount', ['amount' => $min])]; | $errors = ['amount' => \trans('validation.minamount', ['amount' => $min])]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
$wallet->setSetting('mandate_amount', $request->amount); | // If the mandate is disabled the update will trigger | ||||
$wallet->setSetting('mandate_balance', $request->balance); | // an auto-payment and the amount must cover the debt | ||||
if ($wallet->getSetting('mandate_disabled')) { | |||||
if ($wallet->balance < 0 && $wallet->balance + $amount < 0) { | |||||
$errors = ['amount' => \trans('validation.minamountdebt')]; | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
return response()->json([ | $wallet->setSetting('mandate_disabled', null); | ||||
'status' => 'success', | |||||
'message' => \trans('app.mandate-update-success'), | if ($wallet->balance < intval($request->balance * 100)) { | ||||
\App\Jobs\WalletCharge::dispatch($wallet); | |||||
} | |||||
} | |||||
$wallet->setSettings([ | |||||
'mandate_amount' => $request->amount, | |||||
'mandate_balance' => $request->balance, | |||||
]); | ]); | ||||
$result = self::walletMandate($wallet); | |||||
$result['status'] = 'success'; | |||||
$result['message'] = \trans('app.mandate-update-success'); | |||||
return response()->json($result); | |||||
} | } | ||||
/** | /** | ||||
* Create a new payment. | * Create a new payment. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse The response | * @return \Illuminate\Http\JsonResponse The response | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | public function webhook($provider) | ||||
if ($provider = PaymentProvider::factory($provider)) { | if ($provider = PaymentProvider::factory($provider)) { | ||||
$code = $provider->webhook(); | $code = $provider->webhook(); | ||||
} | } | ||||
return response($code < 400 ? 'Success' : 'Server error', $code); | return response($code < 400 ? 'Success' : 'Server error', $code); | ||||
} | } | ||||
/** | /** | ||||
* Charge a wallet with a "recurring" payment. | * Top up a wallet with a "recurring" payment. | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet to charge | * @param \App\Wallet $wallet The wallet to charge | ||||
* @param int $amount The amount of money in cents | |||||
* | * | ||||
* @return bool | * @return bool True if the payment has been initialized | ||||
*/ | */ | ||||
public static function directCharge(Wallet $wallet, $amount): bool | public static function topUpWallet(Wallet $wallet): bool | ||||
{ | { | ||||
if ((bool) $wallet->getSetting('mandate_disabled')) { | |||||
return false; | |||||
} | |||||
$min_balance = (int) (floatval($wallet->getSetting('mandate_balance')) * 100); | |||||
$amount = (int) (floatval($wallet->getSetting('mandate_amount')) * 100); | |||||
// The wallet balance is greater than the auto-payment threshold | |||||
if ($wallet->balance >= $min_balance) { | |||||
// Do nothing | |||||
return false; | |||||
} | |||||
// The defined top-up amount is not enough | |||||
// Disable auto-payment and notify the user | |||||
if ($wallet->balance + $amount < 0) { | |||||
// Disable (not remove) the mandate | |||||
$wallet->setSetting('mandate_disabled', 1); | |||||
\App\Jobs\PaymentMandateDisabledEmail::dispatch($wallet); | |||||
return false; | |||||
} | |||||
$provider = PaymentProvider::factory($wallet); | |||||
$mandate = (array) $provider->getMandate($wallet); | |||||
if (empty($mandate['isValid'])) { | |||||
return false; | |||||
} | |||||
$request = [ | $request = [ | ||||
'type' => PaymentProvider::TYPE_RECURRING, | 'type' => PaymentProvider::TYPE_RECURRING, | ||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'description' => \config('app.name') . ' Recurring Payment', | 'description' => \config('app.name') . ' Recurring Payment', | ||||
]; | ]; | ||||
$provider = PaymentProvider::factory($wallet); | $result = $provider->payment($wallet, $request); | ||||
if ($result = $provider->payment($wallet, $request)) { | |||||
return true; | |||||
} | |||||
return false; | return !empty($result); | ||||
} | } | ||||
/** | /** | ||||
* Returns auto-payment mandate info for the specified wallet | * Returns auto-payment mandate info for the specified wallet | ||||
* | * | ||||
* @param \App\Wallet $wallet A wallet object | * @param \App\Wallet $wallet A wallet object | ||||
* | * | ||||
* @return array A mandate metadata | * @return array A mandate metadata | ||||
*/ | */ | ||||
public static function walletMandate(Wallet $wallet): array | public static function walletMandate(Wallet $wallet): array | ||||
{ | { | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
// Get the Mandate info | // Get the Mandate info | ||||
$mandate = (array) $provider->getMandate($wallet); | $mandate = (array) $provider->getMandate($wallet); | ||||
$mandate['amount'] = (int) (PaymentProvider::MIN_AMOUNT / 100); | $mandate['amount'] = (int) (PaymentProvider::MIN_AMOUNT / 100); | ||||
$mandate['balance'] = 0; | $mandate['balance'] = 0; | ||||
$mandate['isDisabled'] = !empty($mandate['id']) && $wallet->getSetting('mandate_disabled'); | |||||
foreach (['amount', 'balance'] as $key) { | foreach (['amount', 'balance'] as $key) { | ||||
if (($value = $wallet->getSetting("mandate_{$key}")) !== null) { | if (($value = $wallet->getSetting("mandate_{$key}")) !== null) { | ||||
$mandate[$key] = $value; | $mandate[$key] = $value; | ||||
} | } | ||||
} | } | ||||
return $mandate; | return $mandate; | ||||
} | } | ||||
} | } |