def _post(url, params={}): response = Http().post(url, params) if "transaction" in response: return SuccessfulResult( {"transaction": Transaction(response["transaction"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def create(params={}): """ Create an Address. A customer_id is required:: customer = braintree.Customer.create().customer result = braintree.Address.create({ "customer_id": customer.id, "first_name": "John", ... }) """ 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 = Http().post( "/customers/" + params.pop("customer_id") + "/addresses", {"address": params}) if "address" in response: return SuccessfulResult({"address": Address(response["address"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def expiring_between(start_date, end_date): """ Return a collection of credit cards expiring between the given dates. """ formatted_start_date = start_date.strftime("%m%Y") formatted_end_date = end_date.strftime("%m%Y") query = "start=%s&end=%s" % (formatted_start_date, formatted_end_date) response = Http().post("/payment_methods/all/expiring_ids?" + query) return ResourceCollection(query, response, CreditCard.__fetch_existing_between)
def delete(customer_id, address_id): """ Delete an address, given a customer_id and address_id:: result = braintree.Address.delete("my_customer_id", "my_address_id") """ Http().delete("/customers/" + customer_id + "/addresses/" + address_id) return SuccessfulResult()
def delete(credit_card_token): """ Delete a credit card, given a credit_card_id:: result = braintree.CreditCard.delete("my_credit_card_id") """ Http().delete("/payment_methods/" + credit_card_token) return SuccessfulResult()
def __fetch(query, ids): criteria = Transaction.__criteria(query) criteria[ "ids"] = braintree.transaction_search.TransactionSearch.ids.in_list( ids).to_param() response = Http().post("/transactions/advanced_search", {"search": criteria}) return [ Transaction(item) for item in ResourceCollection._extract_as_array( response["credit_card_transactions"], "transaction") ]
def cancel(subscription_id): """ Cancel a subscription by subscription_id::: result = braintree.Subscription.cancel("my_subscription_id") """ response = Http().put("/subscriptions/" + subscription_id + "/cancel") if "subscription" in response: return SuccessfulResult( {"subscription": Subscription(response["subscription"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def __fetch(query, ids): criteria = Subscription.__criteria(query) criteria[ "ids"] = braintree.subscription_search.SubscriptionSearch.ids.in_list( ids).to_param() response = Http().post("/subscriptions/advanced_search", {"search": criteria}) return [ Subscription(item) for item in ResourceCollection._extract_as_array( response["subscriptions"], "subscription") ]
def void(transaction_id): """ Voids an existing transaction. It expects a transaction_id. :: result = braintree.Transaction.void("my_transaction_id") """ response = Http().put("/transactions/" + transaction_id + "/void") if "transaction" in response: return SuccessfulResult( {"transaction": Transaction(response["transaction"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def retryCharge(subscription_id, amount=None): response = Http().post( "/transactions", { "transaction": { "amount": amount, "subscription_id": subscription_id, "type": Transaction.Type.Sale } }) if "transaction" in response: return SuccessfulResult( {"transaction": Transaction(response["transaction"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def find(credit_card_token): """ Find a credit card, given a credit_card_id. This does not return a result object. This will raise a :class:`NotFoundError <braintree.exceptions.not_found_error.NotFoundError>` if the provided credit_card_id is not found. :: credit_card = braintree.CreditCard.find("my_credit_card_id") """ try: response = Http().get("/payment_methods/" + credit_card_token) return CreditCard(response["credit_card"]) except NotFoundError: raise NotFoundError("payment method with token " + credit_card_token + " not found")
def update(credit_card_token, params={}): """ Update an existing CreditCard by credit_card_id. The params are similar to create:: result = braintree.CreditCard.update("my_credit_card_id", { "cardholder_name": "John Doe" }) """ Resource.verify_keys(params, CreditCard.update_signature()) response = Http().put("/payment_methods/" + credit_card_token, {"credit_card": params}) if "credit_card" in response: return SuccessfulResult({"credit_card": CreditCard(response["credit_card"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def find(customer_id, address_id): """ Find an address, given a customer_id and address_id. This does not return a result object. This will raise a :class:`NotFoundError <braintree.exceptions.not_found_error.NotFoundError>` if the provided customer_id/address_id are not found. :: address = braintree.Address.find("my_customer_id", "my_address_id") """ try: response = Http().get("/customers/" + customer_id + "/addresses/" + address_id) return Address(response["address"]) except NotFoundError: raise NotFoundError("address for customer " + customer_id + " with id " + address_id + " not found")
def find(subscription_id): """ Find a subscription given a subscription_id. This does not return a result object. This will raise a :class:`NotFoundError <braintree.exceptions.not_found_error.NotFoundError>` if the provided subscription_id is not found. :: subscription = braintree.Subscription.find("my_subscription_id") """ try: response = Http().get("/subscriptions/" + subscription_id) return Subscription(response["subscription"]) except NotFoundError: raise NotFoundError("subscription with id " + subscription_id + " not found")
def find(transaction_id): """ Find a transaction, given a transaction_id. This does not return a result object. This will raise a :class:`NotFoundError <braintree.exceptions.not_found_error.NotFoundError>` if the provided credit_card_id is not found. :: transaction = braintree.Transaction.find("my_transaction_id") """ try: response = Http().get("/transactions/" + transaction_id) return Transaction(response["transaction"]) except NotFoundError: raise NotFoundError("transaction with id " + transaction_id + " not found")
def update(customer_id, address_id, params={}): """ Update an existing Address. A customer_id and address_id are required:: result = braintree.Address.update("my_customer_id", "my_address_id", { "first_name": "John" }) """ Resource.verify_keys(params, Address.update_signature()) response = Http().put( "/customers/" + customer_id + "/addresses/" + address_id, {"address": params}) if "address" in response: return SuccessfulResult({"address": Address(response["address"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def create(params={}): """ Create a Subscription::: result = braintree.Subscription.create({ "payment_method_token": "my_payment_token", "plan_id": "some_plan_id", }) """ Resource.verify_keys(params, Subscription.create_signature()) response = Http().post("/subscriptions", {"subscription": params}) if "subscription" in response: return SuccessfulResult( {"subscription": Subscription(response["subscription"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def refund(transaction_id, amount=None): """ Refunds an existing transaction. It expects a transaction_id. :: result = braintree.Transaction.refund("my_transaction_id") """ response = Http().post("/transactions/" + transaction_id + "/refund", {"transaction": { "amount": amount }}) if "transaction" in response: return SuccessfulResult( {"transaction": Transaction(response["transaction"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def submit_for_settlement(transaction_id, amount=None): """ Submits an authorized transaction for settlement. :: result = braintree.Transaction.submit_for_settlement("my_transaction_id") """ response = Http().put( "/transactions/" + transaction_id + "/submit_for_settlement", {"transaction": { "amount": amount }}) if "transaction" in response: return SuccessfulResult( {"transaction": Transaction(response["transaction"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def update(subscription_id, params={}): """ Update an existing subscription by subscription_id. The params are similar to create:: result = braintree.Subscription.update("my_subscription_id", { "price": "9.99", }) """ Resource.verify_keys(params, Subscription.update_signature()) response = Http().put("/subscriptions/" + subscription_id, {"subscription": params}) if "subscription" in response: return SuccessfulResult( {"subscription": Subscription(response["subscription"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def search(query): """ Allows searching on subscriptions. There are two types of fields that are searchable: text and multiple value fields. Searchable text fields are: - plan_id - days_past_due Searchable multiple value fields are: - status For text fields, you can search using the following operators: ==, !=, starts_with, ends_with and contains. For mutiple value fields, you can search using the in_list operator. An example:: braintree.Subscription.search([ braintree.SubscriptionSearch.plan_id.starts_with("abc"), braintree.SubscriptionSearch.days_past_due == "30", braintree.SubscriptionSearch.status.in_list([braintree.Subscription.Status.PastDue]) ]) """ response = Http().post("/subscriptions/advanced_search_ids", {"search": Subscription.__criteria(query)}) return ResourceCollection(query, response, Subscription.__fetch)
def _post(url, params={}): response = Http().post(url, params) if "credit_card" in response: return SuccessfulResult({"credit_card": CreditCard(response["credit_card"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
def search(query): response = Http().post("/transactions/advanced_search_ids", {"search": Transaction.__criteria(query)}) return ResourceCollection(query, response, Transaction.__fetch)
def __fetch_existing_between(query, ids): criteria = {} criteria["ids"] = IdsSearch.ids.in_list(ids).to_param() response = Http().post("/payment_methods/all/expiring?" + query, {"search": criteria}) return [CreditCard(item) for item in ResourceCollection._extract_as_array(response["payment_methods"], "credit_card")]
def expired(): """ Return a collection of expired credit cards. """ response = Http().post("/payment_methods/all/expired_ids") return ResourceCollection(None, response, CreditCard.__fetch_expired)