-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathApplePayClient.php
More file actions
111 lines (105 loc) · 4.08 KB
/
ApplePayClient.php
File metadata and controls
111 lines (105 loc) · 4.08 KB
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
<?php
namespace Square\ApplePay;
use Psr\Http\Client\ClientInterface;
use Square\Core\Client\RawClient;
use Square\ApplePay\Requests\RegisterDomainRequest;
use Square\Types\RegisterDomainResponse;
use Square\Exceptions\SquareException;
use Square\Exceptions\SquareApiException;
use Square\Core\Json\JsonApiRequest;
use Square\Environments;
use Square\Core\Client\HttpMethod;
use JsonException;
use Psr\Http\Client\ClientExceptionInterface;
class ApplePayClient
{
/**
* @var array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options @phpstan-ignore-next-line Property is used in endpoint methods via HttpEndpointGenerator
*/
private array $options;
/**
* @var RawClient $client
*/
private RawClient $client;
/**
* @param RawClient $client
* @param ?array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options
*/
public function __construct(
RawClient $client,
?array $options = null,
) {
$this->client = $client;
$this->options = $options ?? [];
}
/**
* Activates a domain for use with Apple Pay on the Web and Square. A validation
* is performed on this domain by Apple to ensure that it is properly set up as
* an Apple Pay enabled domain.
*
* This endpoint provides an easy way for platform developers to bulk activate
* Apple Pay on the Web with Square for merchants using their platform.
*
* Note: You will need to host a valid domain verification file on your domain to support Apple Pay. The
* current version of this file is always available at https://app.squareup.com/digital-wallets/apple-pay/apple-developer-merchantid-domain-association,
* and should be hosted at `.well_known/apple-developer-merchantid-domain-association` on your
* domain. This file is subject to change; we strongly recommend checking for updates regularly and avoiding
* long-lived caches that might not keep in sync with the correct file version.
*
* To learn more about the Web Payments SDK and how to add Apple Pay, see [Take an Apple Pay Payment](https://developer.squareup.com/docs/web-payments/apple-pay).
*
* @param RegisterDomainRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return RegisterDomainResponse
* @throws SquareException
* @throws SquareApiException
*/
public function registerDomain(RegisterDomainRequest $request, ?array $options = null): RegisterDomainResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/apple-pay/domains",
method: HttpMethod::POST,
body: $request,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return RegisterDomainResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
}