Example #1
0
 def instance_url(self):
     self.id = util.utf8(self.id)
     self.charge = util.utf8(self.charge)
     base = Charge.class_url()
     cust_extn = urllib.quote_plus(self.charge)
     extn = urllib.quote_plus(self.id)
     return "%s/%s/refunds/%s" % (base, cust_extn, extn)
 def instance_url(self):
     token = util.utf8(self.id)
     schedule = util.utf8(self.schedule)
     base = SubscriptionSchedule.class_url()
     schedule_extn = quote_plus(schedule)
     extn = quote_plus(token)
     return "%s/%s/revisions/%s" % (base, schedule_extn, extn)
Example #3
0
 def instance_url(self):
     self.id = util.utf8(self.id)
     self.charge = util.utf8(self.transfer)
     base = Transfer.class_url()
     cust_extn = urllib.quote_plus(self.transfer)
     extn = urllib.quote_plus(self.id)
     return "%s/%s/reversals/%s" % (base, cust_extn, extn)
Example #4
0
 def instance_url(self):
     token = util.utf8(self.id)
     transfer = util.utf8(self.transfer)
     base = Transfer.class_url()
     cust_extn = urllib.quote_plus(transfer)
     extn = urllib.quote_plus(token)
     return "%s/%s/reversals/%s" % (base, cust_extn, extn)
Example #5
0
 def instance_url(self):
     self.id = util.utf8(self.id)
     self.fee = util.utf8(self.fee)
     base = ApplicationFee.class_url()
     cust_extn = urllib.quote_plus(self.fee)
     extn = urllib.quote_plus(self.id)
     return "%s/%s/refunds/%s" % (base, cust_extn, extn)
Example #6
0
 def instance_url(self):
     token = util.utf8(self.id)
     account = util.utf8(self.account)
     base = Account.class_url()
     acct_extn = quote_plus(account)
     extn = quote_plus(token)
     return "%s/%s/persons/%s" % (base, acct_extn, extn)
Example #7
0
 def _build_instance_url(cls, fee, sid):
     fee = util.utf8(fee)
     sid = util.utf8(sid)
     base = ApplicationFee.class_url()
     cust_extn = urllib.quote_plus(fee)
     extn = urllib.quote_plus(sid)
     return "%s/%s/refunds/%s" % (base, cust_extn, extn)
Example #8
0
    def instance_url(self):
        self.id = util.utf8(self.id)
        extn = urllib.quote_plus(self.id)
        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, "recipient"):
            recipient = util.utf8(self.recipient)

            base = Recipient.class_url()
            owner_extn = urllib.quote_plus(recipient)
            class_base = "cards"

        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 card_id %s is "
                "attached to a customer, recipient, or "
                "account." % self.id,
                "id",
            )

        return "%s/%s/%s/%s" % (base, owner_extn, class_base, extn)
Example #9
0
    def instance_url(self):
        token = util.utf8(self.id)
        extn = quote_plus(token)
        if hasattr(self, "customer"):
            customer = util.utf8(self.customer)

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

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

            base = Account.class_url()
            owner_extn = 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)
Example #10
0
    def instance_url(self):
        self.id = util.utf8(self.id)
        self.customer = util.utf8(self.customer)

        base = Customer.class_url()
        cust_extn = urllib.quote_plus(self.customer)
        extn = urllib.quote_plus(self.id)

        return "%s/%s/subscriptions/%s" % (base, cust_extn, extn)
Example #11
0
    def instance_url(self):
        token = util.utf8(self.id)
        extn = urllib.quote_plus(token)
        customer = util.utf8(self.customer)

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

        return "%s/%s/sources/%s" % (base, owner_extn, extn)
Example #12
0
    def _build_instance_url(cls, customer, sid):
        token = util.utf8(sid)
        extn = urllib.quote_plus(token)
        customer = util.utf8(customer)

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

        return "%s/%s/sources/%s" % (base, owner_extn, extn)
Example #13
0
    def instance_url(self):
        token = util.utf8(self.id)
        extn = urllib.parse.quote_plus(token)

        if (hasattr(self, 'customer')):
            customer = util.utf8(self.customer)
            base = Customer.class_url()
            cust_extn = urllib.parse.quote_plus(customer)
            return "%s/%s/sources/%s" % (base, cust_extn, extn)
        else:
            base = BitcoinReceiver.class_url()
            return "%s/%s" % (base, extn)
