File size: 14,345 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php

namespace App\Http\Controllers\Finance;

use App\Http\Controllers\Controller;
use App\Models\Currency;
use App\Models\Gateways;
use App\Models\GatewayTax;
use App\Services\GatewaySelector;
use App\Services\PaymentGateways\CoingateService;
use App\Services\PaymentGateways\CryptomusService;
use App\Services\PaymentGateways\IyzicoService;
use App\Services\PaymentGateways\MidtransService;
use App\Services\PaymentGateways\PaddleService;
use App\Services\PaymentGateways\PayPalService;
use App\Services\PaymentGateways\PaystackService;
use App\Services\PaymentGateways\RazorpayService;
use App\Services\PaymentGateways\RevenueCatService;
use App\Services\PaymentGateways\StripeService;
use App\Services\PaymentGateways\YokassaService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use RachidLaasri\LaravelInstaller\Repositories\ApplicationStatusRepositoryInterface;

// Controls ALL Payment Gateway actions
class GatewayController extends Controller
{
    public function __construct(
        public ApplicationStatusRepositoryInterface $applicationStatusRepository
    ) {}

    // Helper functions
    public function gatewayCodesArray(): array
    {
        $paymentGateways = [
            'stripe',
            'paypal',
            'yokassa',
            'iyzico',
            'paystack',
            'banktransfer',
            'revenuecat',
            'coingate',
            'paddle',
            'razorpay',
        ];

        if (class_exists('App\Services\PaymentGateways\CryptomusService')) {
            $paymentGateways[] = 'cryptomus';
        }

        if (class_exists('App\Services\PaymentGateways\MidtransService')) {
            $paymentGateways[] = 'midtrans';
        }

        return $paymentGateways;
    }

    public function defaultGatewayDefinitions(): array
    {
        $gateways = [
            StripeService::gatewayDefinitionArray(),
            PayPalService::gatewayDefinitionArray(),
            YokassaService::gatewayDefinitionArray(),
            IyzicoService::gatewayDefinitionArray(),
            $this->bankTransferArray(),
            PaystackService::gatewayDefinitionArray(),
            RevenueCatService::gatewayDefinitionArray(),
            CoingateService::gatewayDefinitionArray(),
            PaddleService::gatewayDefinitionArray(),
            RazorpayService::gatewayDefinitionArray(),
        ];

        if (class_exists('App\Services\PaymentGateways\CryptomusService')) {
            $gateways[] = CryptomusService::gatewayDefinitionArray();
        }
        if (class_exists('App\Services\PaymentGateways\MidtransService')) {
            $gateways[] = MidtransService::gatewayDefinitionArray();
        }

        return $gateways;
    }

    public function bankTransferArray(): array
    {
        return [
            'code'                  => 'banktransfer',
            'title'                 => 'Bank Transfer',
            'link'                  => '',
            'active'                => 0,                      //if user activated this gateway - dynamically filled in main page
            'available'             => 1,                   //if gateway is available to use
            'img'                   => '/assets/img/payments/banktransfer.png',
            'whiteLogo'             => 0,                   //if gateway logo is white
            'mode'                  => 0,                        // Option in settings - Automatically set according to the "Development" mode. "Development" ? sandbox : live (PAYPAL - 1)
            'sandbox_client_id'     => 0,           // Option in settings 0-Hidden 1-Visible
            'sandbox_client_secret' => 0,       // Option in settings
            'sandbox_app_id'        => 0,              // Option in settings
            'live_client_id'        => 0,              // Option in settings
            'live_client_secret'    => 0,          // Option in settings
            'live_app_id'           => 0,                 // Option in settings
            'currency'              => 1,                    // Option in settings
            'currency_locale'       => 0,             // Option in settings
            'base_url'              => 0,                    // Option in settings
            'sandbox_url'           => 0,                 // Option in settings
            'locale'                => 0,                      // Option in settings
            'validate_ssl'          => 0,                // Option in settings
            'logger'                => 0,                      // Option in settings
            'notify_url'            => 0,                  // Gateway notification url at our side
            'webhook_secret'        => 0,              // Option in settings
            'tax'                   => 1,              // Option in settings
            'bank_account_details'  => 1,
            'bank_account_other'    => 1,
        ];
    }

