-
Notifications
You must be signed in to change notification settings - Fork 0
/
Donation.php
115 lines (100 loc) · 4.87 KB
/
Donation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace MatchBot\Client;
use GuzzleHttp\Exception\RequestException;
use MatchBot\Application\Messenger\DonationUpserted;
use MatchBot\Domain\DonationRepository;
use MatchBot\Domain\Salesforce18Id;
/**
* Client to push / upsert copies of donations to Salesforce.
*/
class Donation extends Common
{
/**
* @throws NotFoundException on missing campaign in a sandbox
* @throws BadRequestException
*/
public function createOrUpdate(DonationUpserted $message): Salesforce18Id
{
if (getenv('DISABLE_CLIENT_PUSH')) {
$this->logger->info("Client push off: Skipping upsert of donation {$message->uuid}}");
throw new BadRequestException('Client push is off');
}
try {
$response = $this->getHttpClient()->post(
$this->getSetting('donation', 'baseUri') . '/' . $message->uuid,
[
'json' => $message->jsonSnapshot,
'headers' => [
'X-Webhook-Verify-Hash' => $this->hash(json_encode($message->jsonSnapshot)),
],
]
);
} catch (RequestException $ex) {
// Sandboxes that 404 on POST may be trying to sync up donations for non-existent campaigns and
// so have probably just been refreshed. In this case we want to update the local state of play
// to stop them getting pushed, instead of treating this as an error. So throw this for appropriate
// handling in the caller without an error level log. In production, 404s should not happen and
// so we continue to throw a `BadRequestException` which means `DonationRepostitory::doCreate()`
// will return false and the caller will log an error.
if ($ex->getCode() === 404 && getenv('APP_ENV') !== 'production') {
throw new NotFoundException();
}
// Sandboxes that 404 on PUT have probably just been refreshed. In this case we want to
// update the local state of play to stop them getting pushed, instead of treating this
// as an error. So throw this for appropriate handling in the caller without an error level
// log. In production, 404s should not happen and so we continue to `return false` which
// will lead the caller to log an error.
if ($ex->getCode() === 404 && getenv('APP_ENV') !== 'production') {
throw new NotFoundException();
}
$sandboxMissingLinkedResource = (
$ex->getCode() === 400 &&
getenv('APP_ENV') !== 'production' &&
str_contains($ex->getMessage(), '"entity is deleted"')
);
$exResponse = $ex->getResponse();
if ($sandboxMissingLinkedResource) {
/**
* The exception we throw here still takes the donation out of the push queue permenantly
* – {@see DonationRepository::doUpdate()} – but as this case is not as well
* understood, we also log a one time high severity error so we are better able to
* monitor these cases and try to understand what is happening in sandboxes that hit
* this edge case.
*/
$this->logger->error(sprintf(
'Donation update skipped due to missing sandbox resource. Exception %s: %s. Body: %s',
get_class($ex),
$ex->getMessage(),
$exResponse ? $exResponse->getBody() : 'N/A',
));
throw new NotFoundException();
}
$this->logger->error(sprintf(
'Donation upsert exception for donation UUID %s %s: %s. Body: %s',
$message->uuid,
get_class($ex),
$ex->getMessage(),
$exResponse ? $exResponse->getBody() : 'N/A',
));
throw new BadRequestException('Donation not upserted');
}
if (! in_array($response->getStatusCode(), [200, 201], true)) {
$this->logger->error('Donation upsert got non-success code ' . $response->getStatusCode());
throw new BadRequestException('Donation not upserted, response code ' . $response->getStatusCode());
}
/**
* @var array{'salesforceId': string} $donationCreatedResponse
*/
$donationCreatedResponse = json_decode($response->getBody()->getContents(), true);
return Salesforce18Id::of($donationCreatedResponse['salesforceId']);
}
private function hash(string $body): string
{
$secret = getenv('WEBHOOK_DONATION_SECRET');
if ($secret === false) {
throw new \Exception("Missing webhook donation secret");
}
return hash_hmac('sha256', trim($body), $secret);
}
}