|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Phalcon\Http\Response; |
|
8
|
|
|
use Gewaer\Models\Users; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PaymentsController |
|
14
|
|
|
* |
|
15
|
|
|
* Class to handle payment webhook from our cashier library |
|
16
|
|
|
* |
|
17
|
|
|
* @package Gewaer\Api\Controllers |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class PaymentsController extends BaseController |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Handle stripe webhoook calls |
|
24
|
|
|
* |
|
25
|
|
|
* @return Response |
|
26
|
|
|
*/ |
|
27
|
|
|
public function handleWebhook(): Response |
|
28
|
|
|
{ |
|
29
|
|
|
//we cant processs if we dont find the stripe header |
|
30
|
|
|
if (!$this->request->hasHeader('Stripe-Signature')) { |
|
31
|
|
|
throw new NotFoundHttpException('Route not found for this call'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$request = $this->request->getPost(); |
|
35
|
|
|
|
|
36
|
|
|
if (empty($request)) { |
|
37
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
38
|
|
|
} |
|
39
|
|
|
$value = ucwords(str_replace(['-', '_'], '', str_replace('.', '_', $request['type']))); |
|
40
|
|
|
$method = 'handle' . $value; |
|
41
|
|
|
if (method_exists($this, $method)) { |
|
42
|
|
|
return $this->{$method}($request); |
|
43
|
|
|
} else { |
|
44
|
|
|
return $this->response(['Missing Method to Handled']); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Handle customer subscription updated. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $payload |
|
52
|
|
|
* @return Response |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function handleCustomerSubscriptionUpdated(array $payload): Response |
|
55
|
|
|
{ |
|
56
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
|
|
|
|
|
57
|
|
|
if ($user) { |
|
58
|
|
|
$data = $payload['data']['object']; |
|
59
|
|
|
//get the current subscription they are updating |
|
60
|
|
|
$subscription = $user->getAllSubscriptions('stripe_id =' . $data['id']); |
|
61
|
|
|
|
|
62
|
|
|
if (is_object($subscription)) { |
|
63
|
|
|
// Quantity... |
|
64
|
|
|
if (isset($data['quantity'])) { |
|
65
|
|
|
$subscription->quantity = $data['quantity']; |
|
66
|
|
|
} |
|
67
|
|
|
// Plan... |
|
68
|
|
|
if (isset($data['plan']['id'])) { |
|
69
|
|
|
$subscription->stripe_plan = $data['plan']['id']; |
|
70
|
|
|
} |
|
71
|
|
|
// Trial ending date... |
|
72
|
|
|
if (isset($data['trial_end'])) { |
|
73
|
|
|
$trial_ends = Carbon::createFromTimestamp($data['trial_end']); |
|
74
|
|
|
if (!$subscription->trial_ends_at || $subscription->trial_ends_at->ne($trial_ends)) { |
|
75
|
|
|
$subscription->trial_ends_at = $trial_ends; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
// Cancellation date... |
|
79
|
|
|
if (isset($data['cancel_at_period_end']) && $data['cancel_at_period_end']) { |
|
80
|
|
|
$subscription->ends_at = $subscription->onTrial() |
|
81
|
|
|
? $subscription->trial_ends_at |
|
82
|
|
|
: Carbon::createFromTimestamp($data['current_period_end']); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$subscription->update(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $this->response(['Webhook Handled']); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Handle a cancelled customer from a Stripe subscription. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $payload |
|
95
|
|
|
* @return Response |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function handleCustomerSubscriptionDeleted(array $payload): Response |
|
98
|
|
|
{ |
|
99
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
100
|
|
|
if ($user) { |
|
101
|
|
|
$subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']); |
|
102
|
|
|
|
|
103
|
|
|
if (is_object($subscription)) { |
|
104
|
|
|
$subscription->markAsCancelled(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
return $this->response(['Webhook Handled']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Handle customer updated. |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $payload |
|
114
|
|
|
* @return Response |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function handleCustomerUpdated(array $payload): Response |
|
117
|
|
|
{ |
|
118
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) { |
|
119
|
|
|
$user->updateCardFromStripe(); |
|
120
|
|
|
} |
|
121
|
|
|
return $this->response(['Webhook Handled']); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Handle customer source deleted. |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $payload |
|
128
|
|
|
* @return Response |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function handleCustomerSourceDeleted(array $payload) : Response |
|
131
|
|
|
{ |
|
132
|
|
|
if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) { |
|
133
|
|
|
$user->updateCardFromStripe(); |
|
134
|
|
|
} |
|
135
|
|
|
return $this->response(['Webhook Handled']); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Handle deleted customer. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $payload |
|
142
|
|
|
* @return Response |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function handleCustomerDeleted(array $payload) : Response |
|
145
|
|
|
{ |
|
146
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['id']); |
|
147
|
|
|
if ($user) { |
|
148
|
|
|
foreach ($user->subscriptions as $subscription) { |
|
149
|
|
|
$subscription->skipTrial()->markAsCancelled(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$user->stripe_id = null; |
|
153
|
|
|
$user->trial_ends_at = null; |
|
154
|
|
|
$user->card_brand = null; |
|
155
|
|
|
$user->card_last_four = null; |
|
156
|
|
|
$user->update(); |
|
157
|
|
|
} |
|
158
|
|
|
return $this->response(['Webhook Handled']); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Handle sucessfull payment |
|
163
|
|
|
* |
|
164
|
|
|
* @todo send email |
|
165
|
|
|
* @param array $payload |
|
166
|
|
|
* @return Response |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function handleChargeSucceeded(array $payload): Response |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->response(['Webhook Handled']); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Handle bad payment |
|
175
|
|
|
* |
|
176
|
|
|
* @todo send email |
|
177
|
|
|
* @param array $payload |
|
178
|
|
|
* @return Response |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function handleChargeFailed(array $payload) : Response |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->response(['Webhook Handled']); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Handle looking for refund |
|
187
|
|
|
* |
|
188
|
|
|
* @todo send email |
|
189
|
|
|
* @param array $payload |
|
190
|
|
|
* @return Response |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function handleChargeDisputeCreated(array $payload) : Response |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->response(['Webhook Handled']); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|