createCheckout method

Future<Checkout> createCheckout({
  1. required String locationId,
  2. required CreateCheckoutRequest request,
  3. String? authToken,
})

Links a checkoutId to a checkout_page_url that customers are directed to in order to provide their payment information using a payment processing workflow hosted on connect.squareup.com.

Implementation

Future<Checkout> createCheckout({
  required String locationId,
  required CreateCheckoutRequest 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/locations/$locationId/checkouts");

  //print (endpoint.toString());

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

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