Example #14
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()
            cust_extn = urllib.quote_plus(self.customer)
            return "%s/%s/sources/%s" % (base, cust_extn, extn)
        else:
            base = BitcoinReceiver.class_url()
            return "%s/%s" % (base, extn)
Example #15
0
    def instance_url(self):
        token = util.utf8(self.id)
        extn = urllib.parse.quote_plus(token)

        if (hasattr(self, 'customer')):
            customer = util.utf8(self.customer)
            base = Customer.class_url()
            cust_extn = urllib.parse.quote_plus(customer)
            return "%s/%s/sources/%s" % (base, cust_extn, extn)
        else:
            base = BitcoinReceiver.class_url()
            return "%s/%s" % (base, extn)
Example #16
0
    def detach(self, **params):
        if hasattr(self, 'customer') and self.customer:
            extn = quote_plus(util.utf8(self.id))
            customer = util.utf8(self.customer)
            base = Customer.class_url()
            owner_extn = quote_plus(customer)
            url = "%s/%s/sources/%s" % (base, owner_extn, extn)

            self.refresh_from(self.request('delete', url, params))
            return self

        else:
            raise NotImplementedError(
                "This source object does not appear to be currently attached "
                "to a customer object.")
Example #17
0
    def verify(self, idempotency_key=None, **params):
        headers = populate_headers(idempotency_key)
        extn = urllib.quote_plus(util.utf8(self.id))
        if (hasattr(self, 'customer')):
            customer = util.utf8(self.customer)
            base = Customer.class_url()
            owner_extn = urllib.quote_plus(customer)
            class_base = "bank_accounts"
            url = "%s/%s/%s/%s/verify" % (base, owner_extn, class_base, extn)
            self.refresh_from(self.request('post', url, params, headers))
        else:
            raise NotImplementedError(
                "Can't verify bank account not attached to customer")

        return self
Example #18
0
    async def retrieve(self, id, **params):
        base = self.get("url")
        id = util.utf8(id)
        extn = quote_plus(id)
        url = "%s/%s" % (base, extn)

        return await self.request("get", url, params)
Example #19
0
    def request(self, method, url, headers, post_data=None):
        s = util.StringIO.StringIO()
        rheaders = util.StringIO.StringIO()
        curl = pycurl.Curl()

        if method == 'get':
            curl.setopt(pycurl.HTTPGET, 1)
        elif method == 'post':
            curl.setopt(pycurl.POST, 1)
            curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        curl.setopt(pycurl.URL, util.utf8(url))

        curl.setopt(pycurl.WRITEFUNCTION, s.write)
        curl.setopt(pycurl.HEADERFUNCTION, rheaders.write)
        curl.setopt(pycurl.NOSIGNAL, 1)
        curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        curl.setopt(pycurl.TIMEOUT, 80)
        curl.setopt(pycurl.HTTPHEADER,
                    ['%s: %s' % (k, v) for k, v in headers.iteritems()])
        if self._verify_ssl_certs:
            curl.setopt(
                pycurl.CAINFO,
                os.path.join(os.path.dirname(__file__),
                             'data/ca-certificates.crt'))
        else:
            curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            curl.perform()
        except pycurl.error, e:
            self._handle_request_error(e)
Example #20
0
    def retrieve(self, id, **params):
        base = self.get("url")
        id = util.utf8(id)
        extn = quote_plus(id)
        url = "%s/%s" % (base, extn)

        return self.request("get", url, params)
Example #21
0
 def _build_instance_url(cls, sid):
     if not sid:
         return "/v1/account"
     sid = util.utf8(sid)
     base = cls.class_url()
     extn = urllib.quote_plus(sid)
     return "%s/%s" % (base, extn)
Example #22
0
    def detach(self, idempotency_key=None, **params):
        if hasattr(self, 'customer') and self.customer:
            extn = quote_plus(util.utf8(self.id))
            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 NotImplementedError(
                "This source object does not appear to be currently attached "
                "to a customer object.")
Example #23
0
    def retrieve(self, id, **params):
        base = self.get('url')
        id = util.utf8(id)
        extn = urllib.quote_plus(id)
        url = "%s/%s" % (base, extn)

        return self.request('get', url, params)
Example #24
0
    def retrieve(self, id, **params):
        base = self.get('url')
        id = util.utf8(id)
        extn = urllib.quote_plus(id)
        url = "%s/%s" % (base, extn)

        return self.request('get', url, params)
