1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Traits; |
||
6 | |||
7 | use Gewaer\Models\Users; |
||
8 | use Phalcon\Http\Response; |
||
9 | |||
10 | /** |
||
11 | * Trait WebhookHandlers |
||
12 | * |
||
13 | * @package Gewaer\Traits |
||
14 | * |
||
15 | * @property Users $user |
||
16 | * @property AppsPlans $appPlan |
||
17 | * @property CompanyBranches $branches |
||
18 | * @property Companies $company |
||
19 | * @property UserCompanyApps $app |
||
20 | * @property \Phalcon\Di $di |
||
21 | * |
||
22 | */ |
||
23 | trait WebhookHandlersTrait |
||
24 | { |
||
25 | /** |
||
26 | * Handle customer subscription updated. |
||
27 | * |
||
28 | * @param array $payload |
||
29 | * @return Response |
||
30 | */ |
||
31 | protected function handleCustomerSubscriptionUpdated(array $payload): Response |
||
32 | { |
||
33 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
34 | if ($user) { |
||
35 | //We need to send a mail to the user |
||
36 | if (!defined('API_TESTS')) { |
||
37 | $this->sendWebhookEmail($user, $payload); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
38 | } |
||
39 | } |
||
40 | return $this->response(['Webhook Handled']); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Handle a cancelled customer from a Stripe subscription. |
||
45 | * |
||
46 | * @param array $payload |
||
47 | * @return Response |
||
48 | */ |
||
49 | protected function handleCustomerSubscriptionDeleted(array $payload): Response |
||
50 | { |
||
51 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
||
52 | if ($user) { |
||
53 | $subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']); |
||
54 | |||
55 | if (is_object($subscription)) { |
||
56 | $subscription->markAsCancelled(); |
||
57 | } |
||
58 | } |
||
59 | return $this->response(['Webhook Handled']); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Handle customer subscription free trial ending. |
||
64 | * |
||
65 | * @param array $payload |
||
66 | * @return Response |
||
67 | */ |
||
68 | 1 | protected function handleCustomerSubscriptionTrialwillend(array $payload): Response |
|
69 | { |
||
70 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
71 | 1 | if ($user) { |
|
72 | //We need to send a mail to the user |
||
73 | if (!defined('API_TESTS')) { |
||
74 | $this->sendWebhookEmail($user, $payload); |
||
75 | } |
||
76 | } |
||
77 | 1 | return $this->response(['Webhook Handled']); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Handle customer updated. |
||
82 | * |
||
83 | * @param array $payload |
||
84 | * @return Response |
||
85 | */ |
||
86 | protected function handleCustomerUpdated(array $payload): Response |
||
87 | { |
||
88 | if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) { |
||
89 | $user->updateCardFromStripe(); |
||
90 | } |
||
91 | return $this->response(['Webhook Handled']); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Handle customer source deleted. |
||
96 | * |
||
97 | * @param array $payload |
||
98 | * @return Response |
||
99 | */ |
||
100 | protected function handleCustomerSourceDeleted(array $payload) : Response |
||
101 | { |
||
102 | if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) { |
||
103 | $user->updateCardFromStripe(); |
||
104 | } |
||
105 | return $this->response(['Webhook Handled']); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Handle deleted customer. |
||
110 | * |
||
111 | * @param array $payload |
||
112 | * @return Response |
||
113 | */ |
||
114 | protected function handleCustomerDeleted(array $payload) : Response |
||
115 | { |
||
116 | $user = Users::findFirstByStripeId($payload['data']['object']['id']); |
||
117 | if ($user) { |
||
118 | foreach ($user->subscriptions as $subscription) { |
||
119 | $subscription->skipTrial()->markAsCancelled(); |
||
120 | } |
||
121 | |||
122 | $user->stripe_id = null; |
||
123 | $user->trial_ends_at = null; |
||
124 | $user->card_brand = null; |
||
125 | $user->card_last_four = null; |
||
126 | $user->update(); |
||
127 | } |
||
128 | return $this->response(['Webhook Handled']); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Handle sucessfull payment |
||
133 | * |
||
134 | * @todo send email |
||
135 | * @param array $payload |
||
136 | * @return Response |
||
137 | */ |
||
138 | 1 | protected function handleChargeSucceeded(array $payload): Response |
|
139 | { |
||
140 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
141 | 1 | if ($user) { |
|
142 | //We need to send a mail to the user |
||
143 | if (!defined('API_TESTS')) { |
||
144 | $this->sendWebhookEmail($user, $payload); |
||
145 | } |
||
146 | } |
||
147 | 1 | return $this->response(['Webhook Handled']); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Handle bad payment |
||
152 | * |
||
153 | * @todo send email |
||
154 | * @param array $payload |
||
155 | * @return Response |
||
156 | */ |
||
157 | 1 | protected function handleChargeFailed(array $payload) : Response |
|
158 | { |
||
159 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
160 | 1 | if ($user) { |
|
161 | //We need to send a mail to the user |
||
162 | if (!defined('API_TESTS')) { |
||
163 | $this->sendWebhookEmail($user, $payload); |
||
164 | } |
||
165 | } |
||
166 | 1 | return $this->response(['Webhook Handled']); |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Handle looking for refund |
||
171 | * |
||
172 | * @todo send email |
||
173 | * @param array $payload |
||
174 | * @return Response |
||
175 | */ |
||
176 | protected function handleChargeDisputeCreated(array $payload) : Response |
||
177 | { |
||
178 | return $this->response(['Webhook Handled']); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Handle pending payments |
||
183 | * |
||
184 | * @todo send email |
||
185 | * @param array $payload |
||
186 | * @return Response |
||
187 | */ |
||
188 | 1 | protected function handleChargePending(array $payload) : Response |
|
189 | { |
||
190 | 1 | $user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
191 | 1 | if ($user) { |
|
192 | //We need to send a mail to the user |
||
193 | if (!defined('API_TESTS')) { |
||
194 | $this->sendWebhookEmail($user, $payload); |
||
195 | } |
||
196 | } |
||
197 | 1 | return $this->response(['Webhook Handled']); |
|
198 | } |
||
199 | } |
||
200 |