-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy patherror_handler.py
More file actions
104 lines (84 loc) · 3.42 KB
/
error_handler.py
File metadata and controls
104 lines (84 loc) · 3.42 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
import config
from flask_ask import statement
# These are errors that will not be handled by Amazon Pay; Merchant must handle
def handleErrors(request):
errorMessage = ''
permissionsError = False
actionResponseStatusCode = request['status']['code']
actionResponseStatusMessage = request['status']['message']
actionResponsePayloadMessage = ''
if 'errorMessage' in request['payload']:
actionResponsePayloadMessage = request['payload']['errorMessage']
knownPermissionError = {
# Permissions errors - These must be resolved before a user can use Amazon Pay
'ACCESS_DENIED',
'ACCESS_NOT_REQUESTED',
'FORBIDDEN'
}
knownIntegrationOrRuntimeError = {
# Integration errors - These must be resolved before Amazon Pay can run
'BuyerEqualsSeller',
'InvalidParameterValue',
'InvalidSandboxCustomerEmail',
'InvalidSellerId',
'UnauthorizedAccess',
'UnsupportedCountryOfEstablishment',
'UnsupportedCurrency',
# Runtime errors - These must be resolved before a charge action can occur
'DuplicateRequest',
'InternalServerError',
'InvalidAuthorizationAmount',
'InvalidBillingAgreementId',
'InvalidBillingAgreementStatus',
'InvalidPaymentAction',
'PeriodicAmountExceeded',
'ProviderNotAuthorized',
'ServiceUnavailable'
}
if actionResponseStatusMessage in knownPermissionError:
permissionsError = True
errorMessage = config.enablePermission
elif actionResponseStatusMessage in knownIntegrationOrRuntimeError:
errorMessage = config.errorMessage + config.errorStatusCode + actionResponseStatusCode + '.' + config.errorStatusMessage + actionResponseStatusMessage + '.' + config.errorPayloadMessage + actionResponsePayloadMessage
else:
errorMessage = config.errorUnknown
debug('handleErrors', request)
# If it is a permissions error send a permission consent card to the user, otherwise .speak() error to resolve during testing
if permissionsError:
return statement(errorMessage).consent_card(config.scope)
else:
return statement(errorMessage)
# If billing agreement equals any of these states, you need to get the user to update their payment method
# Once payment method is updated, billing agreement state will go back to OPEN and you can charge the payment method
def handleBillingAgreementState( billingAgreementStatus, request):
errorMessage = ''
knownStatus = {
'CANCELED',
'CLOSED',
'SUSPENDED'
}
if billingAgreementStatus in knownStatus:
errorMessage = config.errorBillingAgreement + billingAgreementStatus + config.errorBillingAgreementMessage
else:
errorMessage = config.errorUnknown
debug('handleBillingAgreementState', request)
return statement(errorMessage)
# Ideal scenario in authorization decline is that you save the session, allow the customer to fix their payment method,
# and allow customer to resume session. This is just a simple message to tell the user their order was not placed.
def handleAuthorizationDeclines( authorizationStatusReasonCode, request):
errorMessage = ''
knownReasonCode = {
'AmazonRejected',
'InvalidPaymentMethod',
'ProcessingFailure',
'TransactionTimedOut'
}
if authorizationStatusReasonCode in knownReasonCode:
errorMessage = config.authorizationDeclineMessage
else:
errorMessage = config.errorUnknown
debug('handleAuthorizationDeclines', request)
return statement(errorMessage)
# Output object to console for debugging purposes
def debug(funcName, request ):
print('ERROR in %s --- %s\n' % (funcName, str(request)))