> 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`). > - 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: createOnlineOrder(planId: string, startDate: Date, couponCode: string) # Method package: wixPricingPlansBackend # Method menu location: wixPricingPlansBackend --> Checkout --> createOnlineOrder # Method Link: https://dev.wix.com/docs/velo/apis/wix-pricing-plans-backend/checkout/create-online-order.md # Method Description: Creates an order online for currently logged-in member. The `createOnlineOrder()` function returns a Promise that resolves to an `Order` object when the order has been created. The order is for the currently logged-in member. The buyer must be logged in for you to be able to create an order. Purchasing a plan: 1. Starts with creating an order. 1. For non-free plans, calling the Wix Pay [`startPayment()`](wix-pay-frontend/start-payment) function with the `wixPayOrderId` value as the `paymentId` parameter to enable your buyer to pay for the order. (Do not call the Wix Pay `createPayment()` function to create the payment ID because this `createOnlineOrder()` function does that for you.) 1. If the order is free, do not call the Wix Pay [`startPayment()`](wix-pay-frontend/start-payment) function after `createOnlineOrder()`. The order is created and considered paid for. Once an online order is paid for, the `status` of the order is no longer `"DRAFT"` and at this point, its `startDate` cannot be changed. Creating an online order causes: + The order status to be set to `"PENDING"` if the start date is in the future. Otherwise, the `status` is set to `"ACTIVE"`. The [`onOrderCreated()`](wix-pricing-plans-backend/events/on-order-created) event handler runs when an online order is created. > **Note:** > If `startDate` is not used in the function, the missing parameter has to be passed as > `undefined`, for example: `function createOnlineOrder(planId, undefined, couponCode)` # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Create an online order for a plan ```javascript import { Permissions, webMethod } from 'wix-web-module'; import { checkout } from 'wix-pricing-plans-backend'; // Sample planId value: 'aef93bb6-aaaa-4965-966e-bd68dd2a1cc2' export const myCreateOnlineOrderFunction = webMethod(Permissions.Anyone, async (planId) => { try { const order = await checkout.createOnlineOrder(planId); const orderId = order._id; const plan = order.planName; const orderType = order.type; return order; } catch (error) { console.error(error); } }); /* Promise resolves to: * * { * "_id": "73a646b3-6aff-4fc6-a18b-9e30cf725aa5", * "planId": "aef93bb6-aaaa-4965-966e-bd68dd2a1cc2", * "subscriptionId": "f713e998-8344-4791-8c54-00d578746ff4", * "wixPayOrderId": "6a5c0cfc-490b-4120-a704-c49facae1172", * "buyer": { * "memberId": "ea3d74df-b7dc-4ca1-a7c9-c416b9017a86", * "contactId": "ea3d74df-b7dc-4ca1-a7c9-c416b9017a86" * }, * "priceDetails": { * "subtotal": "2", * "discount": "0", * "total": "2", * "planPrice": "2", * "currency": "USD", * "subscription": { * "cycleDuration": { * "count": 1, * "unit": "WEEK" * }, * "cycleCount": 26 * } * }, * "pricing": { * "subscription": { * "cycleDuration": { * "count": 1, * "unit": "WEEK" * }, * "cycleCount": 26 * }, * "prices": [ * { * "duration": { * "cycleFrom": 1, * "numberOfCycles": 26 * }, * "price": { * "subtotal": "2", * "discount": "0", * "total": "2", * "currency": "USD" * } * } * ] * }, * "type": "ONLINE", * "orderMethod": "UNKNOWN", * "status": "DRAFT", * "autoRenewCanceled": false, * "lastPaymentStatus": "UNPAID", * "startDate": "2022-08-01T11:00:00.000Z", * "endDate": "2023-01-30T11:00:00.000Z", * "pausePeriods": [], * "earliestEndDate": "2023-01-30T11:00:00.000Z", * "planName": "Cooking for Kids", * "planDescription": "Get easy-to-make recipes that kids can prepare on their own!", * "planPrice": "2", * "_createdDate": "2022-07-13T04:37:11.197Z", * "_updatedDate": "2022-07-13T04:37:11.197Z" * } */ ``` ## Create an online order and purchase it with Wix Pay ```javascript /********************************** * Backend code - checkout.web.js * *********************************/ import { Permissions, webMethod } from 'wix-web-module'; import { checkout } from 'wix-pricing-plans-backend'; export const myCreateOnlineOrderFunction = webMethod(Permissions.Anyone, async (planId, startDate) => { try { const order = await checkout.createOnlineOrder(planId, startDate) return order; } catch (error) { console.error(error); // Handle the error } }); /* Returns a Promise that resolves to an * `order` object with a `"DRAFT"` * status. In the object, there is a * `wixPayOrderId` property you can use to * complete the purchase. */ /************* * Page code * *************/ import { myCreateOnlineOrderFunction } from 'backend/checkout.web'; import wixPayFrontend from 'wix-pay-frontend'; // ... const planId = $w('#planId').value; const startDate = new Date('July 22, 2022 11:00:00'); $w('#submitButton').onClick(async () => { try { const order = await myCreateOnlineOrderFunction(planId, startDate); const result = await wixPayFrontend.startPayment(order.wixPayOrderId); if (result.status === 'Successful') { console.log('Successfully Ordered'); // Handle payment success } else if (result.status === 'Failed to Order') { console.log('Failed'); // Handle payment failure } else if (result.status === 'Order Pending') { console.log('Pending'); // Handle payment pending } else if (result.status === 'Order Cancelled') { console.log('Cancelled'); // Handle user closing the payment panel // without paying } else { console.log('Failed to Create Order'); // Handle any remaining possibilities } } catch(error) { // Handle the error } }); /* Returns a Promise that resolves to * the created payment. */ ``` ## A full order plan scenario including a collection ```javascript /*********************************************** * For demonstration purposes, the * * following myCreateOnlineOrderFunction() * * triggers the onOrderCreated event. You * * can put this code in a backend jsw file * * or use a different method for triggering * * the event such as with http functions. * ***********************************************/ import { checkout } from 'wix-pricing-plans-backend'; export const myCreateOnlineOrderFunction = webMethod(Permissions.Anyone, (planId) => { return checkout.createOnlineOrder(planId); }); /******************************* * Backend code - events.js * *******************************/ import { myInsertNewOrderFunction } from 'backend/process-orders.web'; export function wixPricingPlans_onOrderCreated(event) { myInsertNewOrderFunction(event.entity); } /**************************************** * Backend code - process-orders.web.js * ***************************************/ import { Permissions, webMethod } from 'wix-web-module'; import wixData from 'wix-data'; export const myInsertOrderFunction = webMethod(Permissions.Anyone, async (order) => { const member = await getMember(order.buyer.memberId); const data = ({ _id: order.id, planId: order.planId, planName: order.planName, status: order.status, nickname: member.nickname }); wixData.insert('Orders', data) .then(() => { console.log('Inserted successfully', data); }) .catch(() => { console.error('Failed to insert', data); }); }); // Gets member details to associate with the order async function getMember(memberId) { try { return wixData.get('Members/PublicData', memberId) } catch (error) { console.error('Failed to fetch member by ID', memberId, error); }; } ``` ---