How to use the localstripe.resources.PaymentMethod._api_retrieve function in localstripe

To help you get started, we’ve selected a few localstripe examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adrienverge / localstripe / localstripe / resources.py View on Github external
def _on_recurring_payment_failure(self, invoice):
        # If source is SEPA, any payment failure at creation or upgrade cancels
        # the subscription:
        if (invoice.charge and PaymentMethod._api_retrieve(
                invoice.charge.payment_method).type == 'sepa_debit'):
            return Subscription._api_delete(self.id)

        self.status = 'past_due'
github adrienverge / localstripe / localstripe / resources.py View on Github external
def _get_default_payment_method_or_source(self):
        if self.invoice_settings.get('default_payment_method'):
            return PaymentMethod._api_retrieve(
                self.invoice_settings['default_payment_method'])
        elif self.default_source:
            return [s for s in self.sources._list
                    if s.id == self.default_source][0]
github adrienverge / localstripe / localstripe / resources.py View on Github external
assert type(customer) is str and customer.startswith('cus_')
            if source is not None:
                assert type(source) is str
                assert (source.startswith('pm_') or source.startswith('src_')
                        or source.startswith('card_'))
            assert type(capture) is bool
        except AssertionError:
            raise UserError(400, 'Bad request')

        if source is None:
            customer_obj = Customer._api_retrieve(customer)
            source = customer_obj._get_default_payment_method_or_source()
            if source is None:
                raise UserError(404, 'This customer has no payment method')
        else:
            source = PaymentMethod._api_retrieve(source)

        if customer is None:
            customer = source.customer

        # All exceptions must be raised before this point.
        super().__init__()

        self._authorized = not source._charging_is_declined()

        self.amount = amount
        self.currency = currency
        self.customer = customer
        self.description = description
        self.invoice = None
        self.metadata = metadata or {}
        self.status = 'pending'
github adrienverge / localstripe / localstripe / resources.py View on Github external
if payment_method is not None:
            raise UserError(500, 'Not implemented')

        try:
            assert type(id) is str and id.startswith('pi_')
        except AssertionError:
            raise UserError(400, 'Bad request')

        obj = cls._api_retrieve(id)

        if obj.status != 'requires_confirmation':
            raise UserError(400, 'Bad request')

        obj._authentication_failed = False
        payment_method = PaymentMethod._api_retrieve(obj.payment_method)
        if payment_method._requires_authentication():
            obj.next_action = {
                'type': 'use_stripe_sdk',
                'use_stripe_sdk': {'type': 'three_d_secure_redirect',
                                   'stripe_js': ''},
            }
        else:
            obj._trigger_payment()

        return obj
github adrienverge / localstripe / localstripe / resources.py View on Github external
assert type(currency) is str and currency
            if customer is not None:
                assert type(customer) is str and customer.startswith('cus_')
            if payment_method is not None:
                assert type(payment_method) is str
                assert (payment_method.startswith('pm_') or
                        payment_method.startswith('src_') or
                        payment_method.startswith('card_'))
        except AssertionError:
            raise UserError(400, 'Bad request')

        if customer:
            Customer._api_retrieve(customer)  # to return 404 if not existant
        if payment_method:
            # return 404 if not existant
            PaymentMethod._api_retrieve(payment_method)

        # All exceptions must be raised before this point.
        super().__init__()

        self.amount = amount
        self.currency = currency
        self.charges = List('/v1/charges?payment_intent=' + self.id)
        self.client_secret = self.id + '_secret_' + random_id(16)
        self.customer = customer
        self.payment_method = payment_method
        self.metadata = metadata or {}
        self.invoice = None
        self.next_action = None

        self._canceled = False
        self._authentication_failed = False
github adrienverge / localstripe / localstripe / resources.py View on Github external
def _trigger_payment(self, on_success=None, on_failure_now=None,
                         on_failure_later=None):
        pm = PaymentMethod._api_retrieve(self.payment_method)
        async_payment = pm.type == 'sepa_debit'

        if async_payment:
            if not self._authorized:
                async def callback():
                    await asyncio.sleep(0.5)
                    self.status = 'failed'
                    if on_failure_later:
                        on_failure_later()
            else:
                async def callback():
                    await asyncio.sleep(0.5)
                    self.status = 'succeeded'
                    if on_success:
                        on_success()
            asyncio.ensure_future(callback())