Search.../

onPlanPurchased( )

An event that triggers when a member purchases a plan.

Description

The onPlanPurchased() event handler runs when a member purchases a pricing plan. The received PlanPurchasedEvent object contains order information about the plan, such as order ID, until when the order is valid, and so on.

Note: Backend events don't work when previewing your site.

Syntax

function onPlanPurchased(event: PlanPurchasedEvent): void

onPlanPurchased Parameters

NAME
TYPE
DESCRIPTION
event
PlanPurchasedEvent

The purchase data.

Returns

This function does not return anything.

Return Type:

void

Was this helpful?

An event when a pricing plan is purchased

This example adds purchased plan event data to a manually-created planEvents database. The database has a data field.

When a plan is purchased and the onPlanPurchased event is triggered, the JSON format of the object is inserted into the data field.

Copy Code
1// Place this code in the events.js file
2// of your site's Backend section.
3// Add the file if it doesn't exist.
4
5// For inserting data about a plan transaction:
6import wixData from 'wix-data';
7
8export function wixPricingPlans_onPlanPurchased(event) {
9
10 // Insert a title reflecting the type of transaction, and
11 // the event's order object (json) into
12 // the collection's data field.
13 if (event.order.price.amount === 0) {
14 let orderData = {
15 "title": "Free plan purchased",
16 "data": event.order
17 };
18 wixData.insert("planEvents", orderData);
19 } else {
20 let orderData = {
21 "title": "Regular plan purchased",
22 "data": event.order
23 };
24 wixData.insert("planEvents", orderData);
25 }
26}
27
28
29/* Event object for a free, one-month purchase, ordered using Thailand baht currency:
30*
31* When the purchase is free, the `wixPayOrderId` is blank,
32* the `price.amount` is 0, and the paymentStatus is marked `PAID`.
33*
34* {
35* "order":{
36* "paymentStatus":"PAID",
37* "validUntil":"2019-09-12T05:43:53.246Z",
38* "price":{
39* "currency":"THB",
40* "amount":0
41* },
42* "validFrom":"2019-08-12T05:43:53.246Z",
43* "planName":"valid 1 week",
44* "wixPayOrderId":"",
45* "recurring":false,
46* "id":"b8401bab-8e5d-4bf6-944b-b2d56698d4c9",
47* "dateCreated":"2019-08-12T05:43:53.246Z",
48* "status":"ACTIVE",
49* "roleId":"",
50* "planDescription":"Platinum Plan",
51* "memberId":"42d90dcb-b9ad-47be-9a36-488be3dec679",
52* "orderType":"ONLINE",
53* "planId":"a52f41cc-8129-4812-9e1c-fafa2807a25d",
54* "validFor":{
55* "forever":false,
56* "period":{
57* "amount":1,
58* "unit":"MONTH"
59* }
60* }
61* }
62* }
63*
64*
65* Event object for a purchase that is valid until the user cancels:
66*
67* When the purchase is valid until the user cancels, `validFor.forever` is true, and
68* `validFor.forever.period.amount` is 0.
69*
70* {
71* "order":{
72* "paymentStatus":"PAID",
73* "validUntil":"2019-09-12T05:43:53.246Z",
74* "price":{
75* "currency":"USD",
76* "amount":0
77* },
78* "cancellationReason":"CANCELLATION_REASON_UNDEFINED",
79* "validFrom":"2019-08-12T05:43:53.246Z",
80* "planName":"valid 1 week",
81* "wixPayOrderId":"a52f41cc-8129-4812-9e1c-fafa2807a25d",
82* "recurring":false,
83* "id":"b8401bab-8e5d-4bf6-944b-b2d56698d4c9",
84* "dateCreated":"2019-08-12T05:43:53.246Z",
85* "status":"ACTIVE",
86* "roleId":"",
87* "planDescription":"Gold Plan",
88* "memberId":"42d90dcb-b9ad-47be-9a36-488be3dec679",
89* "orderType":"ONLINE",
90* "planId":"a52f41cc-8129-4812-9e1c-fafa2807a25d",
91* "validFor":{
92* "forever":true,
93* "period":{
94* "amount":0,
95* "unit":"MONTH"
96* }
97* }
98* }
99* }
100*
101*/