From 46828a2b71500157532a7d030a8e4ce7364fa8f2 Mon Sep 17 00:00:00 2001 From: Miguel Vela Romera Date: Wed, 23 Sep 2020 10:41:36 +0200 Subject: [PATCH] add additional Afterpay error messages --- composer.json | 2 +- .../Helper/AfterpayErrorMessageHelper.php | 30 ++++++++++++++++++- .../Helper/AfterpayErrorMessageHelperTest.php | 8 +++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 7fd7b5e..a1d5007 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "payvision/payvision-sdk-php", "description": "Payvision PHP SDK", "type": "library", - "version": "5.1.1", + "version": "5.1.2", "license": "MIT", "require": { "php": "^7.2.5|^7.3", diff --git a/src/Domain/Service/Helper/AfterpayErrorMessageHelper.php b/src/Domain/Service/Helper/AfterpayErrorMessageHelper.php index 8a4b83a..388a448 100644 --- a/src/Domain/Service/Helper/AfterpayErrorMessageHelper.php +++ b/src/Domain/Service/Helper/AfterpayErrorMessageHelper.php @@ -13,6 +13,18 @@ class AfterpayErrorMessageHelper { + // Error code: 300400800 + public const AGE_IS_UNDER_18 = 'age.under.18'; + public const INCORRECT_ADDRESS = 'incorrect.address'; + public const MAXIMUM_OPEN_ORDERS_REACHED = 'maximum.open.orders.reached'; + public const NO_RESPONSE_FROM_AQUIRER = 'no.response.from.aquirer'; + public const ORDER_IS_NOT_ACCEPTED_BY_AFTERPAY = 'order.is.not.accepted'; + public const TRANSACTION_DECLINED_BY_AUTHORIZATION_SYSTEM = 'transaction.declined'; + + // Error code: 300102000 + public const INVALID_EMAIL_ADDRESS = 'invalid.email.address'; + + // Error code: 100400030 public const BILLTO_CITY_MISSING = 'billto.city.missing'; public const BILLTO_HOUSENUMBER_INVALID = 'billto.housenumber.invalid'; public const BILLTO_HOUSENUMBER_MISSING = 'billto.housenumber.missing'; @@ -57,6 +69,19 @@ class AfterpayErrorMessageHelper public const SHIPTO_POSTALCODE_MISSING = 'shipto.postalcode.missing'; public const SHIPTO_STREETNAME_MISSING = 'shipto.streetname.missing'; + protected const FIXED_STRING_MESSAGES = [ + // Error code: 300400800. + 'age is under 18' => self::AGE_IS_UNDER_18, + 'incorrect address' => self::INCORRECT_ADDRESS, + 'maximum open orders reached' => self::MAXIMUM_OPEN_ORDERS_REACHED, + 'no response from aquirer' => self::NO_RESPONSE_FROM_AQUIRER, + 'order is not accepted by afterpay' => self::ORDER_IS_NOT_ACCEPTED_BY_AFTERPAY, + 'transaction declined by authorization system' => self::TRANSACTION_DECLINED_BY_AUTHORIZATION_SYSTEM, + + // Error code: 300102000. + 'invalid e-mail address' => self::INVALID_EMAIL_ADDRESS, + ]; + /** * @param string $errorDetail * @return array @@ -67,7 +92,7 @@ public static function extractMessageCodes(string $errorDetail): array $result = []; $messages = \explode('|', $errorDetail); foreach ($messages as $message) { - $result[] = self::extractMessageCode(\trim($message)); + $result[] = self::extractMessageCode(\strtolower(\trim($message))); } return $result; } @@ -77,6 +102,9 @@ public static function extractMessageCodes(string $errorDetail): array */ protected static function extractMessageCode(string $message): string { + if (\array_key_exists($message, self::FIXED_STRING_MESSAGES)) { + return self::FIXED_STRING_MESSAGES[$message]; + } if (\preg_match('/^(field\.)?(?P[a-z]+\.[a-z0-9.]+)/', $message, $matches)) { return $matches['code']; } diff --git a/tests/Test/Unit/Domain/Service/Helper/AfterpayErrorMessageHelperTest.php b/tests/Test/Unit/Domain/Service/Helper/AfterpayErrorMessageHelperTest.php index 0ad7134..b5858e5 100644 --- a/tests/Test/Unit/Domain/Service/Helper/AfterpayErrorMessageHelperTest.php +++ b/tests/Test/Unit/Domain/Service/Helper/AfterpayErrorMessageHelperTest.php @@ -63,6 +63,14 @@ public function afterpayExtractMessageCodeProvider(): array 'field.billto.phonenumber2.invalid (fixed line and/or mobile number is invalid)', [AfterpayErrorMessageHelper::BILLTO_PHONENUMBER2_INVALID], ], + 'Fixed string message' => [ + 'age is under 18', + [AfterpayErrorMessageHelper::AGE_IS_UNDER_18] + ], + 'Fixed string message in caps' => [ + 'INVALID E-MAIL ADDRESS', + [AfterpayErrorMessageHelper::INVALID_EMAIL_ADDRESS] + ], ]; }