|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service\Braintree; |
|
4
|
|
|
|
|
5
|
|
|
use Braintree\Exception\InvalidSignature; |
|
6
|
|
|
use Braintree\WebhookNotification; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class WebhookService |
|
10
|
|
|
* @package App\Service\Braintree |
|
11
|
|
|
*/ |
|
12
|
|
|
class WebhookService extends AbstractBraintreeService |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $btSignature |
|
16
|
|
|
* @param string $btPayload |
|
17
|
|
|
* @return bool |
|
18
|
|
|
* @throws InvalidSignature |
|
19
|
|
|
*/ |
|
20
|
|
|
public function processWebhook(string $btSignature, string $btPayload): bool |
|
21
|
|
|
{ |
|
22
|
|
|
$webhookNotification = $this->gateway->webhookNotification()->parse($btSignature, $btPayload); |
|
23
|
|
|
return match ($webhookNotification->kind) { |
|
24
|
|
|
WebhookNotification::LOCAL_PAYMENT_COMPLETED => |
|
25
|
|
|
$this->processLocalPaymentMethodWebhook($webhookNotification), |
|
26
|
|
|
WebhookNotification::PAYMENT_METHOD_REVOKED_BY_CUSTOMER => |
|
27
|
|
|
$this->processPaymentMethodRevokedByCustomer($webhookNotification),default => |
|
28
|
|
|
$this->processOthers($webhookNotification) |
|
29
|
|
|
}; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $type |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function generateTestNotification(string $type): array |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->gateway->webhookTesting()->sampleNotification( |
|
|
|
|
|
|
39
|
|
|
constant(WebhookNotification::class .'::'. $type), |
|
40
|
|
|
sha1(time()) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param WebhookNotification $webhookNotification |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function processLocalPaymentMethodWebhook(WebhookNotification $webhookNotification): bool |
|
49
|
|
|
{ |
|
50
|
|
|
//match the original amount and cart thanks to the $webhookNotification->paymentId; |
|
51
|
|
|
$this->gateway->transaction()->sale([ |
|
52
|
|
|
'amount' => $this->settingsService->getSetting('settings-item-price'), |
|
53
|
|
|
'options' => [ |
|
54
|
|
|
'submitForSettlement' => true, |
|
55
|
|
|
], |
|
56
|
|
|
'paymentMethodNonce' => $webhookNotification->subject['localPayment']['paymentMethodNonce'] |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param WebhookNotification $webhookNotification |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function processPaymentMethodRevokedByCustomer(WebhookNotification $webhookNotification): bool |
|
67
|
|
|
{ |
|
68
|
|
|
error_log(print_r($webhookNotification)); |
|
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param WebhookNotification $webhookNotification |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function processOthers(WebhookNotification $webhookNotification): bool |
|
77
|
|
|
{ |
|
78
|
|
|
error_log(print_r($webhookNotification)); |
|
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|