    public function readManageGatewaysPageData()
    {

        $defaultGateways = self::defaultGatewayDefinitions();
        $requiredGatewayData = [];

        $gatewayActiveData = [];
        $gatewaysData = Gateways::all();
        foreach ($gatewaysData as $gw) {
            array_push($gatewayActiveData, [
                'code'      => $gw->code,
                'is_active' => $gw->is_active,
            ]);
        }

        foreach ($defaultGateways as $gateway) {
            $code = $gateway['code'];
            $is_active = 0;
            foreach ($gatewaysData as $gwdata) {
                if ($gwdata['code'] == $code) {
                    $is_active = $gwdata['is_active'];

                    break;
                }
            }
            array_push($requiredGatewayData, [
                'code'      => $code,
                'title'     => $gateway['title'],
                'link'      => $gateway['link'],
                'available' => $gateway['available'],
                'img'       => $gateway['img'],
                'whiteLogo' => $gateway['whiteLogo'],
                'active'    => $is_active ?? 0,
            ]);
        }

        return $requiredGatewayData;
    }

    public function getCurrencyOptions($index)
    {
        $returnText = '';
        $currencies = Currency::all();
        foreach ($currencies as $currency) {
            $cindex = $currency->id;
            $country = self::appendNBSPtoString($currency->country, 41);
            $code = self::appendNBSPtoString($currency->code, 5);
            $text = $country . $code . $currency->symbol;
            $selected = (int) $index == (int) $cindex ? 'selected' : '';
            $returnText = $returnText . '<option value="' . $cindex . '" ' . $selected . ' style=\'font-family: "Courier New", Courier, monospace;\' >' . $text . '</option>';
        }

        return $returnText;
    }

    public function appendNBSPtoString($stringForAppend, $charCount) // Fills given string with &nbsp; at the end. Used in Country select tag.
    {
        $length = Str::length($stringForAppend);
        $remainingCharcount = $charCount - $length;
        if ($remainingCharcount < 1) {
            return $stringForAppend;
        } else {
            $newString = $stringForAppend;
            for ($i = 1; $i <= $remainingCharcount; $i++) {
                $newString = $newString . '&nbsp;';
            }

            return $newString;
        }
    }

    // Main functions
    public function paymentGateways() // Index page of Payment Gateways in Admin Panel
    {
        $gateways = self::readManageGatewaysPageData();

        return view(
            'panel.admin.finance.gateways.index', [
                'gateways' => $gateways,
                'view'     => view($this->applicationStatusRepository->financePage(), [
                    'gateways' => $gateways,
                ])->render(),
            ]
        );
    }

    // Settings page of gateways in Admin Panel
    public function gatewaySettings($code)
    {

        if (! in_array($code, self::gatewayCodesArray())) {
            abort(404);
        }

        $settings = Gateways::query()->where('code', $code)->first();

        if ($settings != null) {
        } else {
            $settings = new Gateways;
            $settings->code = $code;
            $settings->is_active = 0;
            $settings->currency = '124'; //Default currency for Stripe - USD
            $settings->save();
        }
        $currencies = self::getCurrencyOptions($settings->currency);
        $gateways = self::defaultGatewayDefinitions();
        $options = $gateways[0];
        foreach ($gateways as $gateway) {
            if ($gateway['code'] == $code) {
                $options = $gateway;

                break;
            }
        }

        $taxes = GatewayTax::query()
            ->where('gateway_id', $settings->id)
            ->get();

        return view('panel.admin.finance.gateways.settings', compact('settings', 'currencies', 'options', 'taxes'));
    }

    public function countryTaxEnabled($code)
    {
        $settings = Gateways::query()->where('code', $code)->firstOrFail();

        $settings->update([
            'country_tax_enabled' => ! $settings->country_tax_enabled,
        ]);

        return response()->json([
            'message' => 'Setting updated successfully.',
        ]);
    }

