Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/PaymentsController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4; | namespace App\Http\Controllers\API\V4; | ||||
use App\Http\Controllers\Controller; | use App\Http\Controllers\Controller; | ||||
use App\Providers\PaymentProvider; | use App\Providers\PaymentProvider; | ||||
use App\Tenant; | |||||
use App\Wallet; | use App\Wallet; | ||||
use App\Payment; | use App\Payment; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
class PaymentsController extends Controller | class PaymentsController extends Controller | ||||
{ | { | ||||
/** | /** | ||||
Show All 33 Lines | public function mandateCreate(Request $request) | ||||
} | } | ||||
$wallet->setSettings([ | $wallet->setSettings([ | ||||
'mandate_amount' => $request->amount, | 'mandate_amount' => $request->amount, | ||||
'mandate_balance' => $request->balance, | 'mandate_balance' => $request->balance, | ||||
]); | ]); | ||||
$mandate = [ | $mandate = [ | ||||
'currency' => 'CHF', | 'currency' => $wallet->currency, | ||||
'description' => \config('app.name') . ' Auto-Payment Setup', | 'description' => Tenant::getConfig($user->tenant_id, 'app.name') . ' Auto-Payment Setup', | ||||
'methodId' => $request->methodId | 'methodId' => $request->methodId | ||||
]; | ]; | ||||
// 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); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | protected static function mandateValidate(Request $request, Wallet $wallet) | ||||
$wallet->balance < 0 | $wallet->balance < 0 | ||||
&& $wallet->balance * -1 > PaymentProvider::MIN_AMOUNT | && $wallet->balance * -1 > PaymentProvider::MIN_AMOUNT | ||||
&& $wallet->balance + $amount < 0 | && $wallet->balance + $amount < 0 | ||||
) { | ) { | ||||
return ['amount' => \trans('validation.minamountdebt')]; | return ['amount' => \trans('validation.minamountdebt')]; | ||||
} | } | ||||
if ($amount < PaymentProvider::MIN_AMOUNT) { | if ($amount < PaymentProvider::MIN_AMOUNT) { | ||||
$min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | $min = $wallet->money(PaymentProvider::MIN_AMOUNT); | ||||
return ['amount' => \trans('validation.minamount', ['amount' => $min])]; | return ['amount' => \trans('validation.minamount', ['amount' => $min])]; | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
/** | /** | ||||
* Create a new payment. | * Create a new payment. | ||||
Show All 21 Lines | public function store(Request $request) | ||||
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) ($request->amount * 100); | ||||
// 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 = $wallet->money(PaymentProvider::MIN_AMOUNT); | ||||
$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); | ||||
} | } | ||||
$request = [ | $request = [ | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'currency' => $request->currency, | 'currency' => $request->currency, | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'methodId' => $request->methodId, | 'methodId' => $request->methodId, | ||||
'description' => \config('app.name') . ' Payment', | 'description' => Tenant::getConfig($user->tenant_id, 'app.name') . ' Payment', | ||||
]; | ]; | ||||
$provider = PaymentProvider::factory($wallet); | $provider = PaymentProvider::factory($wallet); | ||||
$result = $provider->payment($wallet, $request); | $result = $provider->payment($wallet, $request); | ||||
$result['status'] = 'success'; | $result['status'] = 'success'; | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | public static function topUpWallet(Wallet $wallet): bool | ||||
// Disable (not remove) the mandate | // Disable (not remove) the mandate | ||||
$wallet->setSetting('mandate_disabled', 1); | $wallet->setSetting('mandate_disabled', 1); | ||||
\App\Jobs\PaymentMandateDisabledEmail::dispatch($wallet); | \App\Jobs\PaymentMandateDisabledEmail::dispatch($wallet); | ||||
return false; | return false; | ||||
} | } | ||||
$request = [ | $request = [ | ||||
'type' => PaymentProvider::TYPE_RECURRING, | 'type' => PaymentProvider::TYPE_RECURRING, | ||||
'currency' => 'CHF', | 'currency' => $wallet->currency, | ||||
'amount' => $amount, | 'amount' => $amount, | ||||
'methodId' => PaymentProvider::METHOD_CREDITCARD, | 'methodId' => PaymentProvider::METHOD_CREDITCARD, | ||||
'description' => \config('app.name') . ' Recurring Payment', | 'description' => Tenant::getConfig($wallet->owner->tenant_id, 'app.name') . ' Recurring Payment', | ||||
]; | ]; | ||||
$result = $provider->payment($wallet, $request); | $result = $provider->payment($wallet, $request); | ||||
return !empty($result); | return !empty($result); | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | public function payments(Request $request) | ||||
->offset($pageSize * ($page - 1)) | ->offset($pageSize * ($page - 1)) | ||||
->get(); | ->get(); | ||||
if (count($result) > $pageSize) { | if (count($result) > $pageSize) { | ||||
$result->pop(); | $result->pop(); | ||||
$hasMore = true; | $hasMore = true; | ||||
} | } | ||||
$result = $result->map(function ($item) { | $result = $result->map(function ($item) use ($wallet) { | ||||
$provider = PaymentProvider::factory($item->provider); | $provider = PaymentProvider::factory($item->provider); | ||||
$payment = $provider->getPayment($item->id); | $payment = $provider->getPayment($item->id); | ||||
$entry = [ | $entry = [ | ||||
'id' => $item->id, | 'id' => $item->id, | ||||
'createdAt' => $item->created_at->format('Y-m-d H:i'), | 'createdAt' => $item->created_at->format('Y-m-d H:i'), | ||||
'type' => $item->type, | 'type' => $item->type, | ||||
'description' => $item->description, | 'description' => $item->description, | ||||
'amount' => $item->amount, | 'amount' => $item->amount, | ||||
'currency' => $wallet->currency, | |||||
// note: $item->currency/$item->currency_amount might be different | |||||
'status' => $item->status, | 'status' => $item->status, | ||||
'isCancelable' => $payment['isCancelable'], | 'isCancelable' => $payment['isCancelable'], | ||||
'checkoutUrl' => $payment['checkoutUrl'] | 'checkoutUrl' => $payment['checkoutUrl'] | ||||
]; | ]; | ||||
return $entry; | return $entry; | ||||
}); | }); | ||||
Show All 9 Lines |