1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Api\Controllers; |
||
6 | |||
7 | use Phalcon\Cashier\Traits\StripeWebhookHandlersTrait; |
||
8 | use Phalcon\Http\Response; |
||
9 | use Gewaer\Models\Users; |
||
10 | use Gewaer\Models\EmailTemplates; |
||
11 | use Phalcon\Di; |
||
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 | * Stripe Webhook Handlers |
||
26 | */ |
||
27 | use StripeWebhookHandlersTrait; |
||
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 (!$this->request->hasHeader('Stripe-Signature')) { |
|
38 | throw new Exception('Route not found for this call'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
39 | } |
||
40 | 4 | $request = $this->request->getPost(); |
|
41 | 4 | if (empty($request)) { |
|
42 | $request = $this->request->getJsonRawBody(true); |
||
43 | } |
||
44 | 4 | $type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.')); |
|
45 | 4 | $method = 'handle' . $type; |
|
46 | 4 | $payloadContent = json_encode($request); |
|
47 | 4 | $this->log->info("Webhook Handler Method: {$method} \n"); |
|
48 | 4 | $this->log->info("Payload: {$payloadContent} \n"); |
|
49 | 4 | if (method_exists($this, $method)) { |
|
50 | 4 | return $this->{$method}($request, $method); |
|
51 | } else { |
||
52 | return $this->response(['Missing Method to Handled']); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Handle customer subscription updated. |
||
58 | * |
||
59 | * @param array $payload |
||
60 | * @return Response |
||
61 | */ |
||
62 | protected function handleCustomerSubscriptionUpdated(array $payload, string $method): Response |
||
63 | { |
||
64 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
65 | if ($user) { |
||
66 | //We need to send a mail to the user |
||
67 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
68 | } |
||
69 | return $this->response(['Webhook Handled']); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Handle customer subscription free trial ending. |
||
74 | * |
||
75 | * @param array $payload |
||
76 | * @return Response |
||
77 | */ |
||
78 | 1 | protected function handleCustomerSubscriptionTrialwillend(array $payload, string $method): Response |
|
79 | { |
||
80 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
81 | 1 | if ($user) { |
|
82 | //We need to send a mail to the user |
||
83 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
84 | } |
||
85 | 1 | return $this->response(['Webhook Handled']); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Handle sucessfull payment |
||
90 | * |
||
91 | * @param array $payload |
||
92 | * @return Response |
||
93 | */ |
||
94 | 1 | protected function handleChargeSucceeded(array $payload, string $method): Response |
|
95 | { |
||
96 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
97 | 1 | if ($user) { |
|
98 | //We need to send a mail to the user |
||
99 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
100 | } |
||
101 | 1 | return $this->response(['Webhook Handled']); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Handle bad payment |
||
106 | * |
||
107 | * @param array $payload |
||
108 | * @return Response |
||
109 | */ |
||
110 | 1 | protected function handleChargeFailed(array $payload, string $method) : Response |
|
111 | { |
||
112 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
113 | 1 | if ($user) { |
|
114 | //We need to send a mail to the user |
||
115 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
116 | } |
||
117 | 1 | return $this->response(['Webhook Handled']); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Handle pending payments |
||
122 | * |
||
123 | * @param array $payload |
||
124 | * @return Response |
||
125 | */ |
||
126 | 1 | protected function handleChargePending(array $payload, string $method) : Response |
|
127 | { |
||
128 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
129 | 1 | if ($user) { |
|
130 | //We need to send a mail to the user |
||
131 | $this->sendWebhookResponseEmail($user, $payload, $method); |
||
132 | } |
||
133 | 1 | return $this->response(['Webhook Handled']); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Send webhook related emails to user |
||
138 | * @param Users $user |
||
139 | * @param array $payload |
||
140 | * @param string $method |
||
141 | * @return void |
||
142 | */ |
||
143 | protected function sendWebhookResponseEmail(Users $user, array $payload, string $method): void |
||
144 | { |
||
145 | switch ($method) { |
||
146 | case 'handleCustomerSubscriptionTrialwillend': |
||
147 | $templateName = 'users-trial-end'; |
||
148 | break; |
||
149 | case 'handleCustomerSubscriptionUpdated': |
||
150 | $templateName = 'users-subscription-updated'; |
||
151 | break; |
||
152 | |||
153 | case 'handleChargeSucceeded': |
||
154 | $templateName = 'users-charge-success'; |
||
155 | break; |
||
156 | |||
157 | case 'handleChargeFailed': |
||
158 | $templateName = 'users-charge-failed'; |
||
159 | break; |
||
160 | |||
161 | case 'handleChargePending': |
||
162 | $templateName = 'users-charge-pending'; |
||
163 | break; |
||
164 | |||
165 | default: |
||
166 | break; |
||
167 | } |
||
168 | |||
169 | //Search for actual template by templateName |
||
170 | $emailTemplate = EmailTemplates::getByName($templateName); |
||
171 | |||
172 | Di::getDefault()->getMail() |
||
173 | ->to($user->email) |
||
174 | ->subject('Canvas Payments and Subscriptions') |
||
175 | ->content($emailTemplate->template) |
||
176 | ->sendNow(); |
||
177 | } |
||
178 | } |
||
179 |