1
|
|
|
<?php namespace BB\Http\Controllers; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use BB\Entities\Payment; |
5
|
|
|
use BB\Entities\User; |
6
|
|
|
use BB\Helpers\GoCardlessHelper; |
7
|
|
|
use BB\Repo\PaymentRepository; |
8
|
|
|
use BB\Repo\SubscriptionChargeRepository; |
9
|
|
|
use \Carbon\Carbon; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
class GoCardlessWebhookController extends Controller |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var PaymentRepository |
17
|
|
|
*/ |
18
|
|
|
private $paymentRepository; |
19
|
|
|
/** |
20
|
|
|
* @var SubscriptionChargeRepository |
21
|
|
|
*/ |
22
|
|
|
private $subscriptionChargeRepository; |
23
|
|
|
/** |
24
|
|
|
* @var GoCardlessHelper |
25
|
|
|
*/ |
26
|
|
|
private $goCardless; |
27
|
|
|
/** |
28
|
|
|
* @var \BB\Repo\UserRepository |
29
|
|
|
*/ |
30
|
|
|
private $userRepository; |
31
|
|
|
|
32
|
|
View Code Duplication |
public function __construct(GoCardlessHelper $goCardless, PaymentRepository $paymentRepository, SubscriptionChargeRepository $subscriptionChargeRepository, \BB\Repo\UserRepository $userRepository) |
33
|
|
|
{ |
34
|
|
|
$this->goCardless = $goCardless; |
35
|
|
|
$this->paymentRepository = $paymentRepository; |
36
|
|
|
$this->subscriptionChargeRepository = $subscriptionChargeRepository; |
37
|
|
|
$this->userRepository = $userRepository; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function receive() |
41
|
|
|
{ |
42
|
|
|
$request = \Request::instance(); |
43
|
|
|
$webhookData = $request->getContent(); |
44
|
|
|
$signature = $request->header('Webhook-Signature'); |
45
|
|
|
|
46
|
|
|
$hash = hash_hmac('sha256', $webhookData, env('NEW_GOCARDLESS_WEBHOOK_SECRET')); |
47
|
|
|
|
48
|
|
|
if ($signature != $hash) { |
49
|
|
|
return \Response::make('', 403); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$webhookData = json_decode($webhookData, true); |
53
|
|
|
|
54
|
|
|
foreach ($webhookData['events'] as $event) { |
55
|
|
|
$parser = new \BB\Services\Payment\GoCardlessWebhookParser(); |
56
|
|
|
$parser->parseResponse($event); |
57
|
|
|
|
58
|
|
|
switch ($parser->getResourceType()) { |
59
|
|
|
case 'payments': |
|
|
|
|
60
|
|
|
|
61
|
|
|
switch ($parser->getAction()) { |
62
|
|
|
case 'created': |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->processNewPayment($event); |
65
|
|
|
|
66
|
|
|
break; |
67
|
|
|
case 'submitted': |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->processSubmittedPayment($event); |
70
|
|
|
|
71
|
|
|
break; |
72
|
|
|
case 'confirmed': |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->processPaidBills($event); |
75
|
|
|
|
76
|
|
|
break; |
77
|
|
|
case 'paid_out': |
|
|
|
|
78
|
|
|
|
79
|
|
|
break; |
80
|
|
|
case 'failed': |
81
|
|
|
case 'cancelled': |
|
|
|
|
82
|
|
|
|
83
|
|
|
$this->paymentFailed($event); |
84
|
|
|
|
85
|
|
|
break; |
86
|
|
|
default: |
|
|
|
|
87
|
|
|
|
88
|
|
|
\Log::info('GoCardless payment event. Action: ' . $parser->getAction() . '. Data: ' . json_encode($event)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
break; |
92
|
|
|
case 'mandates': |
|
|
|
|
93
|
|
|
|
94
|
|
|
switch ($parser->getAction()) { |
95
|
|
|
case 'cancelled': |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->cancelPreAuth($event); |
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
default: |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
break; |
104
|
|
|
case 'subscriptions': |
|
|
|
|
105
|
|
|
|
106
|
|
|
switch ($parser->getAction()) { |
107
|
|
|
case 'cancelled': |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->cancelSubscriptions($event); |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
case 'payment_created': |
|
|
|
|
113
|
|
|
|
114
|
|
|
$this->processNewSubscriptionPayment($event); |
115
|
|
|
|
116
|
|
|
break; |
117
|
|
|
default: |
|
|
|
|
118
|
|
|
|
119
|
|
|
\Log::info('GoCardless subscription event. Action: ' . $parser->getAction() . '. Data: ' . json_encode($event)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return \Response::make('Success', 200); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* A Bill has been created, these will always start within the system except for subscription payments |
132
|
|
|
* |
133
|
|
|
* @param array $bill |
134
|
|
|
*/ |
135
|
|
|
private function processNewPayment(array $bill) |
136
|
|
|
{ |
137
|
|
|
\Log::info('New payment notification. ' . json_encode($bill)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* A Bill has been created, these will always start within the system except for subscription payments |
143
|
|
|
* |
144
|
|
|
* @param array $bill |
145
|
|
|
*/ |
146
|
|
|
private function processNewSubscriptionPayment(array $bill) |
147
|
|
|
{ |
148
|
|
|
// Lookup the payment from the API |
149
|
|
|
$payment = $this->goCardless->getPayment($bill['links']['payment']); |
150
|
|
|
|
151
|
|
|
try { |
152
|
|
|
|
153
|
|
|
//Locate the user through their subscription id |
154
|
|
|
$user = User::where('subscription_id', $bill['links']['subscription'])->first(); |
155
|
|
|
|
156
|
|
|
if ( ! $user) { |
157
|
|
|
\Log::warning("GoCardless new sub payment notification for unmatched user. Bill ID: " . $bill['links']['payment']); |
|
|
|
|
158
|
|
|
|
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$amount = ($payment->amount * 1) / 100; |
163
|
|
|
$this->paymentRepository->recordSubscriptionPayment($user->id, 'gocardless', $bill['links']['payment'], $amount, $payment->status); |
164
|
|
|
|
165
|
|
|
} catch (\Exception $e) { |
166
|
|
|
\Log::error($e); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
View Code Duplication |
private function processSubmittedPayment(array $bill) |
172
|
|
|
{ |
173
|
|
|
//When a bill is submitted to the bank update the status on the local record |
174
|
|
|
|
175
|
|
|
$existingPayment = $this->paymentRepository->getPaymentBySourceId($bill['links']['payment']); |
176
|
|
|
if ($existingPayment) { |
177
|
|
|
$this->paymentRepository->markPaymentPending($existingPayment->id); |
|
|
|
|
178
|
|
|
} else { |
179
|
|
|
\Log::info("GoCardless Webhook received for unknown payment: " . $bill['links']['payment']); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
private function processPaidBills(array $bill) |
184
|
|
|
{ |
185
|
|
|
//When a bill is paid update the status on the local record and the connected sub charge (if there is one) |
186
|
|
|
|
187
|
|
|
$existingPayment = $this->paymentRepository->getPaymentBySourceId($bill['links']['payment']); |
188
|
|
|
if ($existingPayment) { |
189
|
|
|
$this->paymentRepository->markPaymentPaid($existingPayment->id, Carbon::now()); |
|
|
|
|
190
|
|
|
} else { |
191
|
|
|
\Log::info("GoCardless Webhook received for unknown payment: " . $bill['links']['payment']); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array $bill |
197
|
|
|
*/ |
198
|
|
|
private function paymentFailed(array $bill) |
199
|
|
|
{ |
200
|
|
|
$existingPayment = $this->paymentRepository->getPaymentBySourceId($bill['links']['payment']); |
201
|
|
|
$payment = $this->goCardless->getPayment($bill['links']['payment']); |
202
|
|
|
if ($existingPayment) { |
203
|
|
|
$this->paymentRepository->recordPaymentFailure($existingPayment->id, $payment->status); |
|
|
|
|
204
|
|
|
} else { |
205
|
|
|
\Log::info("GoCardless Webhook received for unknown payment: " . $bill['links']['payment']); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $action |
212
|
|
|
*/ |
213
|
|
|
private function processBills($action, array $bills) |
214
|
|
|
{ |
215
|
|
|
foreach ($bills as $bill) { |
216
|
|
|
$existingPayment = $this->paymentRepository->getPaymentBySourceId($bill['id']); |
217
|
|
|
if ($existingPayment) { |
218
|
|
|
if (($bill['status'] == 'pending') && ($action == 'resubmission_requested')) { |
219
|
|
|
//Failed payment is being retried |
220
|
|
|
$subCharge = $this->subscriptionChargeRepository->getById($existingPayment->reference); |
|
|
|
|
221
|
|
|
if ($subCharge) { |
222
|
|
|
if ($subCharge->amount == $bill['amount']) { |
223
|
|
|
$this->subscriptionChargeRepository->markChargeAsProcessing($subCharge->id); |
224
|
|
|
} else { |
225
|
|
|
//@TODO: Handle partial payments |
226
|
|
|
\Log::warning("Sub charge handling - gocardless partial payment"); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} elseif ($bill['status'] == 'refunded') { |
230
|
|
|
//Payment refunded |
231
|
|
|
//Update the payment record and possible the user record |
232
|
|
|
} |
233
|
|
|
} else { |
234
|
|
|
\Log::info("GoCardless Webhook received for unknown payment: " . $bill['id']); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param array $preAuth |
242
|
|
|
*/ |
243
|
|
|
private function cancelPreAuth($preAuth) |
244
|
|
|
{ |
245
|
|
|
/** @var User $user */ |
246
|
|
|
$user = User::where('mandate_id', $preAuth['links']['mandate'])->first(); |
247
|
|
|
if ($user) { |
248
|
|
|
$user->cancelSubscription(); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
private function cancelSubscriptions($subscription) |
254
|
|
|
{ |
255
|
|
|
//Make sure our local record is correct |
256
|
|
|
/** @var User $user */ |
257
|
|
|
$user = User::where('subscription_id', $subscription['links']['subscription'])->first(); |
258
|
|
|
if ($user) { |
259
|
|
|
if ($user->payment_method == 'gocardless') { |
260
|
|
|
$user->cancelSubscription(); |
|
|
|
|
261
|
|
|
} else { |
262
|
|
|
// The user probably has a new subscription alongside an existing mandate, |
263
|
|
|
// we don't want to touch that so just remove the subscription details |
264
|
|
|
$this->userRepository->recordGoCardlessSubscription($user->id, null); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.