> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Method name: createPlan(plan: Plan)

# Method package: wixPricingPlansV2

# Method menu location: wixPricingPlansV2 --> plans --> createPlan

# Method Link: https://dev.wix.com/docs/velo/apis/wix-pricing-plans-v2/plans/create-plan.md

# Method Description:

Creates a pricing plan. The specified `plan` object must contain a pricing model. A pricing model can be 1 of the following:
- **A subscription**: A subscription with recurring payments and how often the plan occurs. Subscriptions can have free trial days.
- **A plan that doesn't renew**: A single payment for a specific duration that doesn't renew.
- **An unlimited plan**: A single payment for an unlimited amount of time (until canceled).
Pricing plans are available to the Wix user in the Pricing Plans section in a site's dashboard.



# Method Code Examples:

*** Note: do not assume any prop names or enum values other than the ones in the example.

## Create a plan with optional fields (dashboard page code)

```javascript
import { plans } from 'wix-pricing-plans.v2';

/* Sample plan value:
 * {
 *   allowFutureStartDate: true,
 *   buyerCanCancel: true,
 *   description: 'Fun content for our younger users',
 *   formId: 'ee62cefa-bdc2-4b5d-baab-6faeef83cecb',
 *   maxPurchasesPerBuyer: 1,
 *   name: 'Junior',
 *   perks: {
 *     values: [
 *       'Multiplayer',
 *       'Multiple devices',
 *       'No ads",
 *       'Unlimited access'
 *     ]
 *   },
 *   pricing: {
 *     price: {
 *       currency: 'USD',
 *       value: '10.00"
 *     },
 *     subscription: {
 *       cycleCount: 12,
 *       cycleDuration: {
 *         count: 1,
 *         unit: 'MONTH'
 *       }
 *     }
 *   },
 *   public: true,
 *   termsAndConditions: 'No sharing access with others!'
 * }
 */

export async function myCreatePlanFunction(plan) {
  try {
    const newPlan = await plans.createPlan(plan);

    const planId = newPlan._id;
    const pricingObject = newPlan.pricing;
    const perks = newPlan.perks;

    return newPlan;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "_createdDate": "2024-01-21T07:06:06.993Z",
 *   "_id": "046371ff-7e5d-4bdb-a01d-a00ff462bed8",
 *   "_updatedDate": "2024-01-21T07:06:06.993Z",
 *   "allowFutureStartDate": true,
 *   "archived": false,
 *   "buyerCanCancel": true,
 *   "description": "Fun content for our younger users",
 *   "formId": "ee62cefa-bdc2-4b5d-baab-6faeef83cecb",
 *   "hasOrders": false,
 *   "maxPurchasesPerBuyer": 1,
 *   "name": "Junior",
 *   "perks": {
 *     "values": [
 *       "Multiplayer",
 *       "Multiple devices",
 *       "No ads",
 *       "Unlimited access"
 *     ]
 *   },
 *   "pricing": {
 *     "price": {
 *       "currency": "USD",
 *       "value": "10.00"
 *     },
 *     "public": true,
 *     "slug": "junior",
 *     "subscription": {
 *       "cycleCount": 12,
 *       "cycleDuration": {
 *         "count": 1,
 *         "unit": "MONTH"
 *       }
 *     },
 *     "termsAndConditions": "No sharing access with others!"
 *   }
 * }
 */ 
```

## Create a plan with optional fields (export from backend code)

```javascript
import { Permissions, webMethod } from 'wix-web-module';
import { plans } from 'wix-pricing-plans.v2';
import { elevate } from 'wix-auth';

/* Sample plan value:
 * {
 *   allowFutureStartDate: true,
 *   buyerCanCancel: true,
 *   description: 'Fun content for our younger users',
 *   formId: 'ee62cefa-bdc2-4b5d-baab-6faeef83cecb',
 *   maxPurchasesPerBuyer: 1,
 *   name: 'Junior',
 *   perks: {
 *     values: [
 *       'Multiplayer',
 *       'Multiple devices',
 *       'No ads",
 *       'Unlimited access'
 *     ]
 *   },
 *   pricing: {
 *     price: {
 *       currency: 'USD',
 *       value: '10.00"
 *     },
 *     subscription: {
 *       cycleCount: 12,
 *       cycleDuration: {
 *         count: 1,
 *         unit: 'MONTH'
 *       }
 *     }
 *   },
 *   public: true,
 *   termsAndConditions: 'No sharing access with others!'
 * }
 */

export const myCreatePlanFunction = webMethod(Permissions.Anyone, async (plan) => {
  try {
    const elevatedCreatePlan = elevate(plans.createPlan);
    const newPlan = await elevatedCreatePlan(plan);

    const planId = newPlan._id;
    const pricingObject = newPlan.pricing;
    const perks = newPlan.perks;

    return newPlan;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
});

/* Promise resolves to:
 * {
 *   "_createdDate": "2024-01-21T07:06:06.993Z",
 *   "_id": "046371ff-7e5d-4bdb-a01d-a00ff462bed8",
 *   "_updatedDate": "2024-01-21T07:06:06.993Z",
 *   "allowFutureStartDate": true,
 *   "archived": false,
 *   "buyerCanCancel": true,
 *   "description": "Fun content for our younger users",
 *   "formId": "ee62cefa-bdc2-4b5d-baab-6faeef83cecb",
 *   "hasOrders": false,
 *   "maxPurchasesPerBuyer": 1,
 *   "name": "Junior",
 *   "perks": {
 *     "values": [
 *       "Multiplayer",
 *       "Multiple devices",
 *       "No ads",
 *       "Unlimited access"
 *     ]
 *   },
 *   "pricing": {
 *     "price": {
 *       "currency": "USD",
 *       "value": "10.00"
 *     },
 *     "public": true,
 *     "slug": "junior",
 *     "subscription": {
 *       "cycleCount": 12,
 *       "cycleDuration": {
 *         "count": 1,
 *         "unit": "MONTH"
 *       }
 *     },
 *     "termsAndConditions": "No sharing access with others!"
 *   }
 * }
 */ 

```

