Skip to content

Commit

Permalink
add additional Afterpay error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Vela Romera committed Sep 23, 2020
1 parent ba1b6ed commit 46828a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 29 additions & 1 deletion src/Domain/Service/Helper/AfterpayErrorMessageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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<code>[a-z]+\.[a-z0-9.]+)/', $message, $matches)) {
return $matches['code'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
],
];
}

Expand Down

0 comments on commit 46828a2

Please sign in to comment.