#sukhwinder-singh-mann_error
1 messages ¡ Page 1 of 1 (latest)
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
âąď¸ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
đ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1316522213578506341
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
code :
import stripe
from fastapi import FastAPI, HTTPException
from typing import Optional
import json
class PaymentProcessor:
def __init__(self, stripe_api_key: str, webhook_secret: Optional[str] = None):
# Initialize Stripe with your API key
stripe.api_key = stripe_api_key
self.webhook_secret = webhook_secret
async def create_payment_session(
self,
success_url: str,
cancel_url: str,
user_id: str, # User id to identify customer
amount: int, # Amount in cents ($5.00)
user_email: str = 'gb98159@gmail.com', # User email for customer creation
interval: str = "month", # Recurring interval (month/year)
currency: str = "usd", # Currency (default: USD)
):
"""
Create a Stripe Checkout Session for payments with optional coupon support.
The coupon code can be applied by the user during checkout.
"""
try:
# Step 1: Check if customer already exists in Stripe
customers = stripe.Customer.list(email=user_email)
if customers.data:
customer_id = customers.data[0].id
# Check if the customer has any payment methods saved
payment_methods = stripe.PaymentMethod.list(customer=customer_id, type="card")
payment_method_id = payment_methods.data[0].id if payment_methods.data else None
else:
customer_id = None
payment_method_id = None
# Step 2: Create a Product
product = stripe.Product.create(
name="Premium Discord Bot Subscription",
description="Monthly subscription for premium features",
type="service",
)
# Step 3: Create the price linked to the product
price = stripe.Price.create(
unit_amount=amount, # Amount in cents ($5.00)
currency=currency,
recurring={"interval": interval}, # Recurring billing interval
product=product.id, # Link to the product
)
# Step 4: Create the Checkout Session
session_params = {
"allow_promotion_codes": True, # Allow promo codes
"line_items": [{
"price": price.id,
"quantity": 1,
}],
"mode": "subscription", # Subscription mode
"success_url": success_url, # Redirect URL after successful payment
"cancel_url": cancel_url, # Redirect URL if payment is canceled
"saved_payment_method_options": {
"allow_redisplay_filters": ["always"],
"payment_method_save": 'enabled',
},
"adaptive_pricing": {"enabled": True},
"payment_method_collection": "always",
}
# If the customer has a saved payment method, pass the customer ID and payment method ID
if customer_id:
session_params["customer"] = customer_id # Link to the saved customer
if payment_method_id:
session_params["payment_method_configuration"] = payment_method_id
else:
session_params["payment_method_types"] = ["card"]
else:
session_params["payment_method_types"] = ["card"]
session = stripe.checkout.Session.create(**session_params)
# Step 5: Return the checkout session URL to redirect the user
return {"session_url": session.url}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to create payment session: {str(e)}")
I don't think there is a way to determine the first selected payment method but am looking in to it
Also to clarify, the payment_method_configuration parameter is a completely separate thing. PMCs determine which payment method types are shown as available for the checkout session.
i am useing a payment method id and it shows
Failed to create payment session: 500: Failed to create payment session: Request req_rtQWd4b0ppyVg8: No such payment_method_configuration: pm_1QUwKkSGaRjnt1UfNXQpuHWM
Yep, looks like there isn't a way to specify which is the first that shows up
and that payment_method_configuration id is correct
Is it showing up in your list of saved PMs at all?
Right, that is a payment method object, but it is not a payment method configuration object, which is what that parameter is expecting the ID of
can you explain more how can i solve this
It is not currently possible to do what you are trying to do.
If the payment method isn't showing up at all in the list of saved payment methods, you may need to set allow_redisplay='always' on the payment method. But unfortunately there isn't a way to say "select this payment method by default for this checkout session"
i tried with both payment method ids but still shoow this error
**Mastercard: **
Failed to create payment session: 500: Failed to create payment session: Request req_I8Jwz0FWZspQ1g: No such payment_method_configuration: pm_1QUwFVSGaRjnt1UfNClu3gyP
**Visa: **
Failed to create payment session: 500: Failed to create payment session: Request req_rtQWd4b0ppyVg8: No such payment_method_configuration: pm_1QUwKkSGaRjnt1UfNXQpuHWM
You need to remove your line that specifies payment_method_configuration
That is not what that parameter is for
it is in docs