createCheckout method

Future<Checkout?> createCheckout({
  1. required String amount,
  2. required String currency,
  3. required String countryCode,
  4. required String customerId,
  5. String? orderNumber,
  6. String? completePaymentURL,
  7. String? errorPaymentURL,
  8. String? merchantReferenceId,
  9. List<String>? paymentMethods,
  10. bool useCardholdersPreferredCurrency = true,
  11. String languageCode = 'en',
})

Used for generating a new checkout object.

The required parameters are amount, currency, countryCode, and customerId

  • amount has to be passed as a String.

  • currency of the country, has to be passed as a String.

  • countryCode from where the payment is being processed, has to be passed as a String.

  • customerId of the customer, has to be passed as a String.

The optional parameters are:

  • orderNumber that you may want to associate with this checkout, maybe helpful while generating invoice for the order.

  • completePaymentURL the URL where you want to redirect the user after completion of a successful transaction.

  • errorPaymentURL the URL where you want to redirect the user after completion of a failed transaction.

  • merchantReferenceId to be associated with the checkout.

  • paymentMethods that should be accepted, more information here.

  • useCardholdersPreferredCurrency whether to accept the card holder's preferred currency. By default, it's set to true.

  • languageCode By default, it's set to en (English).

Implementation

Future<Checkout?> createCheckout({
  required String amount,
  required String currency,
  required String countryCode,
  required String customerId,
  String? orderNumber,
  String? completePaymentURL,
  String? errorPaymentURL,
  String? merchantReferenceId,
  List<String>? paymentMethods,
  bool useCardholdersPreferredCurrency = true,
  String languageCode = 'en',
}) async {
  Checkout? checkoutDetails;

  var method = "post";
  var checkoutEndpoint = '/v1/checkout';

  final checkoutURL = Uri.parse(baseURL + checkoutEndpoint);

  var data = jsonEncode({
    "amount": amount,
    "complete_payment_url": completePaymentURL,
    "country": countryCode,
    "currency": currency,
    "error_payment_url": errorPaymentURL,
    "merchant_reference_id": merchantReferenceId,
    "cardholder_preferred_currency": useCardholdersPreferredCurrency,
    "language": languageCode,
    "metadata": {
      "merchant_defined": true,
      "sales_order": orderNumber,
    },
    "payment_method_types_include": paymentMethods,
    "customer": customerId,
  });

  final headers = _generateHeader(
    method: method,
    endpoint: checkoutEndpoint,
    body: data,
  );

  try {
    var response = await http.post(
      checkoutURL,
      headers: headers,
      body: data,
    );

    if (response.statusCode == 200) {
      dev.log('SUCCESSFULLY CHECKOUT');
      checkoutDetails = Checkout.fromJson(jsonDecode(response.body));
    } else {
      dev.log(response.statusCode.toString());
    }
  } catch (e) {
    dev.log('Failed to generate the checkout');
  }

  return checkoutDetails;
}