| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 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: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | class ExceptionFactory |
| 21: | { |
| 22: | const IDEMPOTENCE_ERROR_CODE = '1409'; |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public function createException( |
| 31: | $httpStatusCode, |
| 32: | DataObject $errorObject, |
| 33: | CallContext $callContext = null |
| 34: | ) { |
| 35: | if ($errorObject instanceof PaymentErrorResponse && !is_null($errorObject->paymentResult)) { |
| 36: | return new DeclinedPaymentException($httpStatusCode, $errorObject); |
| 37: | } |
| 38: | if ($errorObject instanceof PayoutErrorResponse && !is_null($errorObject->payoutResult)) { |
| 39: | return new DeclinedPayoutException($httpStatusCode, $errorObject); |
| 40: | } |
| 41: | if ($errorObject instanceof RefundErrorResponse && !is_null($errorObject->refundResult)) { |
| 42: | return new DeclinedRefundException($httpStatusCode, $errorObject); |
| 43: | } |
| 44: | if ($httpStatusCode === 400) { |
| 45: | return new ValidationException($httpStatusCode, $errorObject); |
| 46: | } |
| 47: | if ($httpStatusCode === 403) { |
| 48: | return new AuthorizationException($httpStatusCode, $errorObject); |
| 49: | } |
| 50: | if ($httpStatusCode === 404) { |
| 51: | return new ReferenceException($httpStatusCode, $errorObject); |
| 52: | } |
| 53: | if ($httpStatusCode === 409) { |
| 54: | if ($callContext && strlen($callContext->getIdempotenceKey()) > 0 && |
| 55: | $this->isIdempotenceError($errorObject) |
| 56: | ) { |
| 57: | return new IdempotenceException( |
| 58: | $httpStatusCode, |
| 59: | $errorObject, |
| 60: | null, |
| 61: | $callContext->getIdempotenceKey(), |
| 62: | $callContext->getIdempotenceRequestTimestamp() |
| 63: | ); |
| 64: | } |
| 65: | return new ReferenceException($httpStatusCode, $errorObject); |
| 66: | } |
| 67: | if ($httpStatusCode === 410) { |
| 68: | return new ReferenceException($httpStatusCode, $errorObject); |
| 69: | } |
| 70: | if ($httpStatusCode === 500) { |
| 71: | return new PlatformException($httpStatusCode, $errorObject); |
| 72: | } |
| 73: | if ($httpStatusCode === 502) { |
| 74: | return new PlatformException($httpStatusCode, $errorObject); |
| 75: | } |
| 76: | if ($httpStatusCode === 503) { |
| 77: | return new PlatformException($httpStatusCode, $errorObject); |
| 78: | } |
| 79: | return new ApiException($httpStatusCode, $errorObject); |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | protected function isIdempotenceError(DataObject $errorObject) |
| 87: | { |
| 88: | $errorObjectVariables = get_object_vars($errorObject); |
| 89: | if (!array_key_exists('errors', $errorObjectVariables)) { |
| 90: | return false; |
| 91: | } |
| 92: | $errors = $errorObjectVariables['errors']; |
| 93: | return is_array($errors) |
| 94: | && count($errors) === 1 |
| 95: | && $errors[0] instanceof APIError |
| 96: | && $errors[0]->code == static::IDEMPOTENCE_ERROR_CODE; |
| 97: | } |
| 98: | } |
| 99: | |