Example #25
0
    def request(self, method, url, headers, post_data=None):
        s = util.StringIO.StringIO()
        rheaders = util.StringIO.StringIO()
        curl = pycurl.Curl()

        if method == 'get':
            curl.setopt(pycurl.HTTPGET, 1)
        elif method == 'post':
            curl.setopt(pycurl.POST, 1)
            curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        curl.setopt(pycurl.URL, util.utf8(url))

        curl.setopt(pycurl.WRITEFUNCTION, s.write)
        curl.setopt(pycurl.HEADERFUNCTION, rheaders.write)
        curl.setopt(pycurl.NOSIGNAL, 1)
        curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        curl.setopt(pycurl.TIMEOUT, 80)
        curl.setopt(pycurl.HTTPHEADER, ['%s: %s' % (k, v)
                    for k, v in headers.iteritems()])
        if self._verify_ssl_certs:
            curl.setopt(pycurl.CAINFO, os.path.join(
                os.path.dirname(__file__), 'data/ca-certificates.crt'))
        else:
            curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            curl.perform()
        except pycurl.error, e:
            self._handle_request_error(e)
Example #26
0
 def custom_method_request_stream(cls, sid, **params):
     url = "%s/%s/%s" % (
         cls.class_url(),
         quote_plus(util.utf8(sid)),
         http_path,
     )
     return cls._static_request_stream(http_verb, url, **params)
Example #27
0
 def _build_instance_url(cls, sid):
     if not sid:
         return "/v1/account"
     sid = util.utf8(sid)
     base = cls.class_url()
     extn = urllib.quote_plus(sid)
     return "%s/%s" % (base, extn)
Example #28
0
    def delete(self, **params):
        if hasattr(self, 'customer') and self.customer:
            extn = urllib.quote_plus(util.utf8(self.id))
            customer = util.utf8(self.customer)
            base = Customer.class_url()
            owner_extn = urllib.quote_plus(customer)
            url = "%s/%s/sources/%s" % (base, owner_extn, extn)

            self.refresh_from(self.request('delete', url, params))
            return self

        else:
            raise NotImplementedError(
                'Source objects cannot be deleted, they can only be detached '
                'from customer objects. This source object does not appear to '
                'be currently attached to a customer object.')
Example #29
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)

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

        return "%s/%s/bank_accounts/%s" % (base, owner_extn, extn)
Example #30
0
 def instance_url(self):
     id = self.get('id')
     if not id:
         return "/v1/account"
     id = util.utf8(id)
     base = self.class_url()
     extn = urllib.quote_plus(id)
     return "%s/%s" % (base, extn)
Example #31
0
 def instance_url(self):
     id = self.get("id")
     if not id:
         return "/v1/account"
     id = util.utf8(id)
     base = self.class_url()
     extn = urllib.quote_plus(id)
     return "%s/%s" % (base, extn)
Example #32
0
    def request(self, method, url, headers, post_data=None):
        b = util.io.BytesIO()
        rheaders = util.io.BytesIO()

        # Pycurl's design is a little weird: although we set per-request
        # options on this object, it's also capable of maintaining established
        # connections. Here we call reset() between uses to make sure it's in a
        # pristine state, but notably reset() doesn't reset connections, so we
        # still get to take advantage of those by virtue of re-using the same
        # object.
        self._curl.reset()

        proxy = self._get_proxy(url)
        if proxy:
            if proxy.hostname:
                self._curl.setopt(pycurl.PROXY, proxy.hostname)
            if proxy.port:
                self._curl.setopt(pycurl.PROXYPORT, proxy.port)
            if proxy.username or proxy.password:
                self._curl.setopt(
                    pycurl.PROXYUSERPWD,
                    "%s:%s" % (proxy.username, proxy.password),
                )

        if method == "get":
            self._curl.setopt(pycurl.HTTPGET, 1)
        elif method == "post":
            self._curl.setopt(pycurl.POST, 1)
            self._curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            self._curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        self._curl.setopt(pycurl.URL, util.utf8(url))

        self._curl.setopt(pycurl.WRITEFUNCTION, b.write)
        self._curl.setopt(pycurl.HEADERFUNCTION, rheaders.write)
        self._curl.setopt(pycurl.NOSIGNAL, 1)
        self._curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        self._curl.setopt(pycurl.TIMEOUT, 80)
        self._curl.setopt(
            pycurl.HTTPHEADER,
            ["%s: %s" % (k, v) for k, v in six.iteritems(dict(headers))],
        )
        if self._verify_ssl_certs:
            self._curl.setopt(pycurl.CAINFO, stripe.ca_bundle_path)
        else:
            self._curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            self._curl.perform()
        except pycurl.error as e:
            self._handle_request_error(e)
        rbody = b.getvalue().decode("utf-8")
        rcode = self._curl.getinfo(pycurl.RESPONSE_CODE)
        headers = self.parse_headers(rheaders.getvalue().decode("utf-8"))

        return rbody, rcode, headers
