forked from braintree/braintree_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomer_gateway.py
More file actions
77 lines (63 loc) · 3.57 KB
/
customer_gateway.py
File metadata and controls
77 lines (63 loc) · 3.57 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
import braintree
from braintree.customer import Customer
from braintree.error_result import ErrorResult
from braintree.exceptions.not_found_error import NotFoundError
from braintree.ids_search import IdsSearch
from braintree.resource import Resource
from braintree.resource_collection import ResourceCollection
from braintree.successful_result import SuccessfulResult
from braintree.transparent_redirect import TransparentRedirect
class CustomerGateway(object):
def __init__(self, gateway):
self.gateway = gateway
self.config = gateway.config
def all(self):
response = self.config.http().post("/customers/advanced_search_ids")
return ResourceCollection(None, response, self.__fetch)
def confirm_transparent_redirect(self, query_string):
id = self.gateway.transparent_redirect._parse_and_validate_query_string(query_string)["id"][0]
return self._post("/customers/all/confirm_transparent_redirect_request", {"id": id})
def create(self, params={}):
Resource.verify_keys(params, Customer.create_signature())
return self._post("/customers", {"customer": params})
def delete(self, customer_id):
self.config.http().delete("/customers/" + customer_id)
return SuccessfulResult()
def find(self, customer_id):
try:
response = self.config.http().get("/customers/" + customer_id)
return Customer(self.gateway, response["customer"])
except NotFoundError:
raise NotFoundError("customer with id " + customer_id + " not found")
def tr_data_for_create(self, tr_data, redirect_url):
Resource.verify_keys(tr_data, [{"customer": Customer.create_signature()}])
tr_data["kind"] = TransparentRedirect.Kind.CreateCustomer
return self.gateway.transparent_redirect.tr_data(tr_data, redirect_url)
def tr_data_for_update(self, tr_data, redirect_url):
Resource.verify_keys(tr_data, ["customer_id", {"customer": Customer.update_signature()}])
tr_data["kind"] = TransparentRedirect.Kind.UpdateCustomer
return self.gateway.transparent_redirect.tr_data(tr_data, redirect_url)
def transparent_redirect_create_url(self):
return self.config.base_merchant_url() + "/customers/all/create_via_transparent_redirect_request"
def transparent_redirect_update_url(self):
return self.config.base_merchant_url() + "/customers/all/update_via_transparent_redirect_request"
def update(self, customer_id, params={}):
Resource.verify_keys(params, Customer.update_signature())
response = self.config.http().put("/customers/" + customer_id, {"customer": params})
if "customer" in response:
return SuccessfulResult({"customer": Customer(self.gateway, response["customer"])})
elif "api_error_response" in response:
return ErrorResult(self.gateway, response["api_error_response"])
def __fetch(self, query, ids):
criteria = {}
criteria["ids"] = IdsSearch.ids.in_list(ids).to_param()
response = self.config.http().post("/customers/advanced_search", {"search": criteria})
return [Customer(self.gateway, item) for item in ResourceCollection._extract_as_array(response["customers"], "customer")]
def _post(self, url, params={}):
response = self.config.http().post(url, params)
if "customer" in response:
return SuccessfulResult({"customer": Customer(self.gateway, response["customer"])})
elif "api_error_response" in response:
return ErrorResult(self.gateway, response["api_error_response"])
else:
pass