|
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 (!$this->request->hasHeader('Stripe-Signature')) { |
|
38
|
|
|
throw new NotFoundHttpException('Route not found for this call'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
$request = $this->request->getPost(); |
|
42
|
|
|
|
|
43
|
4 |
|
if (empty($request)) { |
|
44
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
45
|
|
|
} |
|
46
|
4 |
|
$type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.')); |
|
47
|
4 |
|
$method = 'handle' . $type; |
|
48
|
|
|
|
|
49
|
4 |
|
$payloadContent = json_encode($request); |
|
50
|
4 |
|
$this->log->info("Webhook Handler Method: {$method} \n"); |
|
51
|
4 |
|
$this->log->info("Payload: {$payloadContent} \n"); |
|
52
|
|
|
|
|
53
|
4 |
|
if (method_exists($this, $method)) { |
|
54
|
4 |
|
return $this->{$method}($request); |
|
55
|
|
|
} else { |
|
56
|
|
|
return $this->response(['Missing Method to Handled']); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Handle customer subscription updated. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $payload |
|
64
|
|
|
* @return Response |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function handleCustomerSubscriptionUpdated(array $payload): Response |
|
67
|
|
|
{ |
|
68
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
69
|
|
|
if ($user) { |
|
70
|
|
|
//We need to send a mail to the user |
|
71
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
|
72
|
|
|
} |
|
73
|
|
|
return $this->response(['Webhook Handled']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Handle a cancelled customer from a Stripe subscription. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $payload |
|
80
|
|
|
* @return Response |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function handleCustomerSubscriptionDeleted(array $payload): Response |
|
83
|
|
|
{ |
|
84
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
85
|
|
|
if ($user) { |
|
86
|
|
|
$subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']); |
|
87
|
|
|
|
|
88
|
|
|
if (is_object($subscription)) { |
|
89
|
|
|
$subscription->markAsCancelled(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
return $this->response(['Webhook Handled']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Handle customer subscription free trial ending. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $payload |
|
99
|
|
|
* @return Response |
|
100
|
|
|
*/ |
|
101
|
1 |
|
protected function handleCustomerSubscriptionTrialwillend(array $payload): Response |
|
102
|
|
|
{ |
|
103
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
104
|
1 |
|
if ($user) { |
|
105
|
|
|
//We need to send a mail to the user |
|
106
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
|
107
|
|
|
} |
|
108
|
1 |
|
return $this->response(['Webhook Handled']); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Handle customer updated. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $payload |
|
115
|
|
|
* @return Response |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function handleCustomerUpdated(array $payload): Response |
|
118
|
|
|
{ |
|
119
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) { |
|
120
|
|
|
$user->updateCardFromStripe(); |
|
121
|
|
|
} |
|
122
|
|
|
return $this->response(['Webhook Handled']); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Handle customer source deleted. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $payload |
|
129
|
|
|
* @return Response |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function handleCustomerSourceDeleted(array $payload) : Response |
|
132
|
|
|
{ |
|
133
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) { |
|
134
|
|
|
$user->updateCardFromStripe(); |
|
135
|
|
|
} |
|
136
|
|
|
return $this->response(['Webhook Handled']); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Handle deleted customer. |
|
141
|
|
|
* |
|
142
|
|
|
* @param array $payload |
|
143
|
|
|
* @return Response |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function handleCustomerDeleted(array $payload) : Response |
|
146
|
|
|
{ |
|
147
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['id']); |
|
148
|
|
|
if ($user) { |
|
149
|
|
|
foreach ($user->subscriptions as $subscription) { |
|
150
|
|
|
$subscription->skipTrial()->markAsCancelled(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$user->stripe_id = null; |
|
154
|
|
|
$user->trial_ends_at = null; |
|
155
|
|
|
$user->card_brand = null; |
|
156
|
|
|
$user->card_last_four = null; |
|
157
|
|
|
$user->update(); |
|
158
|
|
|
} |
|
159
|
|
|
return $this->response(['Webhook Handled']); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Handle sucessfull payment |
|
164
|
|
|
* |
|
165
|
|
|
* @todo send email |
|
166
|
|
|
* @param array $payload |
|
167
|
|
|
* @return Response |
|
168
|
|
|
*/ |
|
169
|
1 |
|
protected function handleChargeSucceeded(array $payload): Response |
|
170
|
|
|
{ |
|
171
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
172
|
1 |
|
if ($user) { |
|
173
|
|
|
//We need to send a mail to the user |
|
174
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
|
175
|
|
|
} |
|
176
|
1 |
|
return $this->response(['Webhook Handled']); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Handle bad payment |
|
181
|
|
|
* |
|
182
|
|
|
* @todo send email |
|
183
|
|
|
* @param array $payload |
|
184
|
|
|
* @return Response |
|
185
|
|
|
*/ |
|
186
|
1 |
|
protected function handleChargeFailed(array $payload) : Response |
|
187
|
|
|
{ |
|
188
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
189
|
1 |
|
if ($user) { |
|
190
|
|
|
//We need to send a mail to the user |
|
191
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
|
192
|
|
|
} |
|
193
|
1 |
|
return $this->response(['Webhook Handled']); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Handle looking for refund |
|
198
|
|
|
* |
|
199
|
|
|
* @todo send email |
|
200
|
|
|
* @param array $payload |
|
201
|
|
|
* @return Response |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function handleChargeDisputeCreated(array $payload) : Response |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->response(['Webhook Handled']); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Handle pending payments |
|
210
|
|
|
* |
|
211
|
|
|
* @todo send email |
|
212
|
|
|
* @param array $payload |
|
213
|
|
|
* @return Response |
|
214
|
|
|
*/ |
|
215
|
1 |
|
protected function handleChargePending(array $payload) : Response |
|
216
|
|
|
{ |
|
217
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
218
|
1 |
|
if ($user) { |
|
219
|
|
|
//We need to send a mail to the user |
|
220
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
|
221
|
|
|
} |
|
222
|
1 |
|
return $this->response(['Webhook Handled']); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Send webhook related emails to user |
|
227
|
|
|
* @param Users $user |
|
228
|
|
|
* @param array $payload |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
|
|
public static function sendWebhookResponseEmail(Users $user, array $payload): void |
|
232
|
|
|
{ |
|
233
|
|
|
// $subject = ''; |
|
234
|
|
|
// $content = ''; |
|
235
|
|
|
// Di::getDefault()->getMail() |
|
236
|
|
|
// ->to($user->email) |
|
237
|
|
|
// ->subject($subject) |
|
238
|
|
|
// ->content($content) |
|
239
|
|
|
// ->sendNow(); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|