Example #33
0
    def request(self, method, url, headers, post_data=None):
        b = util.io.BytesIO()
        rheaders = util.io.BytesIO()

        # Pycurl's design is a little weird: although we set per-request
        # options on this object, it's also capable of maintaining established
        # connections. Here we call reset() between uses to make sure it's in a
        # pristine state, but notably reset() doesn't reset connections, so we
        # still get to take advantage of those by virtue of re-using the same
        # object.
        self._curl.reset()

        proxy = self._get_proxy(url)
        if proxy:
            if proxy.hostname:
                self._curl.setopt(pycurl.PROXY, proxy.hostname)
            if proxy.port:
                self._curl.setopt(pycurl.PROXYPORT, proxy.port)
            if proxy.username or proxy.password:
                self._curl.setopt(
                    pycurl.PROXYUSERPWD,
                    "%s:%s" % (proxy.username, proxy.password),
                )

        if method == "get":
            self._curl.setopt(pycurl.HTTPGET, 1)
        elif method == "post":
            self._curl.setopt(pycurl.POST, 1)
            self._curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            self._curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        self._curl.setopt(pycurl.URL, util.utf8(url))

        self._curl.setopt(pycurl.WRITEFUNCTION, b.write)
        self._curl.setopt(pycurl.HEADERFUNCTION, rheaders.write)
        self._curl.setopt(pycurl.NOSIGNAL, 1)
        self._curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        self._curl.setopt(pycurl.TIMEOUT, 80)
        self._curl.setopt(
            pycurl.HTTPHEADER,
            ["%s: %s" % (k, v) for k, v in six.iteritems(dict(headers))],
        )
        if self._verify_ssl_certs:
            self._curl.setopt(pycurl.CAINFO, stripe.ca_bundle_path)
        else:
            self._curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            self._curl.perform()
        except pycurl.error as e:
            self._handle_request_error(e)
        rbody = b.getvalue().decode("utf-8")
        rcode = self._curl.getinfo(pycurl.RESPONSE_CODE)
        headers = self.parse_headers(rheaders.getvalue().decode("utf-8"))

        return rbody, rcode, headers
Example #34
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')
Example #35
0
def _api_encode(data):
    for key, value in data.iteritems():
        key = util.utf8(key)
        if value is None:
            continue
        elif hasattr(value, 'stripe_id'):
            yield (key, value.stripe_id)
        elif isinstance(value, list) or isinstance(value, tuple):
            for subvalue in value:
                yield ("%s[]" % (key,), util.utf8(subvalue))
        elif isinstance(value, dict):
            subdict = dict(('%s[%s]' % (key, subkey), subvalue) for
                           subkey, subvalue in value.iteritems())
            for subkey, subvalue in _api_encode(subdict):
                yield (subkey, subvalue)
        elif isinstance(value, datetime.datetime):
            yield (key, _encode_datetime(value))
        else:
            yield (key, util.utf8(value))
Example #36
0
def _api_encode(data):
    for key, value in data.items():
        key = util.utf8(key)
        if value is None:
            continue
        elif hasattr(value, 'stripe_id'):
            yield (key, value.stripe_id)
        elif isinstance(value, list) or isinstance(value, tuple):
            for subvalue in value:
                yield ("%s[]" % (key,), util.utf8(subvalue))
        elif isinstance(value, dict):
            subdict = dict(('%s[%s]' % (key, subkey), subvalue) for
                           subkey, subvalue in value.items())
            for subkey, subvalue in _api_encode(subdict):
                yield (subkey, subvalue)
        elif isinstance(value, datetime.datetime):
            yield (key, _encode_datetime(value))
        else:
            yield (key, util.utf8(value))
Example #37
0
 def instance_url(self):
     id = self.get('id')
     if not id:
         raise error.InvalidRequestError(
             'Could not determine which URL to request: %s instance '
             'has invalid ID: %r' % (type(self).__name__, id), 'id')
     id = util.utf8(id)
     base = self.class_url()
     extn = urllib.quote_plus(id)
     return "%s/%s" % (base, extn)
Example #38
0
 def instance_url(self):
     id = self.get('id')
     if not id:
         raise error.InvalidRequestError(
             'Could not determine which URL to request: %s instance '
             'has invalid ID: %r' % (type(self).__name__, id), 'id')
     id = util.utf8(id)
     base = self.class_url()
     extn = urllib.quote_plus(id)
     return "%s/%s" % (base, extn)
Example #39
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 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)
Example #41
0
 def retrieve(self,
              id,
              api_key=None,
              stripe_version=None,
              stripe_account=None,
              **params):
     url = "%s/%s" % (self.get("url"), quote_plus(util.utf8(id)))
     return self._request("get",
                          url,
                          api_key=api_key,
                          stripe_version=stripe_version,
                          stripe_account=stripe_account,
                          **params)
Example #42
0
    def instance_url(self):
        id = self.get('id')

        if not isinstance(id, basestring):
            raise error.InvalidRequestError(
                'Could not determine which URL to request: %s instance '
                'has invalid ID: %r, %s. ID should be of type `str` (or'
                ' `unicode`)' % (type(self).__name__, id, type(id)), 'id')

        id = util.utf8(id)
        base = self.class_url()
        extn = urllib.quote_plus(id)
        return "%s/%s" % (base, extn)
Example #43
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)
Example #44
0
    def instance_url(self):
        id = self.get('id')

        if not isinstance(id, six.string_types):
            raise error.InvalidRequestError(
                'Could not determine which URL to request: %s instance '
                'has invalid ID: %r, %s. ID should be of type `str` (or'
                ' `unicode`)' % (type(self).__name__, id, type(id)), 'id')

        id = util.utf8(id)
        base = self.class_url()
        extn = quote_plus(id)
        return "%s/%s" % (base, extn)
def _api_encode(data):
    for key, value in six.iteritems(data):
        key = util.utf8(key)
        if value is None:
            continue
        elif hasattr(value, "stripe_id"):
            yield (key, value.stripe_id)
        elif isinstance(value, list) or isinstance(value, tuple):
            for i, sv in enumerate(value):
                if isinstance(sv, dict):
                    subdict = _encode_nested_dict("%s[%d]" % (key, i), sv)
                    for k, v in _api_encode(subdict):
                        yield (k, v)
                else:
                    yield ("%s[%d]" % (key, i), util.utf8(sv))
        elif isinstance(value, dict):
            subdict = _encode_nested_dict(key, value)
            for subkey, subvalue in _api_encode(subdict):
                yield (subkey, subvalue)
        elif isinstance(value, datetime.datetime):
            yield (key, _encode_datetime(value))
        else:
            yield (key, util.utf8(value))
Example #46
0
    def instance_url(self):
        self.id = util.utf8(self.id)
        extn = urllib.quote_plus(self.id)
        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." % self.id, 'id')

        return "%s/%s/%s/%s" % (base, owner_extn, class_base, extn)
Example #47
0
        def custom_method_request(cls, sid, **params):
            url = "%s/%s/%s" % (
                cls.class_url(),
                quote_plus(util.utf8(sid)),
                http_path,
            )
            obj = cls._static_request(http_verb, url, params=params)

            # For list objects, we have to attach the parameters so that they
            # can be referenced in auto-pagination and ensure consistency.
            if "object" in obj and obj.object == "list":
                obj._retrieve_params = params

            return obj
Example #48
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)
Example #49
0
def _api_encode(data):
    for key, value in six.iteritems(data):
        key = util.utf8(key)
        if value is None:
            continue
        elif hasattr(value, "stripe_id"):
            yield (key, value.stripe_id)
        elif isinstance(value, list) or isinstance(value, tuple):
            for i, sv in enumerate(value):
                if isinstance(sv, dict):
                    subdict = _encode_nested_dict("%s[%d]" % (key, i), sv)
                    for k, v in _api_encode(subdict):
                        yield (k, v)
                else:
                    yield ("%s[%d]" % (key, i), util.utf8(sv))
        elif isinstance(value, dict):
            subdict = _encode_nested_dict(key, value)
            for subkey, subvalue in _api_encode(subdict):
                yield (subkey, subvalue)
        elif isinstance(value, datetime.datetime):
            yield (key, _encode_datetime(value))
        else:
            yield (key, util.utf8(value))
