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

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'

Related

Flutter http can't get Bearer Token from .NET Api

I am starting flutter mobile development and have problem with http package http calls. I already have working API written in .NET with included Micorosft Identity. My goal is to create flutter mobile app that gets data from that API with authorizauion. I have problems implementing username-password authorization to get Token from API. Same API call works on Postman and in my existing Xamarin Forms App. Same problem with http call where in its header I use token which I get from Postman. Using VS Code, all packages installed, can retrive sample data from openweathermap.org in same flutter app. My code is:
Recive Bearer Token from API:
Future<String> authorization(String username, String password) async {
Uri uri = Uri.parse('https://myapi/token');
var request = http.MultipartRequest('POST', uri);
request.fields.addAll({
'grant_type': 'password',
'username': username,
'password': password,
});
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
Map<String, dynamic> auth =
jsonDecode(await response.stream.bytesToString());
return auth['access_token'];
} else {
return "";
}
}
GET Cars Data from API:
Future<String> getCars() async {
Uri uri = Uri.parse('https://myapi/api/getcars');
var token =
'WorkingToken';
http.Response response = await http.get(uri, headers: {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": "Bearer $token",
});
return response.body;
}
Microsoft Identity Authorization request expects application/x-www-form-urlencoded content.
Future<String> authorization(String username, String password) async {
final response = await http.post(
Uri.parse('https:/host/path/token'),
headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
},
body: <String, String>{
'grant_type': 'password',
'username': username,
'password': password,
},
encoding: Encoding.getByName('utf-8')
);
if (response.statusCode == 200) {
return jsonDecode(response.body)['access_token'];
} else {
throw Exception(response.reasonPhrase);
}
}
Your question is very general and your problem depends on your response data. Of course, I did not realize that you have a problem receiving the token or using the token as a header for another api!
But since you are just starting to use Flutter, I suggest you to use the Chopper Library to work with HTTP services and api.
It's very simple and fase.

Flutter how to upload a photo to REST API

I need to upload an image with caption and username to API that is built using Django. Create Post view in Django is marked with #permission_classes((IsAuthenticated,)). This is the code:
#permission_classes((IsAuthenticated,))
class PostCreateAPIView(CreateAPIView):
serializer_class = PostSerializer
def get_queryset(self):
return Post.objects.all()
Serializer:
class PostSerializer(ModelSerializer):
class Meta:
model = Post
fields = ('author', 'description', 'image', 'created_at')
I did some research and found that since only authenticated users can post image I need to somehow use the token which users receive on login.
I am getting the user token when I login and have been able to save it locally using hive. However I have no idea what to do next.
static Future<dynamic> loginUser(String username, String password) async {
final response = await http.post("$apiURL/en/api/users/login/", body: {
"username": username,
"password": password,
});
return response?.body;
}
This is my login code and it returns json format with username, user_id and token. Smth like this:
{
"token": "dc9e0de8fa2eaa917657e810db06aad2458e4f65",
"user_id": 4,
"username": "maria"
}
Merging my suggestion given in comments.
Write this to add headers with authentication token and files both at the same time :
upload(File imageFile, String token) async {
// open a bytestream
var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
// get file length
var length = await imageFile.length();
// string to uri
var uri = Uri.parse("http://ip:8082/composer/predict");
// create multipart request
var request = new http.MultipartRequest("POST", uri);
// add headers with Auth token
Map<String, String> headers = { "Authorization": "Token $token"};
request.headers.addAll(headers);
// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(imageFile.path));
// add file to multipart
request.files.add(multipartFile);
// send
var response = await request.send();
print(response.statusCode);
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
What you probably want to do is to pass the token inside the request header. Something like this should work:
static Future<dynamic> loginUser(String username, String password) async {
final response = await http.post(
"$apiURL/en/api/users/login/",
body: {
"username": username,
"password": password,
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Token $token',
},
);
return response?.body;
}
Note that the headers contains a field named Authorization, this is the place where you pass your the token. The keyword Token inside the string containing the token specifies the kind of authentication you are using. If it doesn't work, maybe you are using another kind of Authorization.
I recommend you take a look at https://www.django-rest-framework.org/api-guide/authentication/

Client Login using Flutter for Mediawiki

I am in the learning proess for both flutter and requests so forgive me if it is a simple mistake. I am trying to make a client login to a mediaiwki instance using client login api. I can fetch the login token succesfully but when I try to login it says invalid csrf token it gives {"error":{"code":"badtoken","info":"Invalid CSRF token.","*":". The api I am using to login is as follows.
https://www.mediawiki.org/wiki/API:Login#Method_2._clientlogin
Thanks for your help.
To fetch login token succesfully I use
_fetch_login_token() async {
Map<String, String> headers = {"Content-type": "application/json"};
Map<String, String> body = {
'action': "query",
'meta': "tokens",
'type': "login",
'format': "json"
};
Response response = await post(
url,
body: body,
);
//print(response);
// int statusCode = response.statusCode;
// print(statusCode);
var decoded = jsonDecode(response.body);
print(decoded);
var jsonsData = response.body; // toString of Response's body is assigned to jsonDataString
var data = jsonDecode(jsonsData);
var token=data['query']['tokens']['logintoken'];
return _makePostRequest(token);
}
And my failed login as follows
Map<String, String> body = {
'action': "clientlogin",
'username': username,
'password': password,
'loginreturnurl': url,
'logintoken': loginToken,
'format': "json"
};
Response response = await post(
url,
body:body,
);
I solved the problem.For future reference, I have downloaded dio package and added intercepter and cookie manager in order to persist the cookies.

Receiving Invalid request error from stripe while creating payment intent using 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.

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