I make a post call to Stripe checkout.I pass multiple line_items because it is from a cart. But eveytime a trigger the call, Stripe return missing success url, even though i pass it. And when i look at the logs in Dashboard of Stripe, i see that the request body is just empty. But the body has data in DevTools. So i really don't know what i do wrong. I think the problem is not a missing success url, but more about the alyout of the list i think?
Here is the payload in DevTools:
I have a list with data type winkelmandItemTypeSlijterij. What the code does is format that list in a list that Stripe expects.
This is the code:
I tried to format the list into the format Stripe expects, but without success
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/actions/actions.dart' as action_blocks;
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<String> nogEenStripe(
List<WinkelmandItemTypeSlijterijStruct> winkelmandList) async {
// Add your function code here!
List<Map<String, dynamic>> aangepasteLijst = [];
for (var item in winkelmandList) {
aangepasteLijst.add({
'price_data': {
'price_data': {
'product': 'prod_${item.product}'
} // Formatteer productnaam
},
'quantity': item.aantal,
});
}
// Maak de formuliergegevens aan
Map<String, dynamic> formData = {
'mode': 'payment',
'line_items': aangepasteLijst,
'success_url': "https://example.com/success",
'cancel_url': "https://example.com/cancel",
};
// Voer de HTTP-postaanvraag uit naar de Stripe Checkout API
final response = await http.post(
Uri.parse('https://api.stripe.com/v1/checkout/sessions'),
headers: {
'Authorization':
'Bearer xxxxxxx',
'Content-Type':
'application/json', // Content-Type gewijzigd naar 'application/json'
},
body: jsonEncode(
formData), // Formuliergegevens worden nu als JSON-gecodeerd verzonden
);
// Controleer of de aanvraag succesvol was
if (response.statusCode == 200) {
// Retourneer de sessie-ID uit de Stripe Checkout API-response
return jsonDecode(response.body)['id'];
} else {
// Retourneer een foutmelding als er een probleem was met de aanvraag
throw Exception('Failed to create checkout session');
}
}
The product in your request was not a Stripe Product. The product ID (prod_xxx) should be created from Stripe Products API.
In addition,
unit_amountwas spelled incorrectly in the request. Your code showedunit_amout,nwas missing in theamount.I'd recommend checking your request against the Checkout Session integration doc to ensure that you set the correct request parameters and their values: https://docs.stripe.com/payments/accept-a-payment?platform=web&ui=stripe-hosted
You can also find the request logs in the Dashboard https://dashboard.stripe.com/test/logs to check the errors and requests that have been sent by your system.