createPlan method

Future<SubscriptionPlan> createPlan({
  1. required String payPalRequestId,
  2. required String product_id,
  3. required String name,
  4. required String description,
  5. String status = "ACTIVE",
  6. Prefer prefer = Prefer.minimal,
  7. required List<BillingCycle> billingCycles,
  8. required PaymentPreferences paymentPreferences,
  9. required Taxes taxes,
  10. required bool quantity_supported,
})

Implementation

Future<SubscriptionPlan> createPlan({
  ///The server stores keys for 72 hours.
  required String payPalRequestId,
  ///The ID of the product.
  ///Minimum length: 6.
  ///Maximum length: 50.
  required String product_id,
  ///The plan name.
  ///Minimum length: 1.
  ///Maximum length: 127.
  required String name,
  ///The detailed description of the plan.
  ///Minimum length: 1.
  ///Maximum length: 127.
  required String description,
  ///The initial state of the plan. Allowed input values are CREATED and ACTIVE.
  ///The possible values are:
  ///CREATED. The plan was created. You cannot create subscriptions for a plan in this state.
  ///INACTIVE. The plan is inactive.
  ///ACTIVE. The plan is active. You can only create subscriptions for a plan in this state.
  String status = "ACTIVE",
  Prefer prefer = Prefer.minimal,
  required List<BillingCycle> billingCycles,
  required PaymentPreferences paymentPreferences,
  required Taxes taxes,
  ///Indicates whether you can subscribe to this plan by providing a quantity for the goods or service.
  required bool quantity_supported,
})async{
  //Parse billing cycles
  List<Map<String,dynamic>> allBillingCycles = [];
  for(BillingCycle billingCycle in billingCycles){
    allBillingCycles.add({
        "frequency": {
          "interval_unit": billingCycle.frequency.interval_unit,
          "interval_count": billingCycle.frequency.interval_count,
        },
        "tenure_type": _enumToString(billingCycle.tenure_type),
        "sequence": billingCycle.sequence,
        "total_cycles": billingCycle.total_cycles,
        "pricing_scheme": {
          "fixed_price": {
            "value": billingCycle.pricing_scheme.value,
            "currency_code": billingCycle.pricing_scheme.currency_code,
          }
        }
      },
    );
  }
  Map<String,dynamic> parameters = {
    "product_id": product_id,
    "name": name,
    "description": description,
    "status": "ACTIVE",
    "billing_cycles": allBillingCycles,
    "payment_preferences": {
      "auto_bill_outstanding": paymentPreferences.auto_bill_outstanding,
      "setup_fee": {
        "value": paymentPreferences.setup_fee.value,
        "currency_code": paymentPreferences.setup_fee.currency_code,
      },
      "setup_fee_failure_action": _enumToString(paymentPreferences.setup_fee_failure_action),
      "payment_failure_threshold": paymentPreferences.payment_failure_threshold,
    },
    "taxes": {
      "percentage": taxes.percentage,
      "inclusive": taxes.inclusive,
    }
  };
  String response = await SexyAPI(
    url: _url,
    parameters: {},
    path: "/v1/billing/plans",
  ).post(
    headers: {
      "Authorization" : "Bearer ${accessToken.access_token}",
      "Content-Type" : "application/json",
      "Prefer" : "return=${_enumToString(prefer)}",
      "PayPal-Request-Id" : payPalRequestId,
    },
    body: jsonEncode(parameters),
  );
  try{
    //Parse response
    return SubscriptionPlan.parse(response);
  }catch(err){
    throw response;
  }
}