-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstipe_util.py
More file actions
86 lines (67 loc) · 2.4 KB
/
stipe_util.py
File metadata and controls
86 lines (67 loc) · 2.4 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
import stripe
from django.conf import settings
stripe.api_key = settings.STRIPE_API_KEY
class StripePayment(object):
def __init__(self, customer_code, user=None):
self.customer_code = customer_code
self.user = user
def retrieve_customer(self):
if self.customer_code:
customer = stripe.Customer.retrieve(self.customer_code)
else:
latest_transaction = self.user.current_payment_transaction()
if latest_transaction:
try:
subscription = stripe.Subscription.retrieve(latest_transaction.transaction_id)
customer = stripe.Customer.retrieve(subscription.customer)
self.customer_code = customer['id']
except:
customer = None
else:
customer = None
return customer
def get(self, card_id=None):
customer = self.retrieve_customer()
response = {
'cards': [],
'default_card': None
}
if customer and card_id:
return customer['default_source']
elif customer:
cards = stripe.Customer.list_sources(self.customer_code, object="card", limit=100)
response['cards'] = cards['data']
response['default_card'] = customer['default_source']
return response
def add(self, token):
customer = self.retrieve_customer()
response = {
'cards': [],
'default_card': None
}
if customer:
try:
card_data = stripe.Customer.create_source(self.customer_code, source=token)
return card_data
except stripe.CardError as err:
return {}
return response
def delete(self, card_id):
customer = self.retrieve_customer()
if customer:
try:
s_response = stripe.Customer.delete_source(self.customer_code, card_id)
if s_response['deleted']:
return True
except:
pass
return False
def make_primary(self, card_id):
customer = self.retrieve_customer()
if customer:
try:
s_response = stripe.Customer.modify(self.customer_code, default_source=card_id)
return True
except:
pass
return False