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 StripeWebhookHandlersTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Handle stripe webhoook calls |
27
|
|
|
* |
28
|
|
|
* @return Response |
29
|
|
|
*/ |
30
|
4 |
|
public function handleWebhook(): Response |
31
|
|
|
{ |
32
|
|
|
//we cant processs if we dont find the stripe header |
33
|
4 |
|
if (!defined('API_TESTS')) { |
34
|
|
|
if (!$this->request->hasHeader('Stripe-Signature')) { |
35
|
|
|
throw new NotFoundHttpException('Route not found for this call'); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
$request = $this->request->getPost(); |
40
|
|
|
|
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
|
|
|
|
47
|
4 |
|
$payloadContent = json_encode($request); |
48
|
4 |
|
$this->log->info("Webhook Handler Method: {$method} \n"); |
49
|
4 |
|
$this->log->info("Payload: {$payloadContent} \n"); |
50
|
|
|
|
51
|
4 |
|
if (method_exists($this, $method)) { |
52
|
4 |
|
return $this->{$method}($request); |
53
|
|
|
} else { |
54
|
|
|
return $this->response(['Missing Method to Handled']); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Handle customer subscription updated. |
60
|
|
|
* |
61
|
|
|
* @param array $payload |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
protected function handleCustomerSubscriptionUpdated(array $payload): Response |
65
|
|
|
{ |
66
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
67
|
|
|
if ($user) { |
68
|
|
|
//We need to send a mail to the user |
69
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
70
|
|
|
} |
71
|
|
|
return $this->response(['Webhook Handled']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Handle a cancelled customer from a Stripe subscription. |
76
|
|
|
* |
77
|
|
|
* @param array $payload |
78
|
|
|
* @return Response |
79
|
|
|
*/ |
80
|
|
|
protected function handleCustomerSubscriptionDeleted(array $payload): Response |
81
|
|
|
{ |
82
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
83
|
|
|
if ($user) { |
84
|
|
|
$subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']); |
85
|
|
|
|
86
|
|
|
if (is_object($subscription)) { |
87
|
|
|
$subscription->markAsCancelled(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $this->response(['Webhook Handled']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Handle customer subscription free trial ending. |
95
|
|
|
* |
96
|
|
|
* @param array $payload |
97
|
|
|
* @return Response |
98
|
|
|
*/ |
99
|
1 |
|
protected function handleCustomerSubscriptionTrialwillend(array $payload): Response |
100
|
|
|
{ |
101
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
102
|
1 |
|
if ($user) { |
103
|
|
|
//We need to send a mail to the user |
104
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
105
|
|
|
} |
106
|
1 |
|
return $this->response(['Webhook Handled']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Handle customer updated. |
111
|
|
|
* |
112
|
|
|
* @param array $payload |
113
|
|
|
* @return Response |
114
|
|
|
*/ |
115
|
|
|
protected function handleCustomerUpdated(array $payload): Response |
116
|
|
|
{ |
117
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) { |
118
|
|
|
$user->updateCardFromStripe(); |
119
|
|
|
} |
120
|
|
|
return $this->response(['Webhook Handled']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Handle customer source deleted. |
125
|
|
|
* |
126
|
|
|
* @param array $payload |
127
|
|
|
* @return Response |
128
|
|
|
*/ |
129
|
|
|
protected function handleCustomerSourceDeleted(array $payload) : Response |
130
|
|
|
{ |
131
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) { |
132
|
|
|
$user->updateCardFromStripe(); |
133
|
|
|
} |
134
|
|
|
return $this->response(['Webhook Handled']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Handle deleted customer. |
139
|
|
|
* |
140
|
|
|
* @param array $payload |
141
|
|
|
* @return Response |
142
|
|
|
*/ |
143
|
|
|
protected function handleCustomerDeleted(array $payload) : Response |
144
|
|
|
{ |
145
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['id']); |
146
|
|
|
if ($user) { |
147
|
|
|
foreach ($user->subscriptions as $subscription) { |
148
|
|
|
$subscription->skipTrial()->markAsCancelled(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$user->stripe_id = null; |
152
|
|
|
$user->trial_ends_at = null; |
153
|
|
|
$user->card_brand = null; |
154
|
|
|
$user->card_last_four = null; |
155
|
|
|
$user->update(); |
156
|
|
|
} |
157
|
|
|
return $this->response(['Webhook Handled']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Handle sucessfull payment |
162
|
|
|
* |
163
|
|
|
* @todo send email |
164
|
|
|
* @param array $payload |
165
|
|
|
* @return Response |
166
|
|
|
*/ |
167
|
1 |
|
protected function handleChargeSucceeded(array $payload): Response |
168
|
|
|
{ |
169
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
170
|
1 |
|
if ($user) { |
171
|
|
|
//We need to send a mail to the user |
172
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
173
|
|
|
} |
174
|
1 |
|
return $this->response(['Webhook Handled']); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Handle bad payment |
179
|
|
|
* |
180
|
|
|
* @todo send email |
181
|
|
|
* @param array $payload |
182
|
|
|
* @return Response |
183
|
|
|
*/ |
184
|
1 |
|
protected function handleChargeFailed(array $payload) : Response |
185
|
|
|
{ |
186
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
187
|
1 |
|
if ($user) { |
188
|
|
|
//We need to send a mail to the user |
189
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
190
|
|
|
} |
191
|
1 |
|
return $this->response(['Webhook Handled']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Handle looking for refund |
196
|
|
|
* |
197
|
|
|
* @todo send email |
198
|
|
|
* @param array $payload |
199
|
|
|
* @return Response |
200
|
|
|
*/ |
201
|
|
|
protected function handleChargeDisputeCreated(array $payload) : Response |
202
|
|
|
{ |
203
|
|
|
return $this->response(['Webhook Handled']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Handle pending payments |
208
|
|
|
* |
209
|
|
|
* @todo send email |
210
|
|
|
* @param array $payload |
211
|
|
|
* @return Response |
212
|
|
|
*/ |
213
|
1 |
|
protected function handleChargePending(array $payload) : Response |
214
|
|
|
{ |
215
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
216
|
1 |
|
if ($user) { |
217
|
|
|
//We need to send a mail to the user |
218
|
|
|
$this->sendWebhookResponseEmail($user, $payload); |
219
|
|
|
} |
220
|
1 |
|
return $this->response(['Webhook Handled']); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Send webhook related emails to user |
225
|
|
|
* @param Users $user |
226
|
|
|
* @param array $payload |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
public static function sendWebhookResponseEmail(Users $user, array $payload): void |
230
|
|
|
{ |
231
|
|
|
$subject = ''; |
232
|
|
|
$content = ''; |
233
|
|
|
Di::getDefault()->getMail() |
|
|
|
|
234
|
|
|
->to($user->email) |
235
|
|
|
->subject($subject) |
236
|
|
|
->content($content) |
237
|
|
|
->sendNow(); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths