Copying the code I used on one of my sites. You need to add the event handler to the backend as well. Naturally you'll need to change the planIDs and page urls plus the image (#group4) that shows during the plan purchase confirmation.
This works but the same code does not work on another site. Maybe @Yisrael (Wix) might give us some insight.
// API Reference: https://www.wix.com/velo/reference/api-overview/introduction
// “Hello, World!” Example: https://learn-code.wix.com/en/article/1-hello-world
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import wixUsers from 'wix-users';
function getUTCTime() {
var utcTime = new Date()
utcTime.setMinutes(utcTime.getMinutes() + utcTime.getTimezoneOffset())
console.log(utcTime)
return utcTime
}
$w.onReady(async function () {
let date = new Date()
console.log(date)
let newDate = date.setMinutes(date.getMinutes() - 1);
console.log(date)
wixLocation.onChange(async (location) => {
let newPath = location.path;
if (newPath.includes("status")) {
// wixLocation.to("/paid-plan-purchase");
$w('#group4').show()
$w('#membershipPlanPickerTpa1').hide()
let user = wixUsers.currentUser;
let userId = user.id;
await wait(4000);
let date = await getUTCTime()
console.log(date)
let newDate = date.setMinutes(date.getMinutes() - 1);
console.log(date)
wixData.query("PaidPlanPurchases")
.eq("paymentStatus", "PAID")
.eq("memberId", userId)
//.ge("_createdDate", newDate)
.find()
.then((results) => {
if (results.items.length > 0) {
console.log("Found")
let firstItem = results.items[0];
console.log(firstItem)
let plan = firstItem.planID
if (plan === "41e4fcbc-9ef9-4a21-adf9-46c5913638a9") {
wixLocation.to("https://www.mattsoule.com/account/free-membership");
} else if (plan === "bfea41d3-a4bf-4f19-aec6-95346d3830fb") {
wixLocation.to("https://www.mattsoule.com/account/master-stress-membership");
}
} else {
console.log("NotFound")
$w('#group4').hide()
$w('#membershipPlanPickerTpa1').show()
// $w('#text108').show()
// $w('#button1').show()
}
})
.catch((err) => {
let errorMsg = err;
});
}
});
});
function wait(ms = 500) {
return new Promise((res) => {
setTimeout(() => { res("done") }, ms);
});
}
===Backend js.===
import wixData from 'wix-data';
export function wixPaidPlans_onPlanPurchased(event) {
console.log("Triggered")
console.log(event)
let plan = event.order.planId
let toInsert = {
"planID": event.order.planId,
"memberId": event.order.memberId,
"paymentStatus": event.order.paymentStatus,
};
wixData.insert("PaidPlanPurchases", toInsert)
.then((results) => {
let item = results;
console.log(item)
console.log("Inserted to Database")
})
.catch((err) => {
let errorMsg = err;
console.log("Error Inserted to Database")
console.log(errorMsg)
});
}
export function test() {
var utcTime = new Date()
utcTime.setMinutes(utcTime.getMinutes() + utcTime.getTimezoneOffset())
console.log(utcTime)
return utcTime
}