Can someone help me set up backend functions? I can't get the 'onPlanPurchased' function or scheduled jobs to run

There are two scenarios where I am trying to use a backend function but no matter how many different combinations of code or forum responses I read I am losing. The ActivityLog is a database I have set up.

(1) After someone purchases a plan, I want to record the purchase. In the front end I have the following (which works fine as the automated confirmation email is triggered)

import wixPaidPlans from'wix-paid-plans';
import wixData from'wix-data';
import wixPay from 'wix-paid-plans';

export function ConfirmationButton_click(event) {
    wixPaidPlans.purchasePlan(selectedPlan._id) // selectedPlan specified earlier
          .catch((err) => {
            console.log(err);
        });
}

This successfully purchases the plan but never triggers the ‘onPlanPurchased’ function in the events.js file in the backend where I have:

import wixPaidPlans from 'wix-paid-plans';
import wixData from 'wix-data';

export function wixPaidPlans_onPlanPurchased(event) {
 let purchaseData = event.order;
 wixData.insert("ActivityLog", purchaseData);
 }

(2) I would like to use scheduled jobs for a variety of things, but can even get a test function to work. I have set up the job in the jobs.config file in two separate ways, unsure if the .jsw or .js file type was causing the issue:

{
 "jobs": [ // Define up to 20 different jobs
 // Choose one of the options below to define the job execution interval:

     {
 "functionLocation": "/TimedJobs.jsw", // Relatively to Backend folder, started by slash
 "functionName": "testFunction",
 "description": "Test",
 "executionConfig": {
 "time": "20:04", 
      }
    },

       {
 "functionLocation": "/events.js", // Relatively to Backend folder, started by slash
 "functionName": "testFunction",
 "description": "Test",
 "executionConfig": {
 "time": "20:04", 
      }
    }
    ]
}

I then have the same testFunction code in a TimedJobs.jsw file and an events.js file both in the backend section which read as follows:

import wixData from 'wix-data';

export function testFunction() {
 let activityDetails = { "title": "test" }
 
    wixData.insert("ActivityLog", activityDetails)
    
    }

Hope that is clear. Any help on either of these issues would be greatly appreciated. Thanks in advance :slight_smile:

Hello.

I had a look at your code and couldn’t spot any issues. I suggest that you use Site Monitoring to debug the issue.

https://support.wix.com/en/article/corvid-generating-a-site-monitoring-event-to-debug-your-site

Just open the Site Monitoring tab on your site’s dashboard and try purchasing a paid plan from your site. This way, you can easily see if the backend events are firing and in case of an error you can see and informative error message.

Do the same for the Job scheduler. Job scheduler is better tested cron expression since they can allow you to schedule for a period as shot as 1 hour.

Good luck!

Hi Sam,

Thanks for this. I didn’t know about the monitoring tool, so that is helpful. The events are not firing, which is what I was hoping you could help me resolve.

Maybe a silly question, but is there a setting or something I need to enable? I have been testing on a published site but maybe there is something else?

It’s pretty crucial for my operation so would really appreciate understanding if there is a problem my side or if this is something the Wix team can look into?

Hey RSA, how do you know that " never triggers the ‘onPlanPurchased’ function"? You should put a console.log(‘event triggered’) statement at the beginning of the event handler function. The console.log() will display in the monitoring tool which is super helpful. You’ll at least know the the event was triggered or not.

Perhaps you’re assuming it wasn’t triggered because nothing was saved to the database collection. In that case, make sure that you have the correct collection permissions . Also, be aware that you can use the suppressAuth option in insert() in the Backend code to override the permissions.

I do have this issue : https://www.wix.com/velo/forum/community-discussion/how-to-trigger-when-a-user-order-a-plan