createCharge method

Future<ChargeObject> createCharge({
  1. int? amount,
  2. required String name,
  3. CurrencyType? currency,
  4. required String description,
  5. required PricingType pricingType,
})

To request a cryptocurrency payment, you create a charge. You can create and view charges. Since cryptocurrency payments are push payments, a charge will expire after a waiting period (payment window) no payment has been detected. Charges are identified by a unique code.

amount amount to be charged.

name name of what you are charging for

description describe the good or service

pricingType indicate if there is a fixed price or no fixed price

currency optional value to use. only important when the priceType is PriceType.fixed

Implementation

Future<ChargeObject> createCharge(
    {int? amount,
    required String name,
    CurrencyType? currency,
    required String description,
    required PricingType pricingType}) async {
  ///A check to ensure that a value for amount is supplied
  ///anytime there is a fixed_price
  if (pricingType == PricingType.fixedPrice) {
    ///Make sure that the [amount] field has been specified
    assert(amount != null);
  }

  /// The response gotten from coinbase is stored here
  ChargeObject coinbaseResponse;

  /// Data retrieved from Coinbase API
  Map data;

  /// Map
  Map response = await postToDB(
    apiKey: _apiKey,
    api: 'https://api.commerce.coinbase.com/charges',
    body: {
      'name': name,
      'description': description,
      "pricing_type": getPricingType(pricingType),
      "local_price": pricingType == PricingType.fixedPrice
          ? {"amount": amount, "currency": getCurrency(currency!)}
          : null,
    },
  );
  if (response.containsKey('error')) {
    data = response['error'];
  } else {
    data = response['data'];
  }
  coinbaseResponse = ChargeObject.fromJson(data);
  Logger(debug).displayLog(coinbaseResponse.toString());

  return coinbaseResponse;
}