adding customer code to OnPlanPurchase

I am trying to do a subscription and wondering if onPlanPurchased - Velo API Reference - Wix.com will work? I’d like to have multiple tiers of subscription, and instead of RSVP . I’d like to assign a CODE to the subscription plan that is assigned to the user name/account.

I put the onplan code into events.js, but how do I get the next code from my database to assign to the next customer?

import wixData from ‘wix-data’ ;

$w . onReady ( async function () {
let RESULTS ;
//console.log("RESULTS: ", RESULTS);
wixData . query ( “Diamond” )
. find ()
. then (( res ) => {
RESULTS = res . items
console . log ( "RESULTS: " , RESULTS );

    }) 
    . catch (( err ) => {  console . log ( err ); }); 

});

function checkData () {
wixData . query ( “Diamond” )
. find ()
. then (( res ) => { console . log ( res . items ) })
. catch (( err ) => { console . log ( err ); });
}

// Place this code in the events.js file
// of your site’s Backend section.

// For inserting data about a plan transaction:
import wixData from ‘wix-data’ ;

export function wixPaidPlans_onPlanPurchased ( event ) {

// Insert a title reflecting the type of transaction, and
// the event’s order object (json) into
// the collection’s data field.
if ( event . order . price . amount === 0 ) {
let orderData = {
“title” : “Free plan purchased” ,
“data” : event . order
};
wixData . insert ( “planEvents” , orderData );
} else {
let orderData = {
“title” : “Regular plan purchased” ,
“data” : event . order
};
wixData . insert ( “planEvents” , orderData );
}
}

/* Event object for a free, one-month purchase, ordered using Thailand baht currency:
*

  • When the purchase is free, the wixPayOrderId is blank,
  • the price.amount is 0, and the paymentStatus is marked PAID.
  • {
  • “order”:{
  •  "paymentStatus":"PAID", 
    
  •  "validUntil":"2019-09-12T05:43:53.246Z", 
    
  •  "price":{   
    
  •     "currency":"THB", 
    
  •     "amount":0 
    
  •  }, 
    
  •  "cancellationReason":"CANCELLATION_REASON_UNDEFINED", 
    
  •  "validFrom":"2019-08-12T05:43:53.246Z", 
    
  •  "planName":"valid 1 week", 
    
  •  "wixPayOrderId":"", 
    
  •  "recurring":false, 
    
  •  "id":"b8401bab-8e5d-4bf6-944b-b2d56698d4c9", 
    
  •  "dateCreated":"2019-08-12T05:43:53.246Z", 
    
  •  "status":"ACTIVE", 
    
  •  "roleId":"", 
    
  •  "planDescription":"Platinum Plan", 
    
  •  "memberId":"42d90dcb-b9ad-47be-9a36-488be3dec679", 
    
  •  "orderType":"ONLINE", 
    
  •  "planId":"a52f41cc-8129-4812-9e1c-fafa2807a25d", 
    
  •  "validFor":{   
    
  •     "forever":false, 
    
  •     "period":{   
    
  •        "amount":1, 
    
  •        "unit":"MONTH" 
    
  •     } 
    
  •  } 
    
  • }
  • }
  • Event object for a purchase that is valid until the user cancels:
  • When the purchase is valid until the user cancels, validFor.forever is true, and
  • validFor.forever.period.amount is 0.
  • {
  • “order”:{
  •  "paymentStatus":"PAID", 
    
  •  "validUntil":"2019-09-12T05:43:53.246Z", 
    
  •  "price":{   
    
  •     "currency":"USD", 
    
  •     "amount":0 
    
  •  }, 
    
  •  "cancellationReason":"CANCELLATION_REASON_UNDEFINED", 
    
  •  "validFrom":"2019-08-12T05:43:53.246Z", 
    
  •  "planName":"valid 1 week", 
    
  •  "wixPayOrderId":"a52f41cc-8129-4812-9e1c-fafa2807a25d", 
    
  •  "recurring":false, 
    
  •  "id":"b8401bab-8e5d-4bf6-944b-b2d56698d4c9", 
    
  •  "dateCreated":"2019-08-12T05:43:53.246Z", 
    
  •  "status":"ACTIVE", 
    
  •  "roleId":"", 
    
  •  "planDescription":"Gold Plan", 
    
  •  "memberId":"42d90dcb-b9ad-47be-9a36-488be3dec679", 
    
  •  "orderType":"ONLINE", 
    
  •  "planId":"a52f41cc-8129-4812-9e1c-fafa2807a25d", 
    
  •  "validFor":{   
    
  •     "forever":true, 
    
  •     "period":{   
    
  •        "amount":0, 
    
  •        "unit":"MONTH" 
    
  •     } 
    
  •  } 
    
  • }
  • }

*/

Attachments area

What you are trying to do exactly? If you are just copying and pasting API references examples it will not work always these are just examples that you copied.

If you just want to create multiple plans with different features you don’t need code you can do it with Wix Paid Plans app.

onPlanPurchased( )
This event is triggering when someone buys a paid plan. How you can use it here is an example:

Let say you are telling to your customers that if they buy the “Diamond” plan they will be able to see some private pages on your website. So when someone buys a paid plan you can check if it’s a Diamond plan or not and after that, you can give them permission to let them see the private pages on your site.

However, you can do all these things from your Wix dashboard to but that was an example. That is what you can do with onPlanPurchased() event.

Thanks for reply… I am totally new, so I wanted to replicate what was available before changing things. After scanning the info below, If WixPaid Plans App is still the route to go, please let me know.

My goal: have a subscription service that can be bought online, and when the item is purchased, a Thank you note appears (and is sent to them) with the next custom-identifier (my HEX number uploaded). That HEX number is an access key to my web-app.

When I download customer orders for the week/month, I would like to know which HEX number was assigned to Which customer. The reason is that the monthly subscription each month creates a new order for that particular customer, and so I’d like to know when it is renewed/paid each month, that particular person and their HEX code have paid.

First, I wanted to see if I had a list of custom codes generated from excel and uploaded into Wix DB, could I get access to the data. I wasn’t sure where the data needed to go, or what the code needed to look like. So I passed and got it to work

I will change the RESULTS column to HEX and upload my HEX numbers

from the Wix people: If you want for the data to be inserted in a collection once the plan is purchased, then it would not work with your current code, since backend events (onPlanPurchased) do not work in the frontend. It is a server call, thus, should be written in the events.js backend file, like all the other events on you website. Read more about managing your website events here .

I’m trying to figure out the next steps as I unfortunately am not walking, but crawling through is. Actually, not even crawling… figuring out how to crawl.

This is what I’m going to do next… Please correct me if I am wrong
Velo Backend Events | Help Center | Wix.com

First of all yes you need to create a JS file called as events.js (not .jsw .js) all events should be inside this file. You can call a function from an other backend file which is .jsw.

Read carefully and try to understand now:

First you want to give access to your users (to your web-app) when they buy a paid plan. (You need to install pricing plan app from Wix App Market if you installed it there is no problem) And you want to give them a access with a HEX code (like a key of the door).

Also you want to see a dashboard which will gonna show you which customer has which HEX code and other details about it.

  1. An automated event that will send a HEX code to customer when customer buy a plan from your website.

  2. A dashboard that you can see the details about the database with some filters to make it easier to use.

Let’s clear it you want to do this at least I guess right?

For excel thing if you want to import you data from a local file you need a csv file. Not excel file.

If you are very new into coding this can be hard for you and probably you will do some mistakes like I did and like I’m and everyone else doing but don’t worry it’s not the end of the world.

Contact me from this email (aydontloss@exweiv.com) and let’s do it.