dart 错误:FormatException:意外字符(第2行,字符% 1)

de90aj5v  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(128)

我试图通过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帐户,改变变量...

oknwwptz

oknwwptz1#

你从API调用中得到的是一个网页而不是JSON。这可能是因为以下两个原因之一:

  1. API需要进行身份验证。在这种情况下,您需要在请求中添加身份验证/授权头,如:
var headers = {  'Authorization': 'Bearer <your_token>' }
  1. API需要定义内容类型和/或接受头部,如:
var headers = {   
            'Content-Type': 'application/json',
            'Accept': 'application/json'
           }

或两者的组合:

{ 
               'Authorization': 'Bearer <your_token>',
               'Content-Type': 'application/json',
               'Accept': 'application/json'
              }
nzk0hqpo

nzk0hqpo2#

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: json.encode({
           'email': user.email,
           'amount': (totalAmount * 100).toString(),
         }), 
        headers: {
          "Authorization": 'Bearer <private_key>',
          "Content-Type" : 'application/json',
          "Accept": 'application/json'}
       );
     
       final jsonResponse = jsonDecode(response.body) ;

相关问题