'where to call onPlanPurchased( ) method after it is in events.js or does it gets automatically called?

My idea is to make a wallet collection in database which I will create manually through wix velo code. After a pricing plan is purchased by the user where to do I call onPlanPurchased( ) method to add the amount in the wallet

import wixData from 'wix-data';

export function wixPaidPlans_onPlanPurchased(event) {
  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); 
  }
}


Solution 1:[1]

The event handlers that you add to the events.js file in the backed are called automatically by Wix. So, in your case, when a plan is purchased the wixPaidPlans_onPlanPurchased() function will be called and it will receive information about the plan that was purchased in the event argument that is passed to it.

Side point: I think the event you are using is being deprecated and you should use the one from wix-pricing-plans instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam