-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer_api.py
128 lines (117 loc) · 4.81 KB
/
customer_api.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import urllib, time
from xml.dom import minidom
from datetime import date, timedelta, datetime
from django.conf import settings
url = 'https://<< URL to Foxy >>/api'
class Customer(object):
def get_hash(self, email, key):
action = 'customer_get'
params = urllib.urlencode ({
'customer_email': email,
'api_action': action,
'api_token': key
})
data = urllib.urlopen(url, params).read()
xml = minidom.parseString(data)
pwd_hash = xml.getElementsByTagName('customer_password')[0].firstChild.data
return pwd_hash
def get_cc_info(self, customer_id, key):
action = 'customer_get'
params = urllib.urlencode ({
'customer_id': customer_id,
'api_action': action,
'api_token': key
})
data = urllib.urlopen(url, params).read()
xml = minidom.parseString(data)
cc_number = xml.getElementsByTagName('cc_number')[0].firstChild.data
cc_exp_month = xml.getElementsByTagName('cc_exp_month')[0].firstChild.data
cc_exp_year = xml.getElementsByTagName('cc_exp_year')[0].firstChild.data
cc_data = [cc_number, cc_exp_month, cc_exp_year]
return cc_data
def reset_password(self, data, customer_id, key):
action = 'customer_save'
url_params = {
'customer_id': customer_id,
'api_action': action,
'api_token': key,
'customer_password': data['password'],
}
params = urllib.urlencode (url_params)
fc_data = urllib.urlopen(url, params).read()
xml = minidom.parseString(fc_data)
status = xml.getElementsByTagName('result')[0].firstChild.data
if status == 'SUCCESS':
return True
return False
def update_user(self, data, customer_id, key):
action = 'customer_save'
url_params = {
'customer_id': customer_id,
'api_action': action,
'api_token': key,
'customer_email': data['username'],
}
if data['password1'] != '':
url_params['customer_password'] = data['password1']
params = urllib.urlencode(url_params)
fc_data = urllib.urlopen(url, params).read()
xml = minidom.parseString(fc_data)
status = xml.getElementsByTagName('result')[0].firstChild.data
if status == 'SUCCESS':
return True
return False
def check_cc_expiry(self, customer_id, key):
action = 'customer_get'
params = urllib.urlencode ({
'customer_id': customer_id,
'api_action': action,
'api_token': key
})
data = urllib.urlopen(url, params).read()
xml = minidom.parseString(data)
cc_exp_month = xml.getElementsByTagName('cc_exp_month')[0].firstChild.data
cc_exp_year = xml.getElementsByTagName('cc_exp_year')[0].firstChild.data
card_date = date(int(cc_exp_year), int(cc_exp_month), 1)
today = date.today()
target_date = date(today.year, today.month, 1) + timedelta(weeks=10)
if card_date < target_date:
return True
return False
def cancel_account(self, sub_token, key):
action = 'subscription_cancel'
params = urllib.urlencode ({
'sub_token': sub_token,
'api_action': action,
'api_token': key
})
data = urllib.urlopen(url, params).read()
xml = minidom.parseString(data)
result = xml.getElementsByTagName('result')[0].firstChild.data
messages = xml.getElementsByTagName('messages')
for message in messages:
message = message.getElementsByTagName('message'
)[0].firstChild.data
if result == 'SUCCESS':
return result
return message
def get_subscription(self, sub_token, key):
action = 'subscription_get'
params = urllib.urlencode ({
'sub_token': sub_token,
'api_action': action,
'api_token': key
})
data = urllib.urlopen(url, params).read()
xml = minidom.parseString(data)
result = xml.getElementsByTagName('result')[0].firstChild.data
messages = xml.getElementsByTagName('messages')
for message in messages:
message = message.getElementsByTagName('message'
)[0].firstChild.data
if result == 'SUCCESS':
next_sub = xml.getElementsByTagName('next_transaction_date'
)[0].firstChild.data
next_sub_date = datetime(*time.strptime(next_sub, '%Y-%m-%d')[0:6]).date()
return True, next_sub_date
return False, message