Coupon.fromPresta constructor

Coupon.fromPresta(
  1. Map<String, dynamic> json
)

Implementation

Coupon.fromPresta(Map<String, dynamic> json) {
  try {
    code = json['code'];
    status = CouponStatus.publish;
    id = json['id'].toString();
    usageLimit = int.tryParse(json['quantity'].toString());
    usageCount = int.tryParse(json['partial_use'].toString());
    if (usageCount != null && usageLimit != null) {
      usageLimit = (usageCount ?? 0) + (usageLimit ?? 0);
    }
    usageLimitPerUser = int.tryParse(json['quantity_per_user'].toString());
    if (double.parse(json['reduction_percent']) > 0.0) {
      discountType = 'percent';
      amount = double.parse(json['reduction_percent']);
    } else {
      discountType = 'fixed_cart';
      amount = double.parse(json['reduction_amount']);
    }
    emailRestrictions = [];
    description = json['name'];
    minimumAmount = json['minimum_amount'] != null
        ? double.parse(json['minimum_amount'].toString())
        : 0.0;
    maximumAmount = 0.0;
    freeShipping = json['free_shipping'] == '1' ? true : false;
    dateExpires = DateTime.parse(json['date_to']);
    message = '';
  } catch (e) {
    // ignore: avoid_print
    print(e.toString());
  }
}