cancelPaymentByIdempotencyKey method
Cancels (voids) a payment identified by the idempotency key that is specified in the request.
Use this method when the status of a CreatePayment request is unknown (for example, after you send a CreatePayment request, a network error occurs and you do not get a response). In this case, you can direct Square to cancel the payment using this endpoint. In the request, you provide the same idempotency key that you provided in your CreatePayment request that you want to cancel. After canceling the payment, you can submit your CreatePayment request again.
Note that if no payment with the specified idempotency key is found, no action is taken and the endpoint returns successful
Implementation
Future<bool> cancelPaymentByIdempotencyKey({
required String idempotencyKey,
String? authToken,
}) async {
authToken ??= authenticationService.getCachedToken()?.accessToken;
Map<String, String> headers = {
"Authorization": "Bearer ${authToken ?? ""}",
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json',
};
Uri endpoint = Uri.https(
baseUrl, "/v2/payments/cancel");
//print (endpoint.toString());
var request = {
"idempotency_key": idempotencyKey
};
var response = await
http.post(endpoint, body: jsonEncode(request), headers: headers);
if (response.statusCode == 200) {
print (jsonDecode(response.body));
return true;
}
else {
print (response.body);
throw PaymentException(statusCode: response.statusCode, message: SquarePaymentResponse.fromJson(jsonDecode(response.body)).errors?[0].detail?.toString());
}
}