1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
6
|
|
|
|
7
|
|
|
use Phalcon\Http\Response; |
8
|
|
|
use Gewaer\Models\Users; |
9
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
10
|
|
|
use Gewaer\Traits\EmailTrait; |
11
|
|
|
use Datetime; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PaymentsController |
15
|
|
|
* |
16
|
|
|
* Class to handle payment webhook from our cashier library |
17
|
|
|
* |
18
|
|
|
* @package Gewaer\Api\Controllers |
19
|
|
|
* @property Log $log |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class PaymentsController extends BaseController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Email Trait |
26
|
|
|
*/ |
27
|
|
|
use EmailTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handle stripe webhoook calls |
31
|
|
|
* |
32
|
|
|
* @return Response |
33
|
|
|
*/ |
34
|
4 |
|
public function handleWebhook(): Response |
35
|
|
|
{ |
36
|
|
|
//we cant processs if we dont find the stripe header |
37
|
4 |
|
if (!defined('API_TESTS')) { |
38
|
|
|
if (!$this->request->hasHeader('Stripe-Signature')) { |
39
|
|
|
throw new NotFoundHttpException('Route not found for this call'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
$request = $this->request->getPost(); |
44
|
|
|
|
45
|
4 |
|
if (empty($request)) { |
46
|
|
|
$request = $this->request->getJsonRawBody(true); |
47
|
|
|
} |
48
|
4 |
|
$type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.')); |
49
|
4 |
|
$method = 'handle' . $type; |
50
|
|
|
|
51
|
4 |
|
$payloadContent = json_encode($request); |
52
|
4 |
|
$this->log->info("Webhook Handler Method: {$method} \n"); |
53
|
4 |
|
$this->log->info("Payload: {$payloadContent} \n"); |
54
|
|
|
|
55
|
4 |
|
if (method_exists($this, $method)) { |
56
|
4 |
|
return $this->{$method}($request); |
57
|
|
|
} else { |
58
|
|
|
return $this->response(['Missing Method to Handled']); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Handle customer subscription updated. |
64
|
|
|
* |
65
|
|
|
* @param array $payload |
66
|
|
|
* @return Response |
67
|
|
|
*/ |
68
|
|
|
protected function handleCustomerSubscriptionUpdated(array $payload): Response |
69
|
|
|
{ |
70
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
71
|
|
|
if ($user) { |
72
|
|
|
$subject = "{$user->firstname} {$user->lastname} Updated Subscription"; |
73
|
|
|
$content = "Dear user {$user->firstname} {$user->lastname}, your subscription has been updated."; |
74
|
|
|
|
75
|
|
|
$template = [ |
76
|
|
|
'subject' => $subject, |
77
|
|
|
'content' => $content |
78
|
|
|
]; |
79
|
|
|
//We need to send a mail to the user |
80
|
|
|
if (!defined('API_TESTS')) { |
81
|
|
|
$this->sendWebhookEmail($user->email, $template); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return $this->response(['Webhook Handled']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Handle a cancelled customer from a Stripe subscription. |
89
|
|
|
* |
90
|
|
|
* @param array $payload |
91
|
|
|
* @return Response |
92
|
|
|
*/ |
93
|
|
|
protected function handleCustomerSubscriptionDeleted(array $payload): Response |
94
|
|
|
{ |
95
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
96
|
|
|
if ($user) { |
97
|
|
|
$subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']); |
98
|
|
|
|
99
|
|
|
if (is_object($subscription)) { |
100
|
|
|
$subscription->markAsCancelled(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return $this->response(['Webhook Handled']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Handle customer subscription free trial ending. |
108
|
|
|
* |
109
|
|
|
* @param array $payload |
110
|
|
|
* @return Response |
111
|
|
|
*/ |
112
|
1 |
|
protected function handleCustomerSubscriptionTrialwillend(array $payload): Response |
113
|
|
|
{ |
114
|
1 |
|
$trialEndDate = new Datetime(); |
115
|
1 |
|
$trialEndDate->setTimestamp((int)$payload['data']['object']['trial_end']); |
116
|
1 |
|
$formatedEndDate = $trialEndDate->format('Y-m-d H:i:s'); |
117
|
|
|
|
118
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
119
|
1 |
|
if ($user) { |
120
|
|
|
$subject = "{$user->firstname} {$user->lastname} Free Trial Ending"; |
121
|
|
|
$content = "Dear user {$user->firstname} {$user->lastname}, your free trial is coming to an end on {$formatedEndDate}.Please choose one of our available subscriptions. Thank you"; |
122
|
|
|
|
123
|
|
|
$template = [ |
124
|
|
|
'subject' => $subject, |
125
|
|
|
'content' => $content |
126
|
|
|
]; |
127
|
|
|
//We need to send a mail to the user |
128
|
|
|
if (!defined('API_TESTS')) { |
129
|
|
|
$this->sendWebhookEmail($user->email, $template); |
130
|
|
|
} |
131
|
|
|
} |
132
|
1 |
|
return $this->response(['Webhook Handled']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Handle customer updated. |
137
|
|
|
* |
138
|
|
|
* @param array $payload |
139
|
|
|
* @return Response |
140
|
|
|
*/ |
141
|
|
|
protected function handleCustomerUpdated(array $payload): Response |
142
|
|
|
{ |
143
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) { |
144
|
|
|
$user->updateCardFromStripe(); |
145
|
|
|
} |
146
|
|
|
return $this->response(['Webhook Handled']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Handle customer source deleted. |
151
|
|
|
* |
152
|
|
|
* @param array $payload |
153
|
|
|
* @return Response |
154
|
|
|
*/ |
155
|
|
|
protected function handleCustomerSourceDeleted(array $payload) : Response |
156
|
|
|
{ |
157
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) { |
158
|
|
|
$user->updateCardFromStripe(); |
159
|
|
|
} |
160
|
|
|
return $this->response(['Webhook Handled']); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Handle deleted customer. |
165
|
|
|
* |
166
|
|
|
* @param array $payload |
167
|
|
|
* @return Response |
168
|
|
|
*/ |
169
|
|
|
protected function handleCustomerDeleted(array $payload) : Response |
170
|
|
|
{ |
171
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['id']); |
172
|
|
|
if ($user) { |
173
|
|
|
foreach ($user->subscriptions as $subscription) { |
174
|
|
|
$subscription->skipTrial()->markAsCancelled(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$user->stripe_id = null; |
178
|
|
|
$user->trial_ends_at = null; |
179
|
|
|
$user->card_brand = null; |
180
|
|
|
$user->card_last_four = null; |
181
|
|
|
$user->update(); |
182
|
|
|
} |
183
|
|
|
return $this->response(['Webhook Handled']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Handle sucessfull payment |
188
|
|
|
* |
189
|
|
|
* @todo send email |
190
|
|
|
* @param array $payload |
191
|
|
|
* @return Response |
192
|
|
|
*/ |
193
|
1 |
|
protected function handleChargeSucceeded(array $payload): Response |
194
|
|
|
{ |
195
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
196
|
1 |
|
if ($user) { |
197
|
|
|
$subject = "{$user->firstname} {$user->lastname} Successful Payment"; |
198
|
|
|
$content = "Dear user {$user->firstname} {$user->lastname}, your subscription payment of {$payload['data']['object']['amount']} was successful. Thank you"; |
199
|
|
|
|
200
|
|
|
$template = [ |
201
|
|
|
'subject' => $subject, |
202
|
|
|
'content' => $content |
203
|
|
|
]; |
204
|
|
|
//We need to send a mail to the user |
205
|
|
|
if (!defined('API_TESTS')) { |
206
|
|
|
$this->sendWebhookEmail($user->email, $template); |
207
|
|
|
} |
208
|
|
|
} |
209
|
1 |
|
return $this->response(['Webhook Handled']); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Handle bad payment |
214
|
|
|
* |
215
|
|
|
* @todo send email |
216
|
|
|
* @param array $payload |
217
|
|
|
* @return Response |
218
|
|
|
*/ |
219
|
1 |
|
protected function handleChargeFailed(array $payload) : Response |
220
|
|
|
{ |
221
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
222
|
1 |
|
if ($user) { |
223
|
|
|
$subject = "{$user->firstname} {$user->lastname} Failed Payment"; |
224
|
|
|
$content = "Dear user {$user->firstname} {$user->lastname}, your subscription presents a failed payment of the amount of {$payload['data']['object']['amount']}. Please check card expiration date"; |
225
|
|
|
|
226
|
|
|
$template = [ |
227
|
|
|
'subject' => $subject, |
228
|
|
|
'content' => $content |
229
|
|
|
]; |
230
|
|
|
//We need to send a mail to the user |
231
|
|
|
if (!defined('API_TESTS')) { |
232
|
|
|
$this->sendWebhookEmail($user->email, $template); |
233
|
|
|
} |
234
|
|
|
} |
235
|
1 |
|
return $this->response(['Webhook Handled']); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Handle looking for refund |
240
|
|
|
* |
241
|
|
|
* @todo send email |
242
|
|
|
* @param array $payload |
243
|
|
|
* @return Response |
244
|
|
|
*/ |
245
|
|
|
protected function handleChargeDisputeCreated(array $payload) : Response |
246
|
|
|
{ |
247
|
|
|
return $this->response(['Webhook Handled']); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Handle pending payments |
252
|
|
|
* |
253
|
|
|
* @todo send email |
254
|
|
|
* @param array $payload |
255
|
|
|
* @return Response |
256
|
|
|
*/ |
257
|
1 |
|
protected function handleChargePending(array $payload) : Response |
258
|
|
|
{ |
259
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
260
|
1 |
|
if ($user) { |
261
|
|
|
$subject = "{$user->firstname} {$user->lastname} Pending Payment"; |
262
|
|
|
$content = "Dear user {$user->firstname} {$user->lastname}, your subscription presents a pending payment of {$payload['data']['object']['amount']}"; |
263
|
|
|
|
264
|
|
|
$template = [ |
265
|
|
|
'subject' => $subject, |
266
|
|
|
'content' => $content |
267
|
|
|
]; |
268
|
|
|
//We need to send a mail to the user |
269
|
|
|
if (!defined('API_TESTS')) { |
270
|
|
|
$this->sendWebhookEmail($user->email, $template); |
271
|
|
|
} |
272
|
|
|
} |
273
|
1 |
|
return $this->response(['Webhook Handled']); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|