1: <?php
2: /*
3: * This class was auto-generated from the API references found at
4: * https://apireference.connect.worldline-solutions.com/
5: */
6: namespace Worldline\Connect\Sdk\V1;
7:
8: use Worldline\Connect\Sdk\CallContext;
9: use Worldline\Connect\Sdk\Domain\DataObject;
10: use Worldline\Connect\Sdk\V1\Domain\APIError;
11: use Worldline\Connect\Sdk\V1\Domain\PaymentErrorResponse;
12: use Worldline\Connect\Sdk\V1\Domain\PayoutErrorResponse;
13: use Worldline\Connect\Sdk\V1\Domain\RefundErrorResponse;
14:
15: /**
16: * Class ExceptionFactory
17: *
18: * @package Worldline\Connect\Sdk\V1
19: */
20: class ExceptionFactory
21: {
22: const IDEMPOTENCE_ERROR_CODE = '1409';
23:
24: /**
25: * @param int $httpStatusCode
26: * @param DataObject $errorObject
27: * @param CallContext|null $callContext
28: *
29: * @return ResponseException
30: */
31: public function createException(
32: int $httpStatusCode,
33: DataObject $errorObject,
34: ?CallContext $callContext = null
35: ): ResponseException {
36: if ($errorObject instanceof PaymentErrorResponse && !is_null($errorObject->paymentResult)) {
37: return new DeclinedPaymentException($httpStatusCode, $errorObject);
38: }
39: if ($errorObject instanceof PayoutErrorResponse && !is_null($errorObject->payoutResult)) {
40: return new DeclinedPayoutException($httpStatusCode, $errorObject);
41: }
42: if ($errorObject instanceof RefundErrorResponse && !is_null($errorObject->refundResult)) {
43: return new DeclinedRefundException($httpStatusCode, $errorObject);
44: }
45: if ($httpStatusCode === 400) {
46: return new ValidationException($httpStatusCode, $errorObject);
47: }
48: if ($httpStatusCode === 403) {
49: return new AuthorizationException($httpStatusCode, $errorObject);
50: }
51: if ($httpStatusCode === 404) {
52: return new ReferenceException($httpStatusCode, $errorObject);
53: }
54: if ($httpStatusCode === 409) {
55: if ($callContext && strlen($callContext->getIdempotenceKey()) > 0
56: && $this->isIdempotenceError($errorObject)
57: ) {
58: return new IdempotenceException(
59: $httpStatusCode,
60: $errorObject,
61: null,
62: $callContext->getIdempotenceKey(),
63: $callContext->getIdempotenceRequestTimestamp()
64: );
65: }
66: return new ReferenceException($httpStatusCode, $errorObject);
67: }
68: if ($httpStatusCode === 410) {
69: return new ReferenceException($httpStatusCode, $errorObject);
70: }
71: if ($httpStatusCode === 500) {
72: return new PlatformException($httpStatusCode, $errorObject);
73: }
74: if ($httpStatusCode === 502) {
75: return new PlatformException($httpStatusCode, $errorObject);
76: }
77: if ($httpStatusCode === 503) {
78: return new PlatformException($httpStatusCode, $errorObject);
79: }
80: return new ApiException($httpStatusCode, $errorObject);
81: }
82:
83: /**
84: * @param DataObject $errorObject
85: *
86: * @return bool
87: */
88: protected function isIdempotenceError(DataObject $errorObject): bool
89: {
90: $errorObjectVariables = get_object_vars($errorObject);
91: if (!array_key_exists('errors', $errorObjectVariables)) {
92: return false;
93: }
94: $errors = $errorObjectVariables['errors'];
95: return is_array($errors)
96: && count($errors) === 1
97: && $errors[0] instanceof APIError
98: && $errors[0]->code == static::IDEMPOTENCE_ERROR_CODE;
99: }
100: }
101: