Ejemplo n.º 1
0
    def detach(self, idempotency_key=None, **params):
        token = util.utf8(self.id)

        if hasattr(self, "customer") and self.customer:
            extn = quote_plus(token)
            customer = util.utf8(self.customer)
            base = Customer.class_url()
            owner_extn = quote_plus(customer)
            url = "%s/%s/sources/%s" % (base, owner_extn, extn)
            headers = util.populate_headers(idempotency_key)

            self.refresh_from(self.request("delete", url, params, headers))
            return self

        else:
            raise error.InvalidRequestError(
                "Source %s does not appear to be currently attached "
                "to a customer object." % token,
                "id",
            )
    def handle_api_error(self, rbody, rcode, resp):
        try:
            err = resp['error']
        except (KeyError, TypeError):
            raise error.APIError(
                "Invalid response object from API: %r (HTTP response code "
                "was %d)" % (rbody, rcode), rbody, rcode, resp)

        if rcode in [400, 404]:
            raise error.InvalidRequestError(err.get('message'),
                                            err.get('param'), rbody, rcode,
                                            resp)
        elif rcode == 401:
            raise error.AuthenticationError(err.get('message'), rbody, rcode,
                                            resp)
        elif rcode == 402:
            raise error.CardError(err.get('message'), err.get('param'),
                                  err.get('code'), rbody, rcode, resp)
        else:
            raise error.APIError(err.get('message'), rbody, rcode, resp)
Ejemplo n.º 3
0
    def instance_url(self):
        self.id = util.utf8(self.id)
        extn = urllib.quote_plus(self.id)
        if (hasattr(self, 'customer')):
            self.customer = util.utf8(self.customer)

            base = Customer.class_url()
            owner_extn = urllib.quote_plus(self.customer)

        elif (hasattr(self, 'recipient')):
            self.recipient = util.utf8(self.recipient)

            base = Recipient.class_url()
            owner_extn = urllib.quote_plus(self.recipient)

        else:
            raise error.InvalidRequestError(
                "Could not determine whether card_id %s is "
                "attached to a customer "
                "or a recipient." % self.id, 'id')

        return "%s/%s/cards/%s" % (base, owner_extn, extn)
Ejemplo n.º 4
0
    def instance_url(self):
        token = util.utf8(self.id)
        extn = urllib.quote_plus(token)
        if (hasattr(self, 'customer')):
            customer = util.utf8(self.customer)

            base = Customer.class_url()
            owner_extn = urllib.quote_plus(customer)
            class_base = "sources"

        elif (hasattr(self, 'account')):
            account = util.utf8(self.account)

            base = Account.class_url()
            owner_extn = urllib.quote_plus(account)
            class_base = "external_accounts"

        else:
            raise error.InvalidRequestError(
                "Could not determine whether bank_account_id %s is "
                "attached to a customer or an account." % token, 'id')

        return "%s/%s/%s/%s" % (base, owner_extn, class_base, extn)
Ejemplo n.º 5
0
    def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
        util.log_info(
            'Stripe API error received',
            error_code=error_data.get('code'),
            error_type=error_data.get('type'),
            error_message=error_data.get('message'),
            error_param=error_data.get('param'),
        )

        # Rate limits were previously coded as 400's with code 'rate_limit'
        if rcode == 429 or (rcode == 400
                            and error_data.get('code') == 'rate_limit'):
            return error.RateLimitError(error_data.get('message'), rbody,
                                        rcode, resp, rheaders)
        elif rcode in [400, 404]:
            if error_data.get('type') == "idempotency_error":
                return error.IdempotencyError(error_data.get('message'), rbody,
                                              rcode, resp, rheaders)
            else:
                return error.InvalidRequestError(error_data.get('message'),
                                                 error_data.get('param'),
                                                 error_data.get('code'), rbody,
                                                 rcode, resp, rheaders)
        elif rcode == 401:
            return error.AuthenticationError(error_data.get('message'), rbody,
                                             rcode, resp, rheaders)
        elif rcode == 402:
            return error.CardError(error_data.get('message'),
                                   error_data.get('param'),
                                   error_data.get('code'), rbody, rcode, resp,
                                   rheaders)
        elif rcode == 403:
            return error.PermissionError(error_data.get('message'), rbody,
                                         rcode, resp, rheaders)
        else:
            return error.APIError(error_data.get('message'), rbody, rcode,
                                  resp, rheaders)