1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Api\Controllers; |
||
6 | |||
7 | use Gewaer\Models\AppsPlans; |
||
8 | use Gewaer\Exception\UnauthorizedHttpException; |
||
9 | use Gewaer\Exception\NotFoundHttpException; |
||
10 | use Stripe\Token as StripeToken; |
||
11 | use Phalcon\Http\Response; |
||
12 | use Phalcon\Validation; |
||
13 | use Phalcon\Validation\Validator\PresenceOf; |
||
14 | use Gewaer\Exception\UnprocessableEntityHttpException; |
||
15 | use Phalcon\Cashier\Subscription; |
||
16 | use Baka\Auth\Models\UserCompanyApps; |
||
17 | |||
18 | /** |
||
19 | * Class LanguagesController |
||
20 | * |
||
21 | * @package Gewaer\Api\Controllers |
||
22 | * |
||
23 | */ |
||
24 | class AppsPlansController extends BaseController |
||
25 | { |
||
26 | /* |
||
27 | * fields we accept to create |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $createFields = []; |
||
32 | |||
33 | /* |
||
34 | * fields we accept to create |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $updateFields = []; |
||
39 | |||
40 | /** |
||
41 | * set objects |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public function onConstruct() |
||
46 | { |
||
47 | $this->model = new AppsPlans(); |
||
48 | $this->additionalSearchFields = [ |
||
49 | ['is_deleted', ':', 0], |
||
50 | ['apps_id', ':', $this->app->getId()], |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
51 | ]; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Given the app plan stripe id , subscribe the current company to this aps |
||
56 | * |
||
57 | * @param string $stripeId |
||
58 | * @return void |
||
59 | */ |
||
60 | public function create(): Response |
||
61 | { |
||
62 | if (!$this->userData->hasRole('Default.Admins')) { |
||
0 ignored issues
–
show
The property
userData does not exist on Gewaer\Api\Controllers\AppsPlansController . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
63 | throw new UnauthorizedHttpException(_('You dont have permission to subscribe this apps')); |
||
64 | } |
||
65 | |||
66 | //Ok let validate user password |
||
67 | $validation = new Validation(); |
||
68 | $validation->add('stripe_id', new PresenceOf(['message' => _('The plan is required.')])); |
||
69 | $validation->add('number', new PresenceOf(['message' => _('Credit Card Number is required.')])); |
||
70 | $validation->add('exp_month', new PresenceOf(['message' => _('Credit Card Number is required.')])); |
||
71 | $validation->add('exp_year', new PresenceOf(['message' => _('Credit Card Number is required.')])); |
||
72 | $validation->add('cvc', new PresenceOf(['message' => _('CVC is required.')])); |
||
73 | |||
74 | //validate this form for password |
||
75 | $messages = $validation->validate($this->request->getPost()); |
||
76 | if (count($messages)) { |
||
77 | foreach ($messages as $message) { |
||
78 | throw new UnprocessableEntityHttpException($message); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $stripeId = $this->request->getPost('stripe_id'); |
||
83 | $company = $this->userData->defaultCompany; |
||
84 | $cardNumber = $this->request->getPost('number'); |
||
85 | $expMonth = $this->request->getPost('exp_month'); |
||
86 | $expYear = $this->request->getPost('exp_year'); |
||
87 | $cvc = $this->request->getPost('cvc'); |
||
88 | |||
89 | $appPlan = $this->model->findFirstByStripeId($stripeId); |
||
90 | |||
91 | if (!$appPlan) { |
||
92 | throw new NotFoundHttpException(_('This plan doesnt exist')); |
||
93 | } |
||
94 | |||
95 | $userSubscription = Subscription::findFirst([ |
||
96 | 'conditions' => 'user_id = ?0 and company_id = ?1 and apps_id = ?2 and is_deleted = 0', |
||
97 | 'bind' => [$this->userData->getId(), $this->userData->default_company, $this->app->getId()] |
||
0 ignored issues
–
show
The property
app does not exist on Gewaer\Api\Controllers\AppsPlansController . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
98 | ]); |
||
99 | |||
100 | if ($userSubscription) { |
||
101 | throw new NotFoundHttpException(_('You are already subscribed to this plan')); |
||
102 | } |
||
103 | |||
104 | $card = StripeToken::create([ |
||
105 | 'card' => [ |
||
106 | 'number' => $cardNumber, |
||
107 | 'exp_month' => $expMonth, |
||
108 | 'exp_year' => $expYear, |
||
109 | 'cvc' => $cvc, |
||
110 | ], |
||
111 | ], [ |
||
112 | 'api_key' => $this->config->stripe->secret |
||
113 | ])->id; |
||
114 | |||
115 | //if fails it will throw exception |
||
116 | if ($appPlan->free_trial_dates == 0) { |
||
117 | $this->userData->newSubscription($appPlan->name, $appPlan->stripe_id, $company, $this->app)->create($card); |
||
118 | } else { |
||
119 | $this->userData->newSubscription($appPlan->name, $appPlan->stripe_id, $company, $this->app)->trialDays($appPlan->free_trial_dates)->create($card); |
||
120 | } |
||
121 | |||
122 | //update company app |
||
123 | $companyApp = UserCompanyApps::findFirst([ |
||
124 | 'conditions' => 'company_id = ?0 and apps_id = ?1', |
||
125 | 'bind' => [$this->userData->defaultCompany->getId(), $this->app->getId()] |
||
126 | ]); |
||
127 | |||
128 | //udpate the company app to the new plan |
||
129 | if ($companyApp) { |
||
130 | $companyApp->strip_id = $stripeId; |
||
131 | $companyApp->subscriptions_id = $this->userData->subscription($appPlan->stripe_plan)->getId(); |
||
132 | $companyApp->update(); |
||
133 | } |
||
134 | |||
135 | //sucess |
||
136 | return $this->response($appPlan); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Cancel a given subscription |
||
141 | * |
||
142 | * @param string $stripeId |
||
143 | * @return boolean |
||
144 | */ |
||
145 | public function edit($stripeId) : Response |
||
146 | { |
||
147 | $appPlan = $this->model->findFirstByStripeId($stripeId); |
||
148 | |||
149 | if (!$appPlan) { |
||
150 | throw new NotFoundHttpException(_('This plan doesnt exist')); |
||
151 | } |
||
152 | |||
153 | $userSubscription = Subscription::findFirst([ |
||
154 | 'conditions' => 'user_id = ?0 and company_id = ?1 and apps_id = ?2 and is_deleted = 0', |
||
155 | 'bind' => [$this->userData->getId(), $this->userData->default_company, $this->app->getId()] |
||
0 ignored issues
–
show
The property
app does not exist on Gewaer\Api\Controllers\AppsPlansController . Since you implemented __get , consider adding a @property annotation.
![]() The property
userData does not exist on Gewaer\Api\Controllers\AppsPlansController . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
156 | ]); |
||
157 | |||
158 | if (!$userSubscription) { |
||
159 | throw new NotFoundHttpException(_('No current subscription found')); |
||
160 | } |
||
161 | |||
162 | $this->userData->subscription($userSubscription->name)->swap($stripeId); |
||
163 | |||
164 | //udpate the company app to the new plan |
||
165 | if ($companyApp) { |
||
166 | $companyApp->strip_id = $stripeId; |
||
167 | $companyApp->subscriptions_id = $this->userData->subscription($stripeId)->getId(); |
||
168 | $companyApp->update(); |
||
169 | } |
||
170 | |||
171 | //return the new subscription plan |
||
172 | return $this->response($appPlan); |
||
0 ignored issues
–
show
|
|||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Cancel a given subscription |
||
177 | * |
||
178 | * @param string $stripeId |
||
179 | * @return boolean |
||
180 | */ |
||
181 | public function delete($stripeId): Response |
||
182 | { |
||
183 | $appPlan = $this->model->findFirstByStripeId($stripeId); |
||
184 | |||
185 | if (!$appPlan) { |
||
186 | throw new NotFoundHttpException(_('This plan doesnt exist')); |
||
187 | } |
||
188 | |||
189 | $userSubscription = Subscription::findFirst([ |
||
190 | 'conditions' => 'user_id = ?0 and company_id = ?1 and apps_id = ?2 and is_deleted = 0', |
||
191 | 'bind' => [$this->userData->getId(), $this->userData->default_company, $this->app->getId()] |
||
0 ignored issues
–
show
The property
userData does not exist on Gewaer\Api\Controllers\AppsPlansController . Since you implemented __get , consider adding a @property annotation.
![]() The property
app does not exist on Gewaer\Api\Controllers\AppsPlansController . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
192 | ]); |
||
193 | |||
194 | if (!$userSubscription) { |
||
195 | throw new NotFoundHttpException(_('No current subscription found')); |
||
196 | } |
||
197 | |||
198 | $subscription = $this->userData->subscription($userSubscription->name)->cancel(); |
||
199 | $subscription->is_deleted = 1; |
||
200 | $subscription->update(); |
||
201 | |||
202 | return $this->response($appPlan); |
||
0 ignored issues
–
show
|
|||
203 | } |
||
204 | } |
||
205 |