我非常感谢您的帮助,因为我遇到了以下问题。在Flutter中,我使用Flutter_Stripe包来实现一个支付模块。
在下面你会发现我的Flutter功能:
Future<void> initPayment({required String email, required int montant, required String nomOrga}) async{
try{
//1. Crée un payment intent sur le serveur
final response = await http.post(
Uri.parse("myFirebase URL"),
body: {
"email":email,
"amount": (montant*100).toString(),
},
);
print("email : $email - montant : $montant - organisation : $nomOrga");
final jsonResponse = jsonDecode(response.body);
print(jsonResponse);
//2. Initialise le payment sheet
await Stripe.instance.initPaymentSheet(paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: jsonResponse["paymentIntent"],
merchantDisplayName: nomOrga,
customerId: jsonResponse["customer"],
customerEphemeralKeySecret: jsonResponse["ephemeralKey"],
customFlow: true,
)
);
//3. Présenter le paiement
await Stripe.instance.presentPaymentSheet();
print("on a présenté la paymentsheet");
//4. Attacher la méthode de paiement au PaymentIntent et confirmer le paiement
/*final paymentIntentResult = await Stripe.instance.confirmPayment(
paymentIntentClientSecret: jsonResponse["client_secret"],
);*/
print("on a fini confirmpayment");
//print(paymentIntentResult.status);
/*if(paymentIntentResult.status == PaymentIntentsStatus.Succeeded){
//Paiement réussi
quickConfirm("Paiement réussi", "Le paiement est réussi", context);
}else{
quickAlert("Oups", "Le paiement a échoué", context);
}*/
}
catch(error){
if(error is StripeException){
quickAlert("Oups", "une erreur StripeException s'est produite\n${error.error.message}", context);
}else{
quickAlert("Oups", "une erreur brol s'est produite\n${error}", context);
}
}
}
在服务器功能下:
const functions = require("firebase-functions");
const stripe = require("stripe")("sk_test_...");
require("firebase-functions/logger/compat");
exports.stripePaymentIntentRequest = functions.https.onRequest(async (req, res) => {
try{
let customerId;
//Get the customer who's email matches the one sent by the client
const customerList = await stripe.customers.list({
email: req.body.email,
limit: 1,
});
console.log(customerList);
//Check if customer exists, if not create a new customer
if(customerList.data.lenght !== 0){
customerId = customerList.data[0].id;
} else {
const customer = await stripe.customers.create({
email: req.body.email,
});
customerId = customers.id;
}
//Creates a temporarysecret key linked with the customer
const ephemeralKey = await stripe.ephemeralKeys.create(
{customer: customerId},
{apiVersion: '2020-08-27'}
);
//Creates a new payment intent with amount passed from the client
const paymentIntent = await stripe.paymentIntents.create({
amount: parseInt(req.body.amount),
currency: 'eur',
customer: customerId,
automatic_payment_methods: {
enabled: true,
},
});
console.log('on arrive au send');
console.log(res);
res.send({
paymentIntent: paymentIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customerId,
publishableKey:'pk_test...',
success: true,
});
console.log('on a fini 200');
} catch(error) {
console.log('il y a eu une erreur :', error.message);
res.status(404).send({
success: false,
error: error.message,
});
}
});
我有以下问题:
a)我没有收到任何错误,但支付总是不完整,状态为"requires_payment_method"。我使用的是4242 ...测试卡(注意,我住在比利时,测试卡是美国卡,这是问题的原因吗?)。注意,PaymentIntent请求的状态为200。以下是它的结果:
payment_intent.created
Afficher les détails de l'événement
Données de l'événement
{
"id": "pi_...",
"object": "payment_intent",
"last_payment_error": null,
"livemode": false,
"next_action": null,
"status": "requires_payment_method",
"amount": 100,
"amount_capturable": 0,
"amount_details": {
"tip": {
}
},
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": {
"enabled": true
},
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret": "pi_...",
"confirmation_method": "automatic",
"created": 1677946647,
"currency": "eur",
"customer": "cus_...",
"description": null,
"invoice": null,
"latest_charge": null,
"metadata": {
},
"on_behalf_of": null,
"payment_method": null,
"payment_method_options": {
"bancontact": {
"preferred_language": "en"
},
"card": {
"installments": null,
"mandate_options": null,
"network": null,
"request_three_d_secure": "automatic"
},
"eps": {
},
"giropay": {
},
"ideal": {
}
},
"payment_method_types": [
"card",
"bancontact",
"eps",
"giropay",
"ideal"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"source": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"transfer_data": null,
"transfer_group": null
}
b)在Flutter代码中,point4(confirmPayment)在代码示例中被注解了,如果我不注解它,我会收到一个错误,类似于"Null不是String的子类型"
c)如果客户不存在于Stripe中,我会收到一个错误(可能来自javascript代码,我是否必须测试空响应?)
请注意,我不是一个专业人士(我试图开发自己的应用程序),所以我真的很感谢您的帮助,如果可能的话,详细的答案。
谢谢你的帮助!
贝尔纳
2条答案
按热度按时间von4xj4u1#
a)这看起来像payment_intent.created webhook事件,并且您的PaymentIntent已成功创建。但是,您需要验证请求 * 从前端到后端 * 是否已返回200状态及其数据。这两行:
B)您正在使用PaymentSheet,因此不需要此
confirmPayment
函数c)在这种情况下,您已经在后端创建了一个新客户。可能
req.body.email
为空,您可能希望记录完整的req.body
,并在调用检索客户API之前执行空检查hc2pp10m2#
感谢条纹失调团队的Vanya!她发现了问题:在初始工资单中
我设置了customFlow:真
解决方案是删除它。看起来当customFlow设置为true时,它将支付方式和确认分开。如果设置为true,那么我必须自己调用confirmPaymentSheetPayment。