我试图通过Strap将付款设置为Flutter,但当我单击按钮“checkout”时出现问题,该按钮将指向付款页面。一旦单击,就会显示错误消息,称“错误:FormatException:意外字符(第2行,字符% 1)'。
我认为这可能与jsonResponse有关,它不引用JSON对象?有人有想法吗?非常感谢您的帮助SEE IMAGE HERE
`[tag:import 'dart:convert';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;
import '../models/product.dart';
class PaymentHandler {
final String? payIntentId;
final bool isError;
final String message;
PaymentHandler({
this.payIntentId,
required this.isError,
required this.message,
});
}
class PaymentService {
PaymentService();
Future<PaymentHandler> initPaymentSheet(User user, double totalAmount) async {
try {
// 1. create payment intent on the server
final response = await http.post(
Uri.parse(
'http://us-central1-ecommerceapp-34d7f.cloudfunctions.net/stripePaymentIntentRequest'),
body: {
'email': user.email,
'amount': (totalAmount * 100).toString(),
});
final jsonResponse = jsonDecode(response.body);
//2. initialize the payment sheet
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: jsonResponse['paymentIntent'],
merchantDisplayName: 'Flutter Stripe Store Demo',
customerId: jsonResponse['customer'],
customerEphemeralKeySecret: jsonResponse['ephemeralKey'],
style: ThemeMode.light,
testEnv: true,
merchantCountryCode: 'US',
),
);
// 3. Present to the user and await the result
await Stripe.instance.presentPaymentSheet();
return PaymentHandler(
isError: false,
message: "Success",
payIntentId: jsonResponse['paymentIntent']);
// Note: This is better done through our cloud functions
} catch (e) {
if (e is StripeException) {
return PaymentHandler(
isError: true,
message: 'Error from Stripe: ${e.error.localizedMessage}');
} else {
return PaymentHandler(isError: true, message: 'Error: ${e}');
}
}
}
}]`
我试着升级,重新验证我的firebase帐户,改变变量...
2条答案
按热度按时间oknwwptz1#
你从API调用中得到的是一个网页而不是JSON。这可能是因为以下两个原因之一:
或两者的组合:
nzk0hqpo2#