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