Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/PaymentsController.php
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | public function mandateCreate(Request $request) | ||||
'description' => Tenant::getConfig($user->tenant_id, 'app.name') . ' Auto-Payment Setup', | 'description' => Tenant::getConfig($user->tenant_id, 'app.name') . ' Auto-Payment Setup', | ||||
'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 < intval($request->balance * 100)) { | ||||
$mandate['amount'] = intval($request->amount * 100); | $mandate['amount'] = intval($request->amount * 100); | ||||
self::addTax($wallet, $mandate); | |||||
} | } | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
$result = $provider->createMandate($wallet, $mandate); | $result = $provider->createMandate($wallet, $mandate); | ||||
$result['status'] = 'success'; | $result['status'] = 'success'; | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | public function store(Request $request) | ||||
$request = [ | $request = [ | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'currency' => $currency, | 'currency' => $currency, | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'methodId' => $request->methodId ?: PaymentProvider::METHOD_CREDITCARD, | 'methodId' => $request->methodId ?: PaymentProvider::METHOD_CREDITCARD, | ||||
'description' => Tenant::getConfig($user->tenant_id, 'app.name') . ' Payment', | 'description' => Tenant::getConfig($user->tenant_id, 'app.name') . ' Payment', | ||||
]; | ]; | ||||
self::addTax($wallet, $request); | |||||
$provider = PaymentProvider::factory($wallet, $currency); | $provider = PaymentProvider::factory($wallet, $currency); | ||||
$result = $provider->payment($wallet, $request); | $result = $provider->payment($wallet, $request); | ||||
$result['status'] = 'success'; | $result['status'] = 'success'; | ||||
return response()->json($result); | return response()->json($result); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | public static function topUpWallet(Wallet $wallet): bool | ||||
$request = [ | $request = [ | ||||
'type' => PaymentProvider::TYPE_RECURRING, | 'type' => PaymentProvider::TYPE_RECURRING, | ||||
'currency' => $wallet->currency, | 'currency' => $wallet->currency, | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'methodId' => PaymentProvider::METHOD_CREDITCARD, | 'methodId' => PaymentProvider::METHOD_CREDITCARD, | ||||
'description' => Tenant::getConfig($wallet->owner->tenant_id, 'app.name') . ' Recurring Payment', | 'description' => Tenant::getConfig($wallet->owner->tenant_id, 'app.name') . ' Recurring Payment', | ||||
]; | ]; | ||||
self::addTax($wallet, $request); | |||||
$result = $provider->payment($wallet, $request); | $result = $provider->payment($wallet, $request); | ||||
return !empty($result); | return !empty($result); | ||||
} | } | ||||
/** | /** | ||||
* Returns auto-payment mandate info for the specified wallet | * Returns auto-payment mandate info for the specified wallet | ||||
* | * | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | public function payments(Request $request) | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'list' => $result, | 'list' => $result, | ||||
'count' => count($result), | 'count' => count($result), | ||||
'hasMore' => $hasMore, | 'hasMore' => $hasMore, | ||||
'page' => $page, | 'page' => $page, | ||||
]); | ]); | ||||
} | } | ||||
/** | |||||
* Calculates tax for the payment, fills the request with additional properties | |||||
*/ | |||||
protected static function addTax(Wallet $wallet, array &$request): void | |||||
{ | |||||
$request['vat_rate_id'] = null; | |||||
$request['credit_amount'] = $request['amount']; | |||||
if ($rate = $wallet->vatRate()) { | |||||
$request['vat_rate_id'] = $rate->id; | |||||
switch (\config('app.vat.mode')) { | |||||
case 1: | |||||
// In this mode tax is added on top of the payment. The amount | |||||
// to pay grows, but we keep wallet balance without tax. | |||||
$request['amount'] = $request['amount'] + round($request['amount'] * $rate->rate / 100); | |||||
break; | |||||
mollekopf: I think we should be using ceil instead of round, so we always cover at least the tax, | |||||
default: | |||||
// In this mode tax is "swallowed" by the vendor. The payment | |||||
// amount does not change | |||||
break; | |||||
} | |||||
} | |||||
} | |||||
} | } |
I think we should be using ceil instead of round, so we always cover at least the tax,