def create(self, params={}): Resource.verify_keys(params, Subscription.create_signature()) response = self.config.http().post(self.config.base_merchant_path() + "/subscriptions", {"subscription": params}) if "subscription" in response: return SuccessfulResult({"subscription": Subscription(self.gateway, response["subscription"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def update(self, paypal_account_token, params={}): Resource.verify_keys(params, PayPalAccount.signature()) response = self.config.http().put("/payment_methods/paypal_account/" + paypal_account_token, {"paypal_account": params}) if "paypal_account" in response: return SuccessfulResult({"paypal_account": PayPalAccount(self.gateway, response["paypal_account"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def create(params): """ Creates a transaction. Amount and type are required. Also, a credit card, customer_id or payment_method_token is required. :: result = braintree.Transaction.sale({ "type": braintree.Transaction.Type.Sale, "amount": "100.00", "payment_method_token": "my_token" }) result = braintree.Transaction.sale({ "type": braintree.Transaction.Type.Sale, "amount": "100.00", "credit_card": { "number": "4111111111111111", "expiration_date": "12/2012" } }) result = braintree.Transaction.sale({ "type": braintree.Transaction.Type.Sale, "amount": "100.00", "customer_id": "my_customer_id" }) """ Resource.verify_keys(params, Transaction.create_signature()) return Transaction._post("/transactions", {"transaction": params})
def update(self, subscription_id, params={}): Resource.verify_keys(params, Subscription.update_signature()) response = self.config.http().put("/subscriptions/" + subscription_id, {"subscription": params}) if "subscription" in response: return SuccessfulResult({"subscription": Subscription(self.gateway, response["subscription"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def tr_data_for_create(self, tr_data, redirect_url): Resource.verify_keys(tr_data, [{ "credit_card": CreditCard.create_signature() }]) tr_data["kind"] = TransparentRedirect.Kind.CreatePaymentMethod return self.gateway.transparent_redirect.tr_data(tr_data, redirect_url)
def update(self, credit_card_token, params={}): Resource.verify_keys(params, CreditCard.update_signature()) response = self.config.http().put("/payment_methods/" + credit_card_token, {"credit_card": params}) if "credit_card" in response: return SuccessfulResult({"credit_card": CreditCard(self.gateway, response["credit_card"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def update(self, customer_id, params={}): Resource.verify_keys(params, Customer.update_signature()) response = self.config.http().put(self.config.base_merchant_path() + "/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 test_verify_keys_allows_raw_data(self): raw_string = str.encode("raw_string") assert isinstance(raw_string, TestHelper.raw_type) signature = [{"customer": [{"custom_fields": [raw_string]}]}] params = {"customer": {"custom_fields": {raw_string: raw_string}}} Resource.verify_keys(params, signature)
def create(self, params=None): if params is None: params = {} Resource.verify_keys(params, MerchantAccountGateway._create_signature()) return self._post("/merchant_accounts/create_via_api", {"merchant_account": params})
def test_verify_keys_allows_text(self): text_string = u"text_string" assert isinstance(text_string, TestHelper.text_type) signature = [{"customer": [{"custom_fields": [text_string]}]}] params = {"customer": {"custom_fields": {text_string: text_string}}} Resource.verify_keys(params, signature)
def generate(self, params=None): if params is None: params = {} if "options" in params and "customer_id" not in params: for option in [ "verify_card", "make_default", "fail_on_duplicate_payment_method" ]: if option in params["options"]: raise exceptions.InvalidSignatureError( "cannot specify %s without a customer_id" % option) if "version" not in params: params["version"] = 2 Resource.verify_keys(params, ClientToken.generate_signature()) params = {'client_token': params} response = self.config.http().post( self.config.base_merchant_path() + "/client_token", params) if "client_token" in response: return response["client_token"]["value"] else: raise ValueError(response["api_error_response"]["message"])
def tr_data_for_update(tr_data, redirect_url): """ Builds tr_data for CreditCard updating. """ Resource.verify_keys(tr_data, ["payment_method_token", {"credit_card": CreditCard.update_signature()}]) tr_data["kind"] = TransparentRedirect.Kind.UpdatePaymentMethod return TransparentRedirect.tr_data(tr_data, redirect_url)
def tr_data_for_credit(self, tr_data, redirect_url): if "transaction" not in tr_data: tr_data["transaction"] = {} tr_data["transaction"]["type"] = Transaction.Type.Credit Resource.verify_keys(tr_data, [{"transaction": Transaction.create_signature()}]) tr_data["kind"] = TransparentRedirect.Kind.CreateTransaction return self.gateway.transparent_redirect.tr_data(tr_data, redirect_url)
def create(self, payment_method_token, params={"payment_method_nonce": {}}): try: schema = [{ "payment_method_nonce": [ "merchant_account_id", "authentication_insight", { "authentication_insight_options": [ "amount", "recurring_customer_consent", "recurring_max_amount" ] } ] }] Resource.verify_keys(params, schema) response = self.config.http().post( self.config.base_merchant_path() + "/payment_methods/" + payment_method_token + "/nonces", params) if "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"]) else: payment_method_nonce = self._parse_payment_method_nonce( response) return SuccessfulResult( {"payment_method_nonce": payment_method_nonce}) except NotFoundError: raise NotFoundError("payment method with token " + repr(payment_method_token) + " not found")
def tr_data_for_sale(self, tr_data, redirect_url): if "transaction" not in tr_data: tr_data["transaction"] = {} tr_data["transaction"]["type"] = Transaction.Type.Sale Resource.verify_keys(tr_data, [{"transaction": Transaction.create_signature()}]) tr_data["kind"] = TransparentRedirect.Kind.CreateTransaction return self.gateway.transparent_redirect.tr_data(tr_data, redirect_url)
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 tr_data_for_create(tr_data, redirect_url): """ Builds tr_data for CreditCard creation. """ Resource.verify_keys(tr_data, [{"credit_card": CreditCard.create_signature()}]) tr_data["kind"] = TransparentRedirect.Kind.CreatePaymentMethod return TransparentRedirect.tr_data(tr_data, redirect_url)
def update_details(self, transaction_id, params={}): Resource.verify_keys(params, Transaction.update_details_signature()) response = self.config.http().put(self.config.base_merchant_path() + "/transactions/" + transaction_id + "/update_details", {"transaction": params}) if "transaction" in response: return SuccessfulResult({"transaction": Transaction(self.gateway, response["transaction"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
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 test_verify_keys_escapes_brackets_in_signature(self): signature = [ {"customer": [{"custom_fields": ["__any_key__"]}]} ] params = { "customer_id": "value", } Resource.verify_keys(params, signature)
def update(self, merchant_account_id, params=None): if params is None: params = {} Resource.verify_keys(params, MerchantAccountGateway._update_signature()) return self._put( "/merchant_accounts/%s/update_via_api" % merchant_account_id, {"merchant_account": params})
def test_verify_keys_works_with_array_param(self): signature = [ {"customer": ["one", "two"]} ] params = { "customer": { "one": "foo" } } Resource.verify_keys(params, signature)
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"])
def delete(self, payment_method_token, options={}): Resource.verify_keys(options, PaymentMethod.delete_signature()) query_param = "" if options: if 'revoke_all_grants' in options: options['revoke_all_grants'] = str(options['revoke_all_grants']).lower() query_param = "?" + urlencode(options) self.config.http().delete(self.config.base_merchant_path() + "/payment_methods/any/" + payment_method_token + query_param) return SuccessfulResult()
def submit_for_settlement(self, transaction_id, amount=None, params={}): Resource.verify_keys(params, Transaction.submit_for_settlement_signature()) transaction_params = {"amount": amount} transaction_params.update(params) response = self.config.http().put(self.config.base_merchant_path() + "/transactions/" + transaction_id + "/submit_for_settlement", {"transaction": transaction_params}) if "transaction" in response: return SuccessfulResult({"transaction": Transaction(self.gateway, response["transaction"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def test_verify_keys_raises_on_bad_array_param(self): signature = [ {"customer": ["one", "two"]} ] params = { "customer": { "invalid": "foo" } } Resource.verify_keys(params, signature)
def update_details(self, transaction_id, params=None): if params is None: params = {} Resource.verify_keys(params, Transaction.update_details_signature()) response = self.config.http().put(self.config.base_merchant_path() + "/transactions/" + transaction_id + "/update_details", {"transaction": params}) if "transaction" in response: return SuccessfulResult({"transaction": Transaction(self.gateway, response["transaction"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def update(self, credit_card_token, params={}): Resource.verify_keys(params, CreditCard.update_signature()) response = self.config.http().put( "/payment_methods/" + credit_card_token, {"credit_card": params}) if "credit_card" in response: return SuccessfulResult({ "credit_card": CreditCard(self.gateway, response["credit_card"]) }) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def test_verify_keys_works_with_arrays(self): signature = [{"add_ons": [{"update": ["existing_id", "quantity"]}]}] params = { "add_ons": { "update": [{ "existing_id": "foo", "quantity": 10 }] } } Resource.verify_keys(params, signature)
def generate(self, params): if params: Resource.verify_keys(params, ClientToken.generate_signature()) params = {'client_token': params} response = self.config.http().post("/client_token", params) if "client_token" in response: return response["client_token"]["value"] else: raise exceptions.ValueError(response["api_error_response"]["message"])
def test_verify_keys_allows_wildcard_keys(self): signature = [{"foo": [{"bar": ["__any_key__"]}]}] params = { "foo[bar][lower]": "lowercase", "foo[bar][UPPER]": "uppercase", "foo[bar][123]": "numeric", "foo[bar][under_scores]": "underscores", "foo[bar][dash-es]": "dashes", "foo[bar][ABC-abc_123]": "all together" } Resource.verify_keys(params, signature)
def tr_data_for_sale(tr_data, redirect_url): """ Builds tr_data for a Transaction of type Sale """ if "transaction" not in tr_data: tr_data["transaction"] = {} tr_data["transaction"]["type"] = Transaction.Type.Sale Resource.verify_keys(tr_data, [{"transaction": Transaction.create_signature()}]) tr_data["kind"] = TransparentRedirect.Kind.CreateTransaction return TransparentRedirect.tr_data(tr_data, redirect_url)
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 create(params={}): """ Create an CreditCard. A number and expiration_date are required. :: result = braintree.CreditCard.create({ "number": "4111111111111111", "expiration_date": "12/2012" }) """ Resource.verify_keys(params, CreditCard.create_signature()) return CreditCard._post("/payment_methods", {"credit_card": params})
def create(params={}): """ Create a Customer:: result = braintree.Customer.create({ "company": "Some company", "first_name": "John" }) """ Resource.verify_keys(params, Customer.create_signature()) return Customer._post("/customers", {"customer": params})
def generate(self, params): if params: Resource.verify_keys(params, ClientToken.generate_signature()) params = {'client_token': params} response = self.config.http().post( self.config.base_merchant_path() + "/client_token", params) if "client_token" in response: return response["client_token"]["value"] else: raise ValueError(response["api_error_response"]["message"])
def submit_for_partial_settlement(self, transaction_id, amount, params=None): if params is None: params = {} Resource.verify_keys(params, Transaction.submit_for_settlement_signature()) transaction_params = {"amount": amount} transaction_params.update(params) response = self.config.http().post(self.config.base_merchant_path() + "/transactions/" + transaction_id + "/submit_for_partial_settlement", {"transaction": transaction_params}) if "transaction" in response: return SuccessfulResult({"transaction": Transaction(self.gateway, response["transaction"])}) elif "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"])
def create(self, params={}): Resource.verify_keys(params, DocumentUpload.create_signature()) if "file" in params and not hasattr(params["file"], "read"): raise ValueError("file must be a file handle") response = self.config.http().post_multipart(self.config.base_merchant_path() + "/document_uploads", *self.__payload(params)) if "api_error_response" in response: return ErrorResult(self.gateway, response["api_error_response"]) else: return SuccessfulResult({"document_upload": DocumentUpload(self, response["document_upload"])})
def update(self, payment_method_token, params): Resource.verify_keys(params, PaymentMethod.update_signature()) try: if payment_method_token is None or payment_method_token.strip() == "": raise NotFoundError() return self._put( "/payment_methods/any/" + payment_method_token, {"payment_method": params} ) except NotFoundError: raise NotFoundError("payment method with token " + repr(payment_method_token) + " not found")
def test_verify_keys_allows_wildcard_keys(self): signature = [ {"foo": [{"bar": ["__any_key__"]}]} ] params = { "foo[bar][lower]": "lowercase", "foo[bar][UPPER]": "uppercase", "foo[bar][123]": "numeric", "foo[bar][under_scores]": "underscores", "foo[bar][dash-es]": "dashes", "foo[bar][ABC-abc_123]": "all together" } Resource.verify_keys(params, signature)
def tr_data_for_sale(tr_data, redirect_url): """ Builds tr_data for a Transaction of type Sale """ if "transaction" not in tr_data: tr_data["transaction"] = {} tr_data["transaction"]["type"] = Transaction.Type.Sale Resource.verify_keys(tr_data, [{ "transaction": Transaction.create_signature() }]) tr_data["kind"] = TransparentRedirect.Kind.CreateTransaction return TransparentRedirect.tr_data(tr_data, redirect_url)
def test_verify_keys_allows_raw_data(self): raw_string = str.encode("raw_string") assert isinstance(raw_string, TestHelper.raw_type) signature = [ {"customer": [{"custom_fields": [raw_string]}]} ] params = { "customer": { "custom_fields": { raw_string : raw_string } } } Resource.verify_keys(params, signature)
def update(customer_id, params={}): """ Update an existing Customer by customer_id. The params are similar to create:: result = braintree.Customer.update("my_customer_id", { "last_name": "Smith" }) """ Resource.verify_keys(params, Customer.update_signature()) response = Http().put("/customers/" + customer_id, {"customer": params}) if "customer" in response: return SuccessfulResult({"customer": Customer(response["customer"])}) elif "api_error_response" in response: return ErrorResult(response["api_error_response"])
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 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 test_verify_keys_allows_text(self): text_string = u"text_string" assert isinstance(text_string, TestHelper.text_type) signature = [ {"customer": [{"custom_fields": [text_string]}]} ] params = { "customer": { "custom_fields": { text_string : text_string } } } Resource.verify_keys(params, signature)
def test_verify_keys_raises_with_invalid_param_in_arrays(self): signature = [ {"add_ons": [{"update": ["existing_id", "quantity"]}]} ] params = { "add_ons": { "update": [ { "invalid": "foo", "quantity": 10 } ] } } Resource.verify_keys(params, signature)