-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPaystackError.m
More file actions
109 lines (91 loc) · 5.45 KB
/
PaystackError.m
File metadata and controls
109 lines (91 loc) · 5.45 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
//
// PaystackError.m
// Paystack
//
#import "PaystackError.h"
#import "PSTCKFormEncoder.h"
NSString *const PaystackDomain = @"com.paystack.lib";
NSString *const PSTCKCardErrorCodeKey = @"com.paystack.lib:CardErrorCodeKey";
NSString *const PSTCKErrorMessageKey = @"com.paystack.lib:ErrorMessageKey";
NSString *const PSTCKErrorParameterKey = @"com.paystack.lib:ErrorParameterKey";
NSString *const PSTCKInvalidNumber = @"com.paystack.lib:InvalidNumber";
NSString *const PSTCKInvalidExpMonth = @"com.paystack.lib:InvalidExpiryMonth";
NSString *const PSTCKInvalidExpYear = @"com.paystack.lib:InvalidExpiryYear";
NSString *const PSTCKInvalidCVC = @"com.paystack.lib:InvalidCVC";
NSString *const PSTCKIncorrectNumber = @"com.paystack.lib:IncorrectNumber";
NSString *const PSTCKExpiredCard = @"com.paystack.lib:ExpiredCard";
NSString *const PSTCKCardDeclined = @"com.paystack.lib:CardDeclined";
NSString *const PSTCKProcessingError = @"com.paystack.lib:ProcessingError";
NSString *const PSTCKIncorrectCVC = @"com.paystack.lib:IncorrectCVC";
@implementation NSError(Paystack)
+ (NSError *)pstck_errorFromPaystackResponse:(NSDictionary *)jsonDictionary {
NSString *status = [jsonDictionary[@"status"] description];
if (![status isEqual: @"0"]) {
return nil;
}
NSString *devMessage = jsonDictionary[@"message"];
// There should always be a message for the error
if (devMessage == nil) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"Could not interpret the error response that was returned from Paystack.",
PSTCKErrorMessageKey: @"Could not interpret the error response that was returned from Paystack."
};
return [[NSError alloc] initWithDomain:PaystackDomain code:PSTCKAPIError userInfo:userInfo];
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[PSTCKErrorMessageKey] = devMessage;
return [[NSError alloc] initWithDomain:PaystackDomain code:0 userInfo:userInfo];
}
- (NSError *)pstck_errorFromPaystackResponseOld:(NSDictionary *)jsonDictionary {
NSDictionary *errorDictionary = jsonDictionary[@"error"];
if (!errorDictionary) {
return nil;
}
NSString *type = errorDictionary[@"type"];
NSString *devMessage = errorDictionary[@"message"];
NSString *parameter = errorDictionary[@"param"];
NSInteger code = 0;
// There should always be a message and type for the error
if (devMessage == nil || type == nil) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"Could not interpret the error response that was returned from Paystack.",
PSTCKErrorMessageKey: @"Could not interpret the error response that was returned from Paystack."
};
return [[NSError alloc] initWithDomain:PaystackDomain code:PSTCKAPIError userInfo:userInfo];
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[PSTCKErrorMessageKey] = devMessage;
if (parameter) {
userInfo[PSTCKErrorParameterKey] = [PSTCKFormEncoder stringByReplacingSnakeCaseWithCamelCase:parameter];
}
if ([type isEqualToString:@"api_error"]) {
code = PSTCKAPIError;
userInfo[NSLocalizedDescriptionKey] = PSTCKUnexpectedError;
} else if ([type isEqualToString:@"invalid_request_error"]) {
code = PSTCKInvalidRequestError;
userInfo[NSLocalizedDescriptionKey] = devMessage;
} else if ([type isEqualToString:@"card_error"]) {
code = PSTCKCardError;
NSDictionary *errorCodes = @{
@"incorrect_number": @{@"code": PSTCKIncorrectNumber, @"message": PSTCKCardErrorInvalidNumberUserMessage},
@"invalid_number": @{@"code": PSTCKInvalidNumber, @"message": PSTCKCardErrorInvalidNumberUserMessage},
@"invalid_expiry_month": @{@"code": PSTCKInvalidExpMonth, @"message": PSTCKCardErrorInvalidExpMonthUserMessage},
@"invalid_expiry_year": @{@"code": PSTCKInvalidExpYear, @"message": PSTCKCardErrorInvalidExpYearUserMessage},
@"invalid_cvc": @{@"code": PSTCKInvalidCVC, @"message": PSTCKCardErrorInvalidCVCUserMessage},
@"expired_card": @{@"code": PSTCKExpiredCard, @"message": PSTCKCardErrorExpiredCardUserMessage},
@"incorrect_cvc": @{@"code": PSTCKIncorrectCVC, @"message": PSTCKCardErrorInvalidCVCUserMessage},
@"card_declined": @{@"code": PSTCKCardDeclined, @"message": PSTCKCardErrorDeclinedUserMessage},
@"processing_error": @{@"code": PSTCKProcessingError, @"message": PSTCKCardErrorProcessingErrorUserMessage},
};
NSDictionary *codeMapEntry = errorCodes[errorDictionary[@"code"]];
if (codeMapEntry) {
userInfo[PSTCKCardErrorCodeKey] = codeMapEntry[@"code"];
userInfo[NSLocalizedDescriptionKey] = codeMapEntry[@"message"];
} else {
userInfo[PSTCKCardErrorCodeKey] = errorDictionary[@"code"];
userInfo[NSLocalizedDescriptionKey] = devMessage;
}
}
return [[NSError alloc] initWithDomain:PaystackDomain code:code userInfo:userInfo];
}
@end