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
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
10
|
|
|
use Phalcon\Di; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait WebhookHandlers |
14
|
|
|
* |
15
|
|
|
* @package Gewaer\Traits |
16
|
|
|
* |
17
|
|
|
* @property Users $user |
18
|
|
|
* @property AppsPlans $appPlan |
19
|
|
|
* @property CompanyBranches $branches |
20
|
|
|
* @property Companies $company |
21
|
|
|
* @property UserCompanyApps $app |
22
|
|
|
* @property \Phalcon\Di $di |
23
|
|
|
* @property Subscriptions $subscriptions |
24
|
|
|
* @property Email $email |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
trait 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 (!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
|
|
|
//We need to send a mail to the user |
73
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
74
|
|
|
} |
75
|
|
|
return $this->response(['Webhook Handled']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Handle a cancelled customer from a Stripe subscription. |
80
|
|
|
* |
81
|
|
|
* @param array $payload |
82
|
|
|
* @return Response |
83
|
|
|
*/ |
84
|
|
|
protected function handleCustomerSubscriptionDeleted(array $payload): Response |
85
|
|
|
{ |
86
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
87
|
|
|
if ($user) { |
88
|
|
|
$subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']); |
89
|
|
|
|
90
|
|
|
if (is_object($subscription)) { |
91
|
|
|
$subscription->markAsCancelled(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return $this->response(['Webhook Handled']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Handle customer subscription free trial ending. |
99
|
|
|
* |
100
|
|
|
* @param array $payload |
101
|
|
|
* @return Response |
102
|
|
|
*/ |
103
|
1 |
|
protected function handleCustomerSubscriptionTrialwillend(array $payload): Response |
104
|
|
|
{ |
105
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
106
|
1 |
|
if ($user) { |
107
|
|
|
//We need to send a mail to the user |
108
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
109
|
|
|
} |
110
|
1 |
|
return $this->response(['Webhook Handled']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Handle customer updated. |
115
|
|
|
* |
116
|
|
|
* @param array $payload |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
|
|
protected function handleCustomerUpdated(array $payload): Response |
120
|
|
|
{ |
121
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) { |
122
|
|
|
$user->updateCardFromStripe(); |
123
|
|
|
} |
124
|
|
|
return $this->response(['Webhook Handled']); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Handle customer source deleted. |
129
|
|
|
* |
130
|
|
|
* @param array $payload |
131
|
|
|
* @return Response |
132
|
|
|
*/ |
133
|
|
|
protected function handleCustomerSourceDeleted(array $payload) : Response |
134
|
|
|
{ |
135
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) { |
136
|
|
|
$user->updateCardFromStripe(); |
137
|
|
|
} |
138
|
|
|
return $this->response(['Webhook Handled']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Handle deleted customer. |
143
|
|
|
* |
144
|
|
|
* @param array $payload |
145
|
|
|
* @return Response |
146
|
|
|
*/ |
147
|
|
|
protected function handleCustomerDeleted(array $payload) : Response |
148
|
|
|
{ |
149
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['id']); |
150
|
|
|
if ($user) { |
151
|
|
|
foreach ($user->subscriptions as $subscription) { |
152
|
|
|
$subscription->skipTrial()->markAsCancelled(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$user->stripe_id = null; |
156
|
|
|
$user->trial_ends_at = null; |
157
|
|
|
$user->card_brand = null; |
158
|
|
|
$user->card_last_four = null; |
159
|
|
|
$user->update(); |
160
|
|
|
} |
161
|
|
|
return $this->response(['Webhook Handled']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Handle sucessfull payment |
166
|
|
|
* |
167
|
|
|
* @todo send email |
168
|
|
|
* @param array $payload |
169
|
|
|
* @return Response |
170
|
|
|
*/ |
171
|
1 |
|
protected function handleChargeSucceeded(array $payload): Response |
172
|
|
|
{ |
173
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
174
|
1 |
|
if ($user) { |
175
|
|
|
//We need to send a mail to the user |
176
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
177
|
|
|
} |
178
|
1 |
|
return $this->response(['Webhook Handled']); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Handle bad payment |
183
|
|
|
* |
184
|
|
|
* @todo send email |
185
|
|
|
* @param array $payload |
186
|
|
|
* @return Response |
187
|
|
|
*/ |
188
|
1 |
|
protected function handleChargeFailed(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
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
194
|
|
|
} |
195
|
1 |
|
return $this->response(['Webhook Handled']); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Handle looking for refund |
200
|
|
|
* |
201
|
|
|
* @todo send email |
202
|
|
|
* @param array $payload |
203
|
|
|
* @return Response |
204
|
|
|
*/ |
205
|
|
|
protected function handleChargeDisputeCreated(array $payload) : Response |
206
|
|
|
{ |
207
|
|
|
return $this->response(['Webhook Handled']); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Handle pending payments |
212
|
|
|
* |
213
|
|
|
* @todo send email |
214
|
|
|
* @param array $payload |
215
|
|
|
* @return Response |
216
|
|
|
*/ |
217
|
1 |
|
protected function handleChargePending(array $payload) : Response |
218
|
|
|
{ |
219
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
220
|
1 |
|
if ($user) { |
221
|
|
|
//We need to send a mail to the user |
222
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
223
|
|
|
} |
224
|
1 |
|
return $this->response(['Webhook Handled']); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Send webhook related emails to user |
229
|
|
|
* @param Users $user |
230
|
|
|
* @param array $payload |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
|
public static function sendWebhookResponseEmail(Users $user, array $payload): void |
234
|
|
|
{ |
235
|
|
|
$subject = ''; |
236
|
|
|
$content = ''; |
237
|
|
|
Di::getDefault()->getMail() |
238
|
|
|
->to($user->email) |
239
|
|
|
->subject($subject) |
240
|
|
|
->content($content) |
241
|
|
|
->sendNow(); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|