-
Notifications
You must be signed in to change notification settings - Fork 1
/
merchant_auth.py
55 lines (38 loc) · 1.96 KB
/
merchant_auth.py
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
from authorizenet import apicontractsv1
from django.conf import settings
from payment_authorizenet.enums import ServerMode
class AuthorizeNetError(Exception):
"""Exceptions related to Authorize.net operations
To use the exception, just do this: raise AuthorizeNetError('Message here')
"""
pass
class AuthNet:
"""This class is intended to be inherited by any class wishing to perform
operations on the Authorize.net gateway.
You'll always need to supply credentials, which this class sets
and makes available from settings"""
def __init__(self):
# ********** Set Authentication Credentials *************
merchantAuth = apicontractsv1.merchantAuthenticationType()
if not hasattr(settings, 'AUTHORIZE_NET_API_LOGIN_ID'):
msg = 'AUTHORIZE_NET_API_LOGIN_ID does not exist ' \
'in your Django settings'
raise AuthorizeNetError(msg)
merchantAuth.name = settings.AUTHORIZE_NET_API_LOGIN_ID
if not hasattr(settings, 'AUTHORIZE_NET_TRANSACTION_KEY'):
msg = 'AUTHORIZE_NET_TRANSACTION_KEY does not exist ' \
'in your Django settings'
raise AuthorizeNetError(msg)
merchantAuth.transactionKey = settings.AUTHORIZE_NET_TRANSACTION_KEY
self.merchantAuth = merchantAuth
# ********** Set the POST URL for the controllers *************
SANDBOX = 'https://apitest.authorize.net/xml/v1/request.api'
PRODUCTION = 'https://api2.authorize.net/xml/v1/request.api'
if not hasattr(settings, 'SERVER_MODE'):
msg = 'You must set SERVER_MODE in your Django settings to a ' \
'ServerMode enum: {}}'
raise AuthorizeNetError(msg.format(ServerMode.str_list()))
if settings.SERVER_MODE == ServerMode.production.value:
self.post_url = PRODUCTION
else: # any evironment that's not production should use sandbox
self.post_url = SANDBOX