## Create a simple plan 

```javascript
import { Permissions, webMethod } from 'wix-web-module';
import { plans } from 'wix-pricing-plans.v2';
import { elevate } from 'wix-auth';

/* Sample plan object:
 * {
 *   name: 'Default',
 *   pricing: {
 *     singlePaymentUnlimited: true
 *   }
 * }
 */

export const myCreatePlanFunction = webMethod(Permissions.Anyone, async (plan) => {
  try {
    const elevatedCreatePlan = elevate(plans.createPlan);
    const newPlan = await elevatedCreatePlan(plan);

    return newPlan;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
});

/* Promise resolves to:
 * {
 *   "_createdDate": "2024-01-16T10:32:05.818Z",
 *   "_id": "aa0d8e0e-99ad-4c95-ac48-4955e37956c5",
 *   "_updatedDate": "2024-01-16T10:32:05.818Z",
 *   "allowFutureStartDate": false,
 *   "archived": false,
 *   "buyerCanCancel": true,
 *   "description": "",
 *   "hasOrders": false,
 *   "name": "Default",
 *   "perks": {
 *     "values": []
 *   },
 *   "pricing": {
 *     "price": {
 *       "currency": "EUR",
 *       "value": "0"
 *     },
 *     "singlePaymentUnlimited": true
 *   },
 *   "primary": false,
 *   "public": true,
 *   "slug": "default"
 * }
 */
```

## Duplicate a plan 

```javascript
/*************************************
 * Backend code - plan-functions.web.js *
 *************************************/

import { Permissions, webMethod } from 'wix-web-module';
import { plans } from 'wix-pricing-plans.v2';
import { elevate } from 'wix-auth';

const elevatedListPlans = elevate(plans.listPlans);
const elevatedGetPlan = elevate(plans.getPlan);
const elevatedCreatePlan = elevate(plans.createPlan);

export const listPlans = webMethod(Permissions.Anyone, async () => {
  try {
    const response = await elevatedListPlans();
    const allPlans = response.plans;

    return allPlans;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
});

export const getPlan = webMethod(Permissions.Anyone, async (planId) => {
  try {
    const selectedPlan = await elevatedGetPlan(planId);

    return selectedPlan;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
});

export const createPlan = webMethod(Permissions.Anyone, async (plan) => {
  try {
    const newPlan = await elevatedCreatePlan(plan);

    return newPlan;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
});


/*************
 * Page code *
 *************/

import { listPlans, getPlan, createPlan } from 'backend/plan-functions.web';
$w.onReady(async function () {
  $w('#plansDropdown').disable();
  $w('#clonePlanBtn').disable();
  populatePlansDropdown();

  let selectedPlan;

  $w('#plansDropdown').onChange(async () => {
    const planId = $w('#plansDropdown').value;
    selectedPlan = await getPlan(planId);

    // Set default value for the new plan name
    $w('#planName').value = `${selectedPlan.name}-duplicate`
    $w('#clonePlanBtn').enable();
  });
  
  $w('#clonePlanBtn').onClick(async () => {
    const newPlan = selectedPlan;
    // Use inputs to set the new plan name and price
    newPlan.name = $w('#planName').value;
    newPlan.pricing.price.value = $w('#price').value;

    await createPlan(newPlan);
  });
});

async function populatePlansDropdown() {
  const plans = await listPlans();
  $w('#plansDropdown').options = plans.map((plan) => {
    return {
      label: plan.name,
      value: plan._id
    }
  });
  $w('#plansDropdown').enable();
}
```

---