completePayment method

Future<Payment> completePayment({
  1. required String paymentId,
  2. required CompletePaymentRequest request,
  3. String? authToken,
})

Completes (captures) a payment.

By default, payments are set to complete immediately after they are created.

You can use this endpoint to complete a payment with the APPROVED status.

Implementation

Future<Payment> completePayment({
  required String paymentId,
  required CompletePaymentRequest request,
  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/$paymentId/complete");

  //print (endpoint.toString());

  var response = await
  http.post(endpoint, body: jsonEncode(request.toJson()), headers: headers);

  if (response.statusCode == 200) {
    print (jsonDecode(response.body));
    return SquarePaymentResponse.fromJson(jsonDecode(response.body)).payment!;
  }
  else {
    print (response.body);
    throw PaymentException(statusCode: response.statusCode, message: SquarePaymentResponse.fromJson(jsonDecode(response.body)).errors?[0].detail?.toString());
  }
}