-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathAgentException.php
More file actions
172 lines (150 loc) · 5.4 KB
/
AgentException.php
File metadata and controls
172 lines (150 loc) · 5.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\PayPal\AgentCommerce\Exception;
use Shopware\Core\Framework\Log\Package;
use Swag\PayPal\AgentCommerce\Struct\V1\AgentErrorDetail;
use Swag\PayPal\AgentCommerce\Struct\V1\AgentErrorDetailCollection;
use Symfony\Component\HttpFoundation\Response;
#[Package('checkout')]
class AgentException extends AgentHttpException
{
public const INVALID_REQUEST = 'INVALID_REQUEST';
public const INVALID_CART_ID = 'INVALID_CART_ID';
public const CART_NOT_FOUND = 'CART_NOT_FOUND';
public const INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR';
public const SERVICE_UNAVAILABLE = 'SERVICE_UNAVAILABLE';
public const PAYMENT_PROCESSOR_UNAVAILABLE = 'PAYMENT_PROCESSOR_UNAVAILABLE';
public const PAYMENT_CAPTURE_FAILED = 'PAYMENT_CAPTURE_FAILED';
public const INVENTORY_SYSTEM_ERROR = 'INVENTORY_SYSTEM_ERROR';
public const ORDER_SYSTEM_ERROR = 'ORDER_SYSTEM_ERROR';
public static function requiredFieldsMissing(string ...$fields): self
{
$message = 'Required field \'{{ fields }}\' is missing';
$parameters = ['fields' => implode(', ', $fields)];
$details = new AgentErrorDetailCollection();
foreach ($fields as $field) {
$detail = (new AgentErrorDetail());
$detail->setField($field);
$detail->setIssue('MISSING_REQUIRED_FIELD');
$detail->setDescription(\sprintf('The field \'%s\' is required and cannot be empty', $field));
$details->add($detail);
}
return new self(
Response::HTTP_BAD_REQUEST,
self::INVALID_REQUEST,
$message,
$parameters,
$details
);
}
public static function requiredFieldInvalid(string $field, string $reason): self
{
$message = 'Required field \'{{ field }}\' is invalid: \'{{ reason }}\'';
$parameters = ['field' => $field, 'reason' => $reason];
$detail = new AgentErrorDetail();
$detail->setField($field);
$detail->setIssue('MISSING_REQUIRED_FIELD');
$detail->setDescription(\sprintf('The field \'%s\' is invalid: %s', $field, $reason));
return new self(
Response::HTTP_BAD_REQUEST,
self::INVALID_REQUEST,
$message,
$parameters,
new AgentErrorDetailCollection([$detail])
);
}
public static function invalidJSONFormat(): self
{
return new self(
Response::HTTP_BAD_REQUEST,
self::INVALID_REQUEST,
'Request body contains invalid JSON'
);
}
public static function unauthorized(string $message, ?\Throwable $previous = null): self
{
return new self(
Response::HTTP_UNAUTHORIZED,
self::INVALID_REQUEST,
$message,
previous: $previous,
);
}
public static function invalidCartId(): self
{
return new self(
Response::HTTP_BAD_REQUEST,
self::INVALID_CART_ID,
'Cart ID format is invalid. Expected format: CART-[a-zA-Z0-9]{32}'
);
}
public static function cartNotFound(string $token): self
{
return new self(
Response::HTTP_NOT_FOUND,
self::CART_NOT_FOUND,
'Cart with ID \'{{ token }}\' does not exist',
['token' => $token]
);
}
public static function databaseConnectionFailure(): self
{
return new self(
Response::HTTP_INTERNAL_SERVER_ERROR,
self::INTERNAL_SERVER_ERROR,
'A temporary system error occurred. Please try again later.'
);
}
public static function externalServiceFailure(): self
{
return new self(
Response::HTTP_INTERNAL_SERVER_ERROR,
self::SERVICE_UNAVAILABLE,
'The payment processor is currently unavailable. Please try again later.'
);
}
public static function paymentProcessorUnavailable(): self
{
return new self(
Response::HTTP_INTERNAL_SERVER_ERROR,
self::PAYMENT_PROCESSOR_UNAVAILABLE,
'Payment processing is temporarily unavailable'
);
}
public static function paymentCaptureFailed(string $message): self
{
$detail = (new AgentErrorDetail());
$detail->setField('payment_method');
$detail->setIssue('CAPTURE_FAILED');
$detail->setDescription($message);
$details = new AgentErrorDetailCollection([$detail]);
return new self(
Response::HTTP_INTERNAL_SERVER_ERROR,
self::PAYMENT_CAPTURE_FAILED,
'Unable to capture payment at this time',
[],
$details
);
}
public static function inventorySystemError(): self
{
return new self(
Response::HTTP_INTERNAL_SERVER_ERROR,
self::INVENTORY_SYSTEM_ERROR,
'Unable to reserve inventory for checkout'
);
}
public static function orderSystemError(?\Throwable $previous = null): self
{
return new self(
Response::HTTP_INTERNAL_SERVER_ERROR,
self::ORDER_SYSTEM_ERROR,
'Order could not be created due to system error',
previous: $previous,
);
}
}