    public function gatewaySettingsSave(Request $request) // Save settings of gateway in Admin Panel
    {
        if ($request->code != null) {
            if (! in_array($request->code, self::gatewayCodesArray())) {
                abort(404);
            }
        } else {
            abort(404);
        }
        // return 404 error if the system currency is not the same as the gateway currency
        if ($request->code == 'paystack' && $request->currency != currency()->id) {
            return back()->with(['message' => __('Paystack default currency not the same with the system default currency.'), 'type' => 'error']);
        }

        DB::beginTransaction();
        $gw_settings = Gateways::where('code', $request->code)->first();
        if ($gw_settings != null) {
            if ($request->is_active == 'on') {
                $gw_settings->is_active = 1;
            } else {
                $gw_settings->is_active = 0;
            }
            $propertiesToUpdate = [
                'title', 'currency', 'currency_locale', 'live_client_id', 'live_client_secret',
                'live_app_id', 'sandbox_client_id', 'sandbox_client_secret', 'sandbox_app_id',
                'base_url', 'sandbox_url', 'mode', 'bank_account_other', 'bank_account_details',
            ];
            foreach ($propertiesToUpdate as $property) {
                if (isset($request->$property)) {
                    $gw_settings->$property = $request->$property ?? $gw_settings->$property;
                }
            }
            $gw_settings->save();

            if ($gw_settings->is_active == 1) {
                try {
                    $temp = GatewaySelector::selectGateway($request->code)::saveAllProducts(); // Update all product ids' and create new price ids'
                } catch (Exception $ex) {
                    DB::rollBack();
                    Log::error("GatewayController::gatewaySettingsSave()\n" . $ex->getMessage());

                    return back()->with(['message' => $ex->getMessage(), 'type' => 'error']);
                }
            }
        } else {
            $settings = new Gateways;
            $settings->code = $request->code;
            $settings->is_active = 0;
            $settings->currency = '124'; //Default currency for Stripe - USD
            $settings->save();
        }
        DB::commit();

        return back()->with(['message' => __('Product ID and Price ID of all membership plans are generated.'), 'type' => 'success']);
    }

    public function gatewaySettingsTaxSave(Request $request) // Save settings of gateway in Admin Panel
    {
        $gw_settings = Gateways::query()->where('code', $request->code)->firstOrFail();

        // return 404 error if the system currency is not the same as the gateway currency
        if ($request->code == 'paystack' && $request->currency != currency()->id) {
            return back()->with(['message' => __('Paystack default currency not the same with the system default currency.'), 'type' => 'error']);
        }

        if ($request->code == 'cryptomus') {
            GatewayTax::query()
                ->updateOrCreate([
                    'gateway_id'   => $gw_settings->id,
                    'country_code' => $request->get('country_code'),
                ], [
                    'tax' => $request->get('tax'),
                ]);
        } else {
            DB::beginTransaction();

            $gw_settings->tax = $request->tax ?? $gw_settings->tax;
            $gw_settings->save();

            DB::commit();
        }

        return back()->with(['message' => __('Tax saved succesfully.'), 'type' => 'success']);
    }

    public function gatewaySettingsTaxDelete($id)
    {
        $gatewayTax = GatewayTax::query()->findOrFail($id);

        $gatewayTax->delete();

        return back()->with([
            'type'    => 'success',
            'message' => __('Tax deleted successfully.'),
        ]);
    }

    public function gatewayData($code)
    {
        $gateways = self::defaultGatewayDefinitions();
        $options = $gateways[0];
        foreach ($gateways as $gateway) {
            if ($gateway['code'] == $code) {
                $options = $gateway;

                break;
            }
        }

        return $options;
    }

    public static function checkGatewayWebhooks(): void
    {
        $host = $_SERVER['HTTP_HOST'];
        if ($host !== 'localhost:8000' && $host !== '127.0.0.1:8000') {
            $gateways = Gateways::all();
            foreach ($gateways as $gateway) {
                if ($gateway->webhook_id == null) {
                    $tmp = GatewaySelector::selectGateway($gateway->code)::createWebhook();
                }
            }
            Log::info('All gateways are checked for webhooks.');
        } else {
            Log::info('Webhooks are not available on localhost. Skipping checkGatewayWebhooks()...');
        }
    }
}