listPlans method

Future<ListOfPayPalPlans> listPlans({
  1. String? product_id,
  2. List<PayPalPlanId>? plan_ids,
  3. String page_size = "20",
  4. String page = "1",
})

Implementation

Future<ListOfPayPalPlans> listPlans({
  ///Filters the response by a Product ID. Minimum length: 6. Maximum length: 50.
  String? product_id,
  ///Filters the response by list of plan IDs. Filter supports upto 10 plan IDs. Minimum value: 3. Maximum value: 270.
  List<PayPalPlanId>? plan_ids,
  ///The number of items to return in the response. Minimum value: 1. Maximum value: 20.
  String page_size = "20",
  ///A non-zero integer which is the start index of the entire list of items to return in the response. The combination of page=1 and page_size=20 returns the first 20 items. The combination of page=2 and page_size=20 returns the next 20 items. Minimum value: 1. Maximum value: 100000.
  String page = "1",
})async{
  List<PayPalBillingPlan> billingPlans = [];
  Map<String,dynamic> parameters = {};
  if(product_id != null){
    parameters.addAll({
      "product_id" : product_id,
      "page_size" : page_size,
      "page" : page,
      "total_required" : true,
    });
  }
  if(plan_ids != null){
    List<String> planIds = [];
    for(PayPalPlanId planId in plan_ids){
      planIds.add(planId.plan_id);
    }
    parameters.addAll({
      "plan_ids" : planIds,
    });
  }
  String response = await SexyAPI(
    url: _url,
    path: "/v1/billing/plans",
    parameters: parameters,
  ).get(
    headers: {
      "Authorization" : "Bearer ${accessToken.access_token}",
      "Content-Type" : "application/json",
    },
  );
  Map<String,dynamic> parsedJSON = _parseResponse(response);
  for(Map<String,dynamic> plan in parsedJSON["plans"]){
    //Parse links
    List<PayPalProductLink> links = PayPalProductLink.parseLinks(parsedJSON["links"].cast<Map<String,dynamic>>());
    billingPlans.add(PayPalBillingPlan(
        id: plan["id"],
        product_id: plan["product_id"],
        status: plan["status"],
        name: plan["name"],
        description: plan["description"],
        create_time: plan["create_time"],
        links: links,
      ),
    );
  }
  //Parse PayPal billing plans
  try{
    return ListOfPayPalPlans(
      total_items: parsedJSON["total_items"] ?? 0,
      total_pages: parsedJSON["total_pages"] ?? 0,
      plans: billingPlans,
    );
  }catch(err){
    throw response;
  }
}