Payment setting frontend part 2 - payment setup front/backend

This commit is contained in:
Čarodej
2022-01-07 12:42:47 +01:00
parent 8b7a80e10d
commit e892e0e94f
20 changed files with 316 additions and 1731 deletions
@@ -1,74 +0,0 @@
<?php
namespace Domain\Settings\Controllers;
use Artisan;
use Cartalyst\Stripe\Stripe;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Domain\Settings\Models\Setting;
use Cartalyst\Stripe\Exception\UnauthorizedException;
use Symfony\Component\HttpKernel\Exception\HttpException;
class SetStripeController
{
/**
* Configure stripe additionally
*/
public function __invoke(Request $request): Response
{
// TODO: pridat validator do requestu
// Check payment setup status
if (get_settings('payments_configured')) {
abort(401, 'Gone');
}
// Try to get stripe account details
try {
if (! app()->runningUnitTests()) {
Stripe::make($request->input('secret'), '2020-03-02')
->account()
->details();
}
} catch (UnauthorizedException $e) {
throw new HttpException(401, $e->getMessage());
}
// Get options
collect([
[
'name' => 'stripe_currency',
'value' => $request->input('currency'),
],
[
'name' => 'payments_configured',
'value' => 1,
],
[
'name' => 'payments_active',
'value' => 1,
],
])->each(function ($col) {
Setting::forceCreate([
'name' => $col['name'],
'value' => $col['value'],
]);
});
if (! app()->runningUnitTests()) {
// Set stripe credentials to .env
setEnvironmentValue([
'CASHIER_CURRENCY' => $request->input('currency'),
'STRIPE_KEY' => $request->input('key'),
'STRIPE_SECRET' => $request->input('secret'),
'STRIPE_WEBHOOK_SECRET' => $request->input('webhookSecret'),
]);
// Clear cache
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');
}
return response('Done', 204);
}
}
@@ -0,0 +1,84 @@
<?php
namespace Domain\Settings\Controllers;
use Domain\Settings\Requests\StorePaymentServiceCredentialsRequest;
use Domain\Settings\Models\Setting;
use Illuminate\Http\Response;
use Artisan;
class StorePaymentServiceCredentialsController
{
/**
* Configure stripe additionally
*/
public function __invoke(StorePaymentServiceCredentialsRequest $request): Response
{
// Abort in demo mode
abort_if(is_demo(), 204, 'Done.');
// Try to get stripe account details
/*try {
if (!app()->runningUnitTests()) {
Stripe::make($request->input('secret'), '2020-03-02')
->account()
->details();
}
} catch (UnauthorizedException $e) {
throw new HttpException(401, $e->getMessage());
}*/
$options = [
'stripe' => [
'name' => 'allowed_stripe',
'value' => 1,
],
'paypal' => [
'name' => 'allowed_paypal',
'value' => 1,
],
'paystack' => [
'name' => 'allowed_paystack',
'value' => 1,
],
];
// Get options
collect([$options[$request->input('service')]])
->each(fn($setting) => Setting::updateOrCreate([
'name' => $setting['name'],
], [
'value' => $setting['value'],
]));
// Get and store credentials
if (!app()->runningUnitTests()) {
$credentials = [
'stripe' => [
'STRIPE_PUBLIC_KEY' => $request->input('key'),
'STRIPE_SECRET_KEY' => $request->input('secret'),
],
'paystack' => [
'PAYSTACK_PUBLIC_KEY' => $request->input('key'),
'PAYSTACK_SECRET' => $request->input('secret'),
],
'paypal' => [
'PAYPAL_CLIENT_ID' => $request->input('key'),
'PAYPAL_CLIENT_SECRET' => $request->input('secret'),
],
];
// Store credentials into the .env file
setEnvironmentValue($credentials[$request->input('service')]);
// Clear cache
if (! is_dev()) {
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');
}
}
return response('Done', 204);
}
}
@@ -0,0 +1,32 @@
<?php
namespace Domain\Settings\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePaymentServiceCredentialsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'key' => 'required|string',
'secret' => 'required|string',
'webhook' => 'sometimes|string',
];
}
}