File size: 4,806 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
<?php

namespace App\Http\Controllers\Finance;

use App\Http\Controllers\Controller;
use App\Models\GatewayProducts;
use App\Models\PaymentPlans;
use App\Models\RevenueCatProducts;
//use this for subscription table records
use Illuminate\Http\Request;

/**
 * Controls ALL Payment actions of Mobile ( Android & iOS )
 *
 * Also, controls settings of Mobile ( Android & iOS )
 */
class MobilePaymentsController extends Controller
{
    public function mobilePlanIdSettings(Request $request)
    {

        if ($request->isMethod('post')) {

            if ($request->plan_id == 0 || $request->plan_id == '' || $request->plan_id == null) {
                return back()->with(['message' => __('Please select a plan.'), 'type' => 'error']);
            }

            if ($request->plan_name_label == '') {
                return back()->with(['message' => __('Please enter a plan name.'), 'type' => 'error']);
            }

            if ($request->plan_type_label == '') {
                return back()->with(['message' => __('Please enter a plan type.'), 'type' => 'error']);
            }

            if ($request->revenuecat_package_id == '') {
                return back()->with(['message' => __('Please enter a RevenueCat Package ID.'), 'type' => 'error']);
            }

            if ($request->revenuecat_entitlement_id == '') {
                return back()->with(['message' => __('Please enter a RevenueCat Entitlement ID.'), 'type' => 'error']);
            }

            if ($request->revenuecat_google_id == '') {
                return back()->with(['message' => __('Please enter a RevenueCat Google Product ID.'), 'type' => 'error']);
            }

            if ($request->revenuecat_apple_id == '') {
                return back()->with(['message' => __('Please enter a RevenueCat Apple Product ID.'), 'type' => 'error']);
            }

            $plan_id = $request->plan_id;

            $plan = PaymentPlans::find($plan_id);

            if (! $plan) {
                return redirect()->back()->with('error', 'Plan not found');
            }

            $gatewayProducts = $plan->gateway_products;

            $revenueCatEntitlementFound = false;

            foreach ($gatewayProducts as $gatewayProduct) {
                if ($gatewayProduct->gateway_code == 'revenuecat') {
                    $revenueCatEntitlementFound = true;
                    $gatewayProduct->product_id = $request->revenuecat_package_id;
                    $gatewayProduct->price_id = $request->revenuecat_entitlement_id;
                    $gatewayProduct->save();

                    break;
                }
            }

            if (! $revenueCatEntitlementFound) {
                $gatewayProduct = new GatewayProducts;
                $gatewayProduct->plan_id = $plan_id;
                $gatewayProduct->plan_name = $request->plan_name_label;
                $gatewayProduct->gateway_code = 'revenuecat';
                $gatewayProduct->gateway_title = 'RevenueCat';
                $gatewayProduct->product_id = $request->revenuecat_package_id;
                $gatewayProduct->price_id = $request->revenuecat_entitlement_id;
                $gatewayProduct->save();
            }

            $revenueCatProductFound = false;

            foreach ($plan->revenuecat_products as $revenueCatProduct) {
                if ($revenueCatProduct != null) {
                    $revenueCatProductFound = true;
                    $revenueCatProduct->entitlement_id = $request->revenuecat_entitlement_id;
                    $revenueCatProduct->package_id = $request->revenuecat_package_id;
                    $revenueCatProduct->google_id = $request->revenuecat_google_id;
                    $revenueCatProduct->apple_id = $request->revenuecat_apple_id;
                    $revenueCatProduct->save();

                    break;
                }
            }

            if (! $revenueCatProductFound) {
                $revenueCatProduct = new RevenueCatProducts;
                $revenueCatProduct->plan_id = $plan_id;
                $revenueCatProduct->gatewayproduct_id = $gatewayProduct->id;
                $revenueCatProduct->entitlement_id = $request->revenuecat_entitlement_id;
                $revenueCatProduct->package_id = $request->revenuecat_package_id;
                $revenueCatProduct->google_id = $request->revenuecat_google_id;
                $revenueCatProduct->apple_id = $request->revenuecat_apple_id;
                $revenueCatProduct->save();
            }

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

        $plans = PaymentPlans::with('gateway_products')->with('revenuecat_products')->get();

        return view('panel.admin.finance.mobile.index', compact('plans'));
    }
}