Receiving Invalid request error from stripe while creating payment intent using Flutter - flutter

I am integrating Stripe in my flutter app, want to create payment intent using http POST request having mention here. But I am constantly receiving this error. Tried many thing!
{
"error": {
"code": "parameter_unknown",
"doc_url": "https://stripe.com/docs/error-codes/parameter-unknown",
"message": "Received unknown parameter: {\"amount\":2,\"currency\":\"usd\"}",
"param": "{\"amount\":2,\"currency\":\"usd\"}",
"type": "invalid_request_error"
}
}
My api function is this:
Future<Map<String, dynamic>> createPaymentIntent(int amount) async {
try {
var url = "https://api.stripe.com/v1/payment_intents";
var body = json.encode({
"amount": amount,
"currency": 'usd'
});
var headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'authorization': 'Bearer '+stripeSecret
};
await http.post(url, body: json.encode(body), headers: headers, encoding: Encoding.getByName("utf-8")).then((response) {
if(response.statusCode == 200){
print(response.body);
return json.decode(response.body);
}
else{
return response.reasonPhrase;
}
});
} catch (e) {
return e.message;
}
}
Note: 'Content-type': 'application/x-www-form-urlencoded' is necessary for this.

You are sending a JSON encoded body to the Stripe API, but the Stripe API expects a form-encoded request body:
Our API has predictable resource-oriented URLs, accepts form-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs.

Related

i could not accept the payment using flutter 'checkout.com'

I'm trying to enable payment in my app so i choose 'checkout.com' as a payment gateway
there are couples of methods there to accept the payment and i choose the one with token.
i managed to get the token successfully, but accepting the payment does not work. i am not sure if my code is wrong, or their API is broken for some reason. because it does not response me any data or anything the error code is 401
here is my code
Future<bool> makePayment(PaymentCard card, int amount) async {
String? token = await _getPaymentToken(card);
Map<String, dynamic> body = {
"source": {"type": "token", "token": token},
"amount": amount,
"currency": "USD",
};
http.Response respone = await http.post(Uri.parse(_paymentURL),
headers: _Payment_Header, body: jsonEncode(body));
print(respone.body);
if (respone.statusCode == 200) {
var data = await jsonDecode(respone.body);
print(data['response_summary']);
return true;
} else {
print("invalid - payment");
return false;
}
}
static const Map<String, String> _Payment_Header = {
'Content-Type': 'Application/json',
'Authorization': _secret_key,
};
the response body is empty.
this is the url
static const String _paymentURL = 'https://api.sandbox.checkout.com/payments';
also the secret key is correct.
any idea why ? thank you
Could be a couple of issues. You are most likely on the new platform which requires the word Bearer in front of the secret key. Your authorization header value would look similar to 'Bearer sk_xxxx'

POST call works on Postman but not on Flutter

Future<String> loginUser(User user) async {
var body = jsonEncode({
'strlogin': user.email,
});
Response response = await post(SeguriSignAPIURL.loginUser,
headers: headers,
body: body);
if (response.statusCode == 200) {
var decode = jsonDecode(response.body);
return decode['token'];
} else {
print(response.reasonPhrase);
return '';
}
Hey! When I make this Post call on Postman I get a result, however when I run this code on flutter I get a 400 error. My headers:
final headers = {
'Content-Type': 'application/json; charset=UTF-8',
"Accept": "application/json",
};
Postman:
In Postman you do the request using form-data, but in your Flutter code, you pass json-encoded body.
You either need to use form-data in Flutter or update your server code to accept json content type.

How can I make an http POST request from dart with authentication?

I'm trying to do a post, to create a user, but in a simple way. I do need to use authentication, so I'm passing the token in the header, as I would do in the GET request. But I'm not sure why I'm receiving the error "400 - Bad Request".
import 'package:http/http.dart';
{...}
Future test() async {
Response response = await post('https://access.altaviu.com/Data/api/Account/Register',
body: jsonEncode({
"Email": "useruser#gmail.com",
"Password": "QWERTY1234!",
"ConfirmPassword": "QWERTY1234!",
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ${UserAccountData.token}',
});
if (response.statusCode == 200) {
print('SUCCESSFUL TEST');
} else {
print('ERROR IN THE TEST ${response.statusCode} - ${response.reasonPhrase}');
}
}
I'm using the library (or package) "http: 0.12.0+2"

Can't able to call simple API

I just want to call simple API but can't able to call. If I tried to call in Postman then get proper response.
Look at my code
Future<AppServiceModel> getAllItems({String pageID = ""}) async {
final String _url = "http://www.textsite.com/api/app-view?page_id=";
final finalURL = Uri.encodeFull(_url);
try {
return http.get(finalURL, headers: {
HttpHeaders.contentTypeHeader: 'application/x-www-form-urlencoded',
HttpHeaders.acceptHeader: "application/json",
HttpHeaders.authorizationHeader: 'Bearer $accessToken'
}).then((response) {
Map<String, dynamic> _resDic =
Map<String, dynamic>.from(json.decode(response.body));
print('===>> Response : $_resDic');
return AppServiceModel.fromJson(_resDic);
});
} catch (e) {
print("Error: $e");
return null;
}
}
I also tried to pass QueryParameters by following How do you add query parameters to a Dart http request?
But each time get response
Response : {code: 7, msg: Your login session has expired. Please login again to continue., data: []}
I'm damm sure, passing right TOKEN.
Below is output of Postman

No response message after calling an API

Future<EventObject> addStudentToSubject(String studentCode, String subjectid) async {
try {
final encoding = APIConstants.OCTET_STREAM_ENCODING;
final response = await http.post('${APIConstants.API_BASE_LIVE_URL}/controller_educator/add_student_to_subject.php',
headers: {
'Accept': 'application/json',
},
body: {
'stud_code': studentCode,
'subj_id': subjectid
},
encoding: Encoding.getByName(encoding)
);
print("YAWA" + response.body);
} catch (Exception) {
return EventObject();
}
}
Is there something wrong with my code why i theres no response message??
Logcat just says "I/flutter ( 5013): YAWA"
Don't know what i'm missing.
I do not believe your API does not work in Postman. Can you indicate what status code did you get in Postman?
The problem in your code is your not checking the status code response of your API. The POST request you have called has probably had an empty body. Some POST request only returns the status code.
You should always check the status code of the API. If it returns 200 means your API has processed your request successfully.
final encoding = APIConstants.OCTET_STREAM_ENCODING;
final response = await http.post('${APIConstants.API_BASE_LIVE_URL}/controller_educator/add_student_to_subject.php',
headers: {
'Accept': 'application/json',
},
body: {
'stud_code': studentCode,
'subj_id': subjectid
},
encoding: Encoding.getByName(encoding)
);
print(${response.statusCode});
if (response.statusCode == 200) {
String data = response.body;
print(data);
} else {
print(response.statusCode);
}