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\Payment; | |||||
use App\Providers\PaymentProvider; | use App\Providers\PaymentProvider; | ||||
use App\Tenant; | use App\Tenant; | ||||
use App\Wallet; | use App\Wallet; | ||||
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 | ||||
{ | { | ||||
/** | /** | ||||
* Get the auto-payment mandate info. | * Get the auto-payment mandate info. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | protected static function mandateValidate(Request $request, Wallet $wallet) | ||||
} | } | ||||
$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 | ||||
if ( | if ( | ||||
$wallet->balance < 0 | $wallet->balance < 0 | ||||
&& $wallet->balance <= PaymentProvider::MIN_AMOUNT * -1 | && $wallet->balance <= Payment::MIN_AMOUNT * -1 | ||||
&& $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 < Payment::MIN_AMOUNT) { | ||||
$min = $wallet->money(PaymentProvider::MIN_AMOUNT); | $min = $wallet->money(Payment::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 20 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 < Payment::MIN_AMOUNT) { | ||||
$min = $wallet->money(PaymentProvider::MIN_AMOUNT); | $min = $wallet->money(Payment::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); | ||||
} | } | ||||
$currency = $request->currency; | $currency = $request->currency; | ||||
$request = [ | $request = [ | ||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => Payment::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); | self::addTax($wallet, $request); | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | public static function topUpWallet(Wallet $wallet): bool | ||||
if ($wallet->balance + $amount < 0) { | if ($wallet->balance + $amount < 0) { | ||||
// 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' => Payment::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); | self::addTax($wallet, $request); | ||||
Show All 12 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'] = (int) (PaymentProvider::MIN_AMOUNT / 100); | $mandate['amount'] = (int) (Payment::MIN_AMOUNT / 100); | ||||
$mandate['balance'] = 0; | $mandate['balance'] = 0; | ||||
$mandate['isDisabled'] = !empty($mandate['id']) && $settings['mandate_disabled']; | $mandate['isDisabled'] = !empty($mandate['id']) && $settings['mandate_disabled']; | ||||
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; | ||||
} | } | ||||
} | } | ||||
Show All 32 Lines | class PaymentsController extends Controller | ||||
public function hasPayments(Request $request) | public function hasPayments(Request $request) | ||||
{ | { | ||||
$user = $this->guard()->user(); | $user = $this->guard()->user(); | ||||
// TODO: Wallet selection | // TODO: Wallet selection | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$exists = Payment::where('wallet_id', $wallet->id) | $exists = Payment::where('wallet_id', $wallet->id) | ||||
->where('type', PaymentProvider::TYPE_ONEOFF) | ->where('type', Payment::TYPE_ONEOFF) | ||||
->whereIn('status', [ | ->whereIn('status', [ | ||||
PaymentProvider::STATUS_OPEN, | Payment::STATUS_OPEN, | ||||
PaymentProvider::STATUS_PENDING, | Payment::STATUS_PENDING, | ||||
PaymentProvider::STATUS_AUTHORIZED | Payment::STATUS_AUTHORIZED | ||||
]) | ]) | ||||
->exists(); | ->exists(); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'hasPending' => $exists | 'hasPending' => $exists | ||||
]); | ]); | ||||
} | } | ||||
Show All 11 Lines | public function payments(Request $request) | ||||
// TODO: Wallet selection | // TODO: Wallet selection | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$pageSize = 10; | $pageSize = 10; | ||||
$page = intval(request()->input('page')) ?: 1; | $page = intval(request()->input('page')) ?: 1; | ||||
$hasMore = false; | $hasMore = false; | ||||
$result = Payment::where('wallet_id', $wallet->id) | $result = Payment::where('wallet_id', $wallet->id) | ||||
->where('type', PaymentProvider::TYPE_ONEOFF) | ->where('type', Payment::TYPE_ONEOFF) | ||||
->whereIn('status', [ | ->whereIn('status', [ | ||||
PaymentProvider::STATUS_OPEN, | Payment::STATUS_OPEN, | ||||
PaymentProvider::STATUS_PENDING, | Payment::STATUS_PENDING, | ||||
PaymentProvider::STATUS_AUTHORIZED | Payment::STATUS_AUTHORIZED | ||||
]) | ]) | ||||
->orderBy('created_at', 'desc') | ->orderBy('created_at', 'desc') | ||||
->limit($pageSize + 1) | ->limit($pageSize + 1) | ||||
->offset($pageSize * ($page - 1)) | ->offset($pageSize * ($page - 1)) | ||||
->get(); | ->get(); | ||||
if (count($result) > $pageSize) { | if (count($result) > $pageSize) { | ||||
$result->pop(); | $result->pop(); | ||||
▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines |