|
| 1 | +from argparse import ArgumentParser |
| 2 | +from dataclasses import dataclass |
| 3 | +from importlib.resources import as_file, files |
| 4 | +import logging |
| 5 | +from os import environ |
| 6 | + |
| 7 | +from aiohttp import web |
| 8 | +import stripe |
| 9 | + |
| 10 | +use_real_stripe_api = False |
| 11 | +stripe_api_pk = 'pk_test_12345' |
| 12 | +stripe.api_key = 'sk_test_12345' |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class CustomerState: |
| 17 | + cus: str | None = None |
| 18 | + pm: str | None = None |
| 19 | + |
| 20 | + |
| 21 | +# Normally these values would be securely stored in a database, indexed by some |
| 22 | +# authenticated customer identifier. For this sample, we have no authentication |
| 23 | +# system so just store one global "customer": |
| 24 | +customer_state = CustomerState() |
| 25 | + |
| 26 | + |
| 27 | +app = web.Application() |
| 28 | +routes = web.RouteTableDef() |
| 29 | + |
| 30 | + |
| 31 | +@routes.get('/stripe.js') |
| 32 | +async def stripe_js(request): |
| 33 | + del request |
| 34 | + |
| 35 | + global use_real_stripe_api |
| 36 | + |
| 37 | + if use_real_stripe_api: |
| 38 | + stripe_js_location = 'https://js.stripe.com/v3/' |
| 39 | + else: |
| 40 | + stripe_js_location = 'http://localhost:8420/js.stripe.com/v3/' |
| 41 | + |
| 42 | + return web.Response(content_type='application/javascript', text=f"""\ |
| 43 | +const script = document.createElement('script'); |
| 44 | +script.src = "{stripe_js_location}"; |
| 45 | +document.head.appendChild(script); |
| 46 | +""") |
| 47 | + |
| 48 | + |
| 49 | +@routes.get('/pm_setup.js') |
| 50 | +async def pm_setup_js(request): |
| 51 | + del request |
| 52 | + |
| 53 | + with as_file(files('samples.pm_setup').joinpath('pm_setup.js')) as f: |
| 54 | + return web.FileResponse(f) |
| 55 | + |
| 56 | + |
| 57 | +@routes.get('/') |
| 58 | +async def index(request): |
| 59 | + del request |
| 60 | + |
| 61 | + global use_real_stripe_api |
| 62 | + |
| 63 | + with files('samples.pm_setup').joinpath('index.html').open('r') as f: |
| 64 | + return web.Response( |
| 65 | + text=f.read(), |
| 66 | + content_type='text/html', |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@routes.post('/setup_intent') |
| 71 | +async def setup_intent(request): |
| 72 | + del request |
| 73 | + |
| 74 | + global customer_state, stripe_api_pk |
| 75 | + |
| 76 | + cus = stripe.Customer.create() |
| 77 | + customer_state.cus = cus.id |
| 78 | + |
| 79 | + seti = stripe.SetupIntent.create( |
| 80 | + customer=cus.id, |
| 81 | + payment_method_types=["card"], |
| 82 | + ) |
| 83 | + return web.json_response(dict( |
| 84 | + id=seti.id, |
| 85 | + client_secret=seti.client_secret, |
| 86 | + stripe_api_pk=stripe_api_pk, |
| 87 | + )) |
| 88 | + |
| 89 | + |
| 90 | +@routes.post('/payment_method') |
| 91 | +async def payment_method(request): |
| 92 | + body = await request.json() |
| 93 | + |
| 94 | + seti = stripe.SetupIntent.retrieve(body['setup_intent']) |
| 95 | + |
| 96 | + customer_state.pm = seti.payment_method |
| 97 | + |
| 98 | + return web.Response() |
| 99 | + |
| 100 | + |
| 101 | +@routes.post('/payment_intent') |
| 102 | +async def payment_intent(request): |
| 103 | + global customer_state |
| 104 | + |
| 105 | + body = await request.json() |
| 106 | + |
| 107 | + pi = stripe.PaymentIntent.create( |
| 108 | + customer=customer_state.cus, |
| 109 | + payment_method=customer_state.pm, |
| 110 | + amount=body['amount'], |
| 111 | + currency='usd', |
| 112 | + ) |
| 113 | + |
| 114 | + return web.json_response(dict( |
| 115 | + client_secret=pi.client_secret, |
| 116 | + )) |
| 117 | + |
| 118 | + |
| 119 | +app.add_routes(routes) |
| 120 | + |
| 121 | + |
| 122 | +def main(): |
| 123 | + global stripe_api_pk, use_real_stripe_api |
| 124 | + |
| 125 | + parser = ArgumentParser() |
| 126 | + parser.add_argument( |
| 127 | + '--real-stripe', action='store_true', help="""\ |
| 128 | +Use the actual Stripe API. This is useful for verifying this sample and |
| 129 | +localstripe are providing an accurate simulation. |
| 130 | +
|
| 131 | +To use, you must set the environment variable SK to your Stripe account's |
| 132 | +secret API key, and PK to your Stripe account's publishable API key. It is |
| 133 | +obviously recommended that you use the test mode variant of your Stripe account |
| 134 | +for this. |
| 135 | +""") |
| 136 | + args = parser.parse_args() |
| 137 | + |
| 138 | + if args.real_stripe: |
| 139 | + use_real_stripe_api = True |
| 140 | + stripe.api_key = environ.get('SK') |
| 141 | + stripe_api_pk = environ.get('PK') |
| 142 | + if not stripe.api_key or not stripe_api_pk: |
| 143 | + parser.print_help() |
| 144 | + parser.exit(1) |
| 145 | + else: |
| 146 | + stripe.api_base = 'http://localhost:8420' |
| 147 | + |
| 148 | + logger = logging.getLogger('aiohttp.access') |
| 149 | + logger.setLevel(logging.DEBUG) |
| 150 | + logger.addHandler(logging.StreamHandler()) |
| 151 | + |
| 152 | + web.run_app(app, access_log=logger) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + main() |
0 commit comments