-
Notifications
You must be signed in to change notification settings - Fork 0
/
square_client.py
69 lines (54 loc) · 2.28 KB
/
square_client.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
import json
from collections import namedtuple
from uuid import uuid4
import secret
from square.client import Client
SQUARE_APPLICATION_TOKEN = secret.square_application_token()
assert SQUARE_APPLICATION_TOKEN is not None
client = Client(
environment="production",
access_token=SQUARE_APPLICATION_TOKEN
)
customers_api = client.customers
payments_api = client.payments
def create_customer(given_name: str = None, family_name: str = None, email_address: str = None):
# todo check if customer already exists
body = {'email_address': email_address}
result = customers_api.create_customer(body).body
print(result)
return result["customer"]
# # doesn't work fixme
# def customer_already_exists(given_name: str = None, family_name: str = None, email_address: str = None):
# existing_customers = get_customers(given_name=given_name, family_name=family_name, email_address=email_address)
# return existing_customers == {}
#
#
# def get_customers(given_name: str = None, family_name: str = None, email_address: str = None):
# if not (given_name is not None and family_name is not None and email_address is not None):
# raise ValueError("must provide given_name, family_name, and email_address")
#
# body = {'limit': 1, 'given_name': given_name, 'family_name': family_name, 'email_address': email_address}
# existing_customers = customers_api.search_customers(body)
# return existing_customers
def store_card_on_file(nonce: str, customer_id):
body = {'card_nonce': nonce}
result = customers_api.create_customer_card(customer_id, body).body
return result
def donate(cents: int, customer, default):
body = dict()
body['source_id'] = default
body['idempotency_key'] = str(uuid4())[:44]
body['amount_money'] = {}
body['amount_money']['amount'] = cents
body['amount_money']['currency'] = 'USD'
body['autocomplete'] = True
body['customer_id'] = customer.id
result = payments_api.create_payment(body)
if result.is_success():
return result.body
elif result.is_error():
return result.errors
def get_square_customer_by_id(customer_id: str):
customer = customers_api.retrieve_customer(customer_id).text # todo what if I do .body instea of .text? do I still need the blackmagic below?
customer = json.loads(customer, object_hook=lambda d: namedtuple('customers_x', d.keys())(*d.values()))[0]
return customer