Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/69111a25-dab4-8000-8929-8d5e16a9da88
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Install: === <syntaxhighlight>pip install stripe fastapi uvicorn </syntaxhighlight> server.py (minimal example β test mode): <syntaxhighlight lang="python">from fastapi import FastAPI, HTTPException from pydantic import BaseModel import stripe import os stripe.api_key = os.getenv("STRIPE_SECRET_KEY") # set to your test secret key app = FastAPI() class PayPayload(BaseModel): # 'paymentMethodData' is the object from Google Pay JS response token: str # the raw token string (you will parse from Google Pay response) amount: int # cents currency: str = "usd" description: str = "Test charge via Google Pay (stripe)" @app.post("/charge") async def charge(payload: PayPayload): try: # For Google Pay + Stripe gateway tokenization, Google Pay returns an object # - In web flow, you typically receive <code>paymentData</code> and inside it # <code>paymentMethodData.tokenizationData.token</code> (a JSON string) or token object. # Here we expect client to extract the token string and POST it as <code>token</code>. token = payload.token # Create a PaymentIntent with amount and currency and use the token as payment_method_data payment_intent = stripe.PaymentIntent.create( amount=payload.amount, currency=payload.currency, payment_method_types=["card"], description=payload.description, # confirm immediately using the token payment_method_data={ "type": "card", "card": { # Stripe can accept tokenized card details from Google Pay gateway token # Instead of direct raw card number, provide the token as the 'token' parameter "token": token } }, confirm=True # confirm immediately in test ) return {"status": "success", "payment_intent": payment_intent.id, "pi_status": payment_intent.status} except stripe.error.CardError as e: # handle declined card raise HTTPException(status_code=400, detail=str(e.user_message or e)) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) </syntaxhighlight> How the front-end should call this: # Use Google Pay JS to get paymentData. # Extract the gateway token: paymentData.paymentMethodData.tokenizationData.token # POST to /charge with { "token": "<that token>", "amount": 1000, "currency": "usd" } Important: * Stripe test keys allow real token acceptance in test environment. * Confirm in Stripe dashboard (test view) that the PaymentIntent shows up and is successful. If you want a Node.js/Express example I can add it.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)