Example #50
0
    def request(self, method, url, headers, post_data=None):
        s = util.StringIO.StringIO()
        rheaders = util.StringIO.StringIO()
        curl = pycurl.Curl()

        proxy = self._get_proxy(url)
        if proxy:
            if proxy.hostname:
                curl.setopt(pycurl.PROXY, proxy.hostname)
            if proxy.port:
                curl.setopt(pycurl.PROXYPORT, proxy.port)
            if proxy.username or proxy.password:
                curl.setopt(pycurl.PROXYUSERPWD,
                            "%s:%s" % (proxy.username, proxy.password))

        if method == 'get':
            curl.setopt(pycurl.HTTPGET, 1)
        elif method == 'post':
            curl.setopt(pycurl.POST, 1)
            curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        curl.setopt(pycurl.URL, util.utf8(url))

        curl.setopt(pycurl.WRITEFUNCTION, s.write)
        curl.setopt(pycurl.HEADERFUNCTION, rheaders.write)
        curl.setopt(pycurl.NOSIGNAL, 1)
        curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        curl.setopt(pycurl.TIMEOUT, 80)
        curl.setopt(pycurl.HTTPHEADER,
                    ['%s: %s' % (k, v) for k, v in headers.items()])
        if self._verify_ssl_certs:
            curl.setopt(
                pycurl.CAINFO,
                os.path.join(os.path.dirname(__file__),
                             'data/ca-certificates.crt'))
        else:
            curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            curl.perform()
        except pycurl.error as e:
            self._handle_request_error(e)
        rbody = s.getvalue()
        rcode = curl.getinfo(pycurl.RESPONSE_CODE)

        return rbody, rcode, self.parse_headers(rheaders.getvalue())
Example #51
0
    def instance_url(self):
        id = self.get("id")

        if not isinstance(id, six.string_types):
            raise error.InvalidRequestError(
                "Could not determine which URL to request: %s instance "
                "has invalid ID: %r, %s. ID should be of type `str` (or"
                " `unicode`)" % (type(self).__name__, id, type(id)),
                "id",
            )

        id = util.utf8(id)
        base = self.class_url()
        extn = quote_plus(id)
        return "%s/%s" % (base, extn)
 def retrieve(
     self,
     id,
     api_key=None,
     stripe_version=None,
     stripe_account=None,
     **params
 ):
     requestor = api_requestor.APIRequestor(
         api_key, api_version=stripe_version, account=stripe_account
     )
     url = "%s/%s" % (self.get("url"), quote_plus(util.utf8(id)))
     response, api_key = requestor.request("get", url, params)
     return util.convert_to_stripe_object(
         response, api_key, stripe_version, stripe_account
     )
Example #53
0
 def _cls_pdf(cls,
              sid,
              api_key=None,
              idempotency_key=None,
              stripe_version=None,
              stripe_account=None,
              **params):
     url = "%s/%s/%s" % (
         cls.class_url(),
         quote_plus(util.utf8(sid)),
         "pdf",
     )
     requestor = api_requestor.APIRequestor(
         api_key,
         api_base=stripe.upload_api_base,
         api_version=stripe_version,
         account=stripe_account,
     )
     headers = util.populate_headers(idempotency_key)
     response, _ = requestor.request_stream("get", url, params, headers)
     return response
Example #54
0
 def modify_source(cls, sid, source_id, **params):
     url = "%s/%s/sources/%s" % (cls.class_url(),
                                 urllib.quote_plus(util.utf8(sid)),
                                 urllib.quote_plus(util.utf8(source_id)))
     return cls._modify(url, **params)
Example #55
0
 def modify_external_account(cls, sid, external_account_id, **params):
     url = "%s/%s/external_accounts/%s" % (
         cls.class_url(), urllib.quote_plus(util.utf8(sid)),
         urllib.quote_plus(util.utf8(external_account_id)))
     return cls._modify(url, **params)
 def remove(cls, sid, **params):
     url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid)))
     return cls._remove(url, **params)
 def modify(cls, sid, **params):
     url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid)))
     return cls._static_request("post", url, **params)
Example #58
0
 def instance_url(self):
     customer = util.utf8(self.customer)
     base = Customer.class_url()
     cust_extn = quote_plus(customer)
     return "%s/%s/cash_balance" % (base, cust_extn)
Example #59
0
 def _cls_delete(cls, sid, **params):
     url = "%s/%s" % (cls.class_url(), quote_plus(util.utf8(sid)))
     return cls._static_request("delete", url, params=params)
 def modify(cls, sid, **params):
     url = "%s/%s" % (cls.class_url(),
                      urllib.parse.quote_plus(util.utf8(sid)))
     return cls._modify(url, **params)