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
|
import re
import braintree
from braintree.address import Address
from braintree.error_result import ErrorResult
from braintree.exceptions.not_found_error import NotFoundError
from braintree.resource import Resource
from braintree.successful_result import SuccessfulResult
class AddressGateway(object):
def __init__(self, gateway):
self.gateway = gateway
self.config = gateway.config
def create(self, params={}):
Resource.verify_keys(params, Address.create_signature())
if not "customer_id" in params:
raise KeyError("customer_id must be provided")
if not re.search("\A[0-9A-Za-z_-]+\Z", params["customer_id"]):
raise KeyError("customer_id contains invalid characters")
response = self.config.http().post("/customers/" + params.pop("customer_id") + "/addresses", {"address": params})
if "address" in response:
return SuccessfulResult({"address": Address(self.gateway, response["address"])})
elif "api_error_response" in response:
return ErrorResult(self.gateway, response["api_error_response"])
def delete(self, customer_id, address_id):
self.config.http().delete("/customers/" + customer_id + "/addresses/" + address_id)
return SuccessfulResult()
def find(self, customer_id, address_id):
try:
if customer_id == None or customer_id.strip() == "" or address_id == None or address_id.strip() == "":
raise NotFoundError()
response = self.config.http().get("/customers/" + customer_id + "/addresses/" + address_id)
return Address(self.gateway, response["address"])
except NotFoundError:
raise NotFoundError("address for customer " + customer_id + " with id " + address_id + " not found")
def update(self, customer_id, address_id, params={}):
Resource.verify_keys(params, Address.update_signature())
response = self.config.http().put(
"/customers/" + customer_id + "/addresses/" + address_id,
{"address": params}
)
if "address" in response:
return SuccessfulResult({"address": Address(self.gateway, response["address"])})
elif "api_error_response" in response:
return ErrorResult(self.gateway, response["api_error_response"])
|