|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Canvas\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Canvas\Models\AppsPlans; |
|
8
|
|
|
use Stripe\Token as StripeToken; |
|
|
|
|
|
|
9
|
|
|
use Phalcon\Http\Response; |
|
|
|
|
|
|
10
|
|
|
use Stripe\Customer as StripeCustomer; |
|
|
|
|
|
|
11
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
|
|
|
|
|
|
12
|
|
|
use Canvas\Http\Exception\NotFoundException; |
|
13
|
|
|
use Canvas\Http\Exception\UnauthorizedException; |
|
14
|
|
|
use Canvas\Http\Exception\UnprocessableEntityException; |
|
15
|
|
|
use Canvas\Models\Subscription as CanvasSubscription; |
|
16
|
|
|
use Phalcon\Cashier\Subscription; |
|
|
|
|
|
|
17
|
|
|
use Canvas\Models\UserCompanyApps; |
|
18
|
|
|
use function Canvas\Core\paymentGatewayIsActive; |
|
19
|
|
|
use Canvas\Validation as CanvasValidation; |
|
20
|
|
|
use Canvas\Models\PaymentMethodsCreds; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class LanguagesController. |
|
24
|
|
|
* |
|
25
|
|
|
* @package Canvas\Api\Controllers |
|
26
|
|
|
* |
|
27
|
|
|
* @property Users $userData |
|
28
|
|
|
* @property Request $request |
|
29
|
|
|
* @property Config $config |
|
30
|
|
|
* @property Apps $app |
|
31
|
|
|
* @property \Phalcon\Db\Adapter\Pdo\Mysql $db |
|
32
|
|
|
*/ |
|
33
|
|
|
class AppsPlansController extends BaseController |
|
34
|
|
|
{ |
|
35
|
|
|
/* |
|
36
|
|
|
* fields we accept to create |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $createFields = []; |
|
41
|
|
|
|
|
42
|
|
|
/* |
|
43
|
|
|
* fields we accept to create |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $updateFields = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* set objects. |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function onConstruct() |
|
55
|
|
|
{ |
|
56
|
|
|
if (!$this->userData->hasRole('Default.Admins') || (int) $id === 0) { |
|
|
|
|
|
|
57
|
|
|
$id = $this->userData->getId(); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->userData->can('Apps-plans.update', true); |
|
61
|
|
|
|
|
62
|
|
|
$this->model = new AppsPlans(); |
|
|
|
|
|
|
63
|
|
|
$this->additionalSearchFields = [ |
|
|
|
|
|
|
64
|
|
|
['is_deleted', ':', '0'], |
|
65
|
|
|
['apps_id', ':', $this->app->getId()], |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Update a given subscription. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $stripeId |
|
73
|
|
|
* @return Response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function edit($stripeId) : Response |
|
76
|
|
|
{ |
|
77
|
|
|
$appPlan = $this->model->findFirstByStripeId($stripeId); |
|
78
|
|
|
|
|
79
|
|
|
if (!is_object($appPlan)) { |
|
80
|
|
|
throw new NotFoundException(_('This plan doesnt exist')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$userSubscription = CanvasSubscription::getActiveForThisApp(); |
|
84
|
|
|
|
|
85
|
|
|
$this->db->begin(); |
|
86
|
|
|
|
|
87
|
|
|
$subscription = $this->userData->subscription($userSubscription->name); |
|
88
|
|
|
|
|
89
|
|
|
if ($subscription->onTrial()) { |
|
90
|
|
|
$subscription->name = $appPlan->name; |
|
91
|
|
|
$subscription->stripe_plan = $appPlan->stripe_plan; |
|
92
|
|
|
} else { |
|
93
|
|
|
$subscription->swap($stripeId); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
//update company app |
|
97
|
|
|
$companyApp = UserCompanyApps::getCurrentApp(); |
|
98
|
|
|
|
|
99
|
|
|
//update the company app to the new plan |
|
100
|
|
|
if (is_object($companyApp)) { |
|
101
|
|
|
$subscription->name = $stripeId; |
|
102
|
|
|
$subscription->save(); |
|
103
|
|
|
|
|
104
|
|
|
$companyApp->stripe_id = $stripeId; |
|
105
|
|
|
$companyApp->subscriptions_id = $subscription->getId(); |
|
106
|
|
|
if (!$companyApp->update()) { |
|
107
|
|
|
$this->db->rollback(); |
|
108
|
|
|
throw new UnprocessableEntityException((string) current($companyApp->getMessages())); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
//update the subscription with the plan |
|
112
|
|
|
$subscription->apps_plans_id = $appPlan->getId(); |
|
113
|
|
|
if (!$subscription->update()) { |
|
114
|
|
|
$this->db->rollback(); |
|
115
|
|
|
|
|
116
|
|
|
throw new UnprocessableEntityException((string) current($subscription->getMessages())); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->db->commit(); |
|
121
|
|
|
|
|
122
|
|
|
//return the new subscription plan |
|
123
|
|
|
return $this->response($appPlan); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Cancel a given subscription. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $stripeId |
|
130
|
|
|
* @return Response |
|
131
|
|
|
*/ |
|
132
|
|
|
public function delete($stripeId): Response |
|
133
|
|
|
{ |
|
134
|
|
|
$appPlan = $this->model->findFirstByStripeId($stripeId); |
|
135
|
|
|
|
|
136
|
|
|
if (!is_object($appPlan)) { |
|
137
|
|
|
throw new NotFoundException(_('This plan doesnt exist')); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$subscription = CanvasSubscription::getActiveSubscription(); |
|
141
|
|
|
|
|
142
|
|
|
//if on trial you can cancel without going to stripe |
|
143
|
|
|
if (!$subscription->onTrial()) { |
|
144
|
|
|
$subscription->cancel(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$subscription->is_cancelled = 1; |
|
148
|
|
|
$subscription->update(); |
|
149
|
|
|
|
|
150
|
|
|
return $this->response($appPlan); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Reactivate a given subscription. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $stripeId |
|
157
|
|
|
* @return Response |
|
158
|
|
|
*/ |
|
159
|
|
|
public function reactivateSubscription($stripeId): Response |
|
160
|
|
|
{ |
|
161
|
|
|
$appPlan = $this->model->findFirstByStripeId($stripeId); |
|
162
|
|
|
|
|
163
|
|
|
if (!is_object($appPlan)) { |
|
164
|
|
|
throw new NotFoundException(_('This plan doesnt exist')); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$subscription = CanvasSubscription::getActiveSubscription(); |
|
168
|
|
|
|
|
169
|
|
|
//if on trial you can cancel without going to stripe |
|
170
|
|
|
if (!$subscription->onTrial()) { |
|
171
|
|
|
$subscription->reactivate(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$subscription->is_cancelled = 0; |
|
175
|
|
|
$subscription->update(); |
|
176
|
|
|
|
|
177
|
|
|
return $this->response($appPlan); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Update payment method. |
|
182
|
|
|
* @param integer $id |
|
183
|
|
|
* @return Response |
|
184
|
|
|
*/ |
|
185
|
|
|
public function updatePaymentMethod(string $id): Response |
|
186
|
|
|
{ |
|
187
|
|
|
if (empty($this->request->hasPut('card_token'))) { |
|
188
|
|
|
$validation = new CanvasValidation(); |
|
189
|
|
|
$validation->add('card_number', new PresenceOf(['message' => _('Credit Card Number is required.')])); |
|
190
|
|
|
$validation->add('card_exp_month', new PresenceOf(['message' => _('Credit Card expiration month is required.')])); |
|
191
|
|
|
$validation->add('card_exp_year', new PresenceOf(['message' => _('Credit Card expiration year is required.')])); |
|
192
|
|
|
$validation->add('card_cvc', new PresenceOf(['message' => _('CVC is required.')])); |
|
193
|
|
|
|
|
194
|
|
|
//validate this form for password |
|
195
|
|
|
$validation->validate($this->request->getPut()); |
|
196
|
|
|
|
|
197
|
|
|
$cardNumber = $this->request->getPut('card_number', 'string'); |
|
198
|
|
|
$expMonth = $this->request->getPut('card_exp_month', 'string'); |
|
199
|
|
|
$expYear = $this->request->getPut('card_exp_year', 'string'); |
|
200
|
|
|
$cvc = $this->request->getPut('card_cvc', 'string'); |
|
201
|
|
|
|
|
202
|
|
|
//Create a new card token |
|
203
|
|
|
$token = StripeToken::create([ |
|
204
|
|
|
'card' => [ |
|
205
|
|
|
'number' => $cardNumber, |
|
206
|
|
|
'exp_month' => $expMonth, |
|
207
|
|
|
'exp_year' => $expYear, |
|
208
|
|
|
'cvc' => $cvc, |
|
209
|
|
|
], |
|
210
|
|
|
], [ |
|
211
|
|
|
'api_key' => $this->config->stripe->secret |
|
212
|
|
|
])->id; |
|
213
|
|
|
} else { |
|
214
|
|
|
$token = $this->request->getPut('card_token'); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$address = $this->request->getPut('address', 'string'); |
|
218
|
|
|
$zipcode = $this->request->getPut('zipcode', 'string'); |
|
219
|
|
|
|
|
220
|
|
|
//update the default company info |
|
221
|
|
|
$this->userData->getDefaultCompany()->address = $address; |
|
222
|
|
|
$this->userData->getDefaultCompany()->zipcode = $zipcode; |
|
223
|
|
|
$this->userData->getDefaultCompany()->update(); |
|
224
|
|
|
|
|
225
|
|
|
$customerId = $this->userData->getDefaultCompany()->get('payment_gateway_customer_id'); |
|
226
|
|
|
|
|
227
|
|
|
//Update default payment method with new card. |
|
228
|
|
|
$stripeCustomer = $this->userData->updatePaymentMethod($customerId, $token); |
|
229
|
|
|
|
|
230
|
|
|
$subscription = CanvasSubscription::getActiveForThisApp(); |
|
231
|
|
|
|
|
232
|
|
|
//not valid? ok then lets charge the credit card to active your subscription |
|
233
|
|
|
if (!$subscription->valid()) { |
|
234
|
|
|
$subscription->activate(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
if (is_object($stripeCustomer) && $stripeCustomer instanceof StripeCustomer) { |
|
238
|
|
|
|
|
239
|
|
|
//We now create a partially persist the payment method data |
|
240
|
|
|
PaymentMethodsCreds::createByStripeToken($token); |
|
241
|
|
|
return $this->response($subscription); |
|
242
|
|
|
} |
|
243
|
|
|
return $this->response('Card could not be updated'); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths