FastAPI und Vue integrieren Stripe-Zahlungen. Kann ich Stripe-Elemente weglassen?Python

Python-Programme
Anonymous
 FastAPI und Vue integrieren Stripe-Zahlungen. Kann ich Stripe-Elemente weglassen?

Post by Anonymous »

Derzeit verwendet unser Projekt FastAPI als Backend und Vue als Frontend. Wir generieren im Backend eine Zahlungsabsichts-ID und das Frontend erstellt mit dieser ID die entsprechende Zahlung. Nach erfolgreicher Zahlung sendet der Webhook von Stripe das Ereignis an die Backend-API. Beim Bezahlvorgang im Frontend treten jedoch einige Probleme auf. Ich werde zuerst etwas Code posten:
Backend-Code:

Code: Select all

@router.post("/create_payment_intent")
async def create_payment_intent(
request: Request,
order_id: str = Form(...),
order_amount: int = Form(...),
currency: str = Form("gbp")
):
metadata = {
'user_id': "",
'order_id': order_id
}
# Create payment intent
intent = stripe.PaymentIntent.create(
amount=order_amount,
currency=currency,
receipt_email=user_email,
metadata=metadata,
automatic_payment_methods={'enabled': True}
)

res = {
"client_secret": intent.client_secret,
"payment_intent_id": intent.id
}
return JSONResponse(RestResult(code=Code.TRUE, msg=Message.SUCCESS_CREATE_PAYMENT_INTENT, data=res).__dict__)
Frontend-Code:

Code: Select all

import { RequestApi } from '@/api/RequestApi';
import { loadStripe } from '@stripe/stripe-js';

export class StripeUtil {
static pay(params: any) {
return new Promise((resolve, reject) => {
RequestApi.createPayment(params).then(async res => {
const stripe = await loadStripe(import.meta.env.VITE_STRIPE_KEY);
if (!stripe) {
return reject('stripe load failed');
}
const { client_secret, payment_intent_id } = res.data;

const { error } = await stripe.confirmPayment({
clientSecret: client_secret,
confirmParams: {
payment_method: payment_intent_id,
return_url: 'https://example.com/order/123/complete',
},
});
if (error.type) {
showDialog({ message: error.message }).then();
reject(error.message);
}
}).catch(error => {
return reject(error);
});
});
}
}
Fehlermeldung:

Code: Select all

{
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such PaymentMethod: 'pi_...'; It's possible this PaymentMethod exists on one of your connected accounts, in which case you should retry this request on that connected account. Learn more at https://stripe.com/docs/connect/authentication",
"param": "payment_method",
"payment_intent": {
"id": "pi_...",
"object": "payment_intent",
"amount": 1512,
"amount_details": {
"tip": {}
},
"automatic_payment_methods": {
"allow_redirects": "always",
"enabled": true
},
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic_async",
"client_secret": "pi_..._secret_...",
"confirmation_method": "automatic",
"created": 1731412378,
"currency": "gbp",
"description": null,
"last_payment_error": null,
"livemode": false,
"next_action": null,
"payment_method": null,
"payment_method_configuration_details": null,
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": "[email protected]",
"setup_future_usage": null,
"shipping": null,
"source": null,
"status": "requires_payment_method"
},
"request_log_url": "https://dashboard.stripe.com/test/logs/req_fsqWt4IIR3rXhM?t=1731412381",
"type": "invalid_request_error"
}
Aus der Fehlermeldung geht hervor, dass der Fehler „requires_paid_method“ anzeigt. Doch wie soll die payment_method erstellt werden? Das Hauptproblem besteht darin, dass wir nicht vorhaben, die Elements von Stripe zum Sammeln von Benutzerkarteninformationen zu verwenden. Wie sollen wir damit umgehen? Ich habe die Dokumentation von Stripe durchgesehen, aber keine passende Lösung gefunden, möglicherweise aufgrund meiner begrenzten Englischkenntnisse und dem Fehlen einiger wichtiger Informationen.
Ich hoffe, einige Hinweise zu erhalten. Vielen Dank im Voraus für Ihre Hilfe, insbesondere dafür, dass Sie sich die Zeit genommen haben, mir trotz Ihres vollen Terminkalenders zu helfen. Ihre Unterstützung und Ihre Erkenntnisse werden sehr geschätzt. Vielen Dank!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post