How to use the localstripe.resources.Charge 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 / server.py View on Github external
if 'source_id' in request.match_info:
            data['source_id'] = request.match_info['source_id']
        if 'subscription_id' in request.match_info:
            data['subscription_id'] = request.match_info['subscription_id']
        expand = data.pop('expand', None)
        return json_response(func(**data)._export(expand=expand))
    return f


# Extra routes must be added *before* regular routes, because otherwise
# `/invoices/upcoming` would fall into `/invoices/{id}`.
for method, url, func in extra_apis:
    app.router.add_route(method, url, api_extra(func, url))


for cls in (Charge, Coupon, Customer, Event, Invoice, InvoiceItem,
            PaymentIntent, PaymentMethod, Plan, Product, Refund, SetupIntent,
            Source, Subscription, SubscriptionItem, TaxRate, Token):
    for method, url, func in (
            ('POST', '/v1/' + cls.object + 's', api_create),
            ('GET', '/v1/' + cls.object + 's/{id}', api_retrieve),
            ('POST', '/v1/' + cls.object + 's/{id}', api_update),
            ('DELETE', '/v1/' + cls.object + 's/{id}', api_delete),
            ('GET', '/v1/' + cls.object + 's', api_list_all)):
        app.router.add_route(method, url, func(cls, url))


def localstripe_js(request):
    path = os.path.dirname(os.path.realpath(__file__)) + '/localstripe-v3.js'
    with open(path) as f:
        return web.Response(text=f.read(),
                            content_type='application/javascript')
github adrienverge / localstripe / localstripe / resources.py View on Github external
def on_success():
            if self.invoice:
                invoice = Invoice._api_retrieve(self.invoice)
                invoice._on_payment_success()

        def on_failure_now():
            if self.invoice:
                invoice = Invoice._api_retrieve(self.invoice)
                invoice._on_payment_failure_now()

        def on_failure_later():
            if self.invoice:
                invoice = Invoice._api_retrieve(self.invoice)
                invoice._on_payment_failure_later()

        charge = Charge(amount=self.amount,
                        currency=self.currency,
                        customer=self.customer,
                        source=self.payment_method)
        self.charges._list.append(charge)
        charge._trigger_payment(on_success, on_failure_now, on_failure_later)
github adrienverge / localstripe / localstripe / resources.py View on Github external
list(created.keys())[0] in ('gt', 'gte', 'lt', 'lte')
                    date = try_convert_to_int(list(created.values())[0])
                elif type(created) is str:
                    date = try_convert_to_int(created)
                assert type(date) is int and date > 1500000000
        except AssertionError:
            raise UserError(400, 'Bad request')

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

        if created:
            if type(created) is str or not created.get('gt'):
                raise UserError(500, 'Not implemented')

        li = super(Charge, cls)._api_list_all(url, limit=limit)
        if customer:
            li._list = [c for c in li._list if c.customer == customer]
        if created and created.get('gt'):
            li._list = [c for c in li._list
                        if c.created > try_convert_to_int(created['gt'])]
        return li
github adrienverge / localstripe / localstripe / resources.py View on Github external
if created:
            if type(created) is str or not created.get('gt'):
                raise UserError(500, 'Not implemented')

        li = super(Charge, cls)._api_list_all(url, limit=limit)
        if customer:
            li._list = [c for c in li._list if c.customer == customer]
        if created and created.get('gt'):
            li._list = [c for c in li._list
                        if c.created > try_convert_to_int(created['gt'])]
        return li


extra_apis.append((
    ('POST', '/v1/charges/{id}/capture', Charge._api_capture)))


class Coupon(StripeObject):
    object = 'coupon'

    def __init__(self, id=None, duration=None, amount_off=None,
                 percent_off=None, currency=None, metadata=None,
                 duration_in_months=None, **kwargs):
        if kwargs:
            raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))

        amount_off = try_convert_to_int(amount_off)
        percent_off = try_convert_to_float(percent_off)
        duration_in_months = try_convert_to_int(duration_in_months)
        try:
            assert type(id) is str and id
github adrienverge / localstripe / localstripe / resources.py View on Github external
def __init__(self, charge=None, amount=None, metadata=None, **kwargs):
        if kwargs:
            raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys()))

        amount = try_convert_to_int(amount)
        try:
            assert type(charge) is str and charge.startswith('ch_')
            if amount is not None:
                assert type(amount) is int and amount > 0
        except AssertionError:
            raise UserError(400, 'Bad request')

        charge_obj = Charge._api_retrieve(charge)

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

        self.charge = charge
        self.metadata = metadata or {}
        self.amount = amount
        self.date = self.created
        self.currency = charge_obj.currency
        self.status = 'succeeded'

        if self.amount is None:
            self.amount = charge_obj.amount