Pricing Plan Update Error

I am trying to change the price of Pricing Plan in Wix Studio. Very simple code:

Back-end Code:

import { Permissions, webMethod } from 'wix-web-module';
import wixPricingPlansBackend from 'wix-pricing-plans-backend';

export const myUpdatePlanFunction = webMethod(Permissions.Anyone, async (planInfo) => {
  console.log("Received planInfo in backend: ", planInfo)
  let updatedPlan = await wixPricingPlansBackend.updatePlan(planInfo);
  return updatedPlan
});

Front-end Code:

import wixWindowFrontend from 'wix-window-frontend';
import wixData from 'wix-data';
import { myGetPlanFunction } from 'backend/changePlanPrice.web.js';
import { myUpdatePlanFunction } from 'backend/changePlanPrice.web.js';

export async function changeButton_click(event) {
        let newPrice = $w('#priceInput').value
        const pricePlan2 = {
            "_id": "7b8baa74-14ef-4a73-b6f9-8bdffb9bacfa",
            "pricing": {
                "price": {
                    "value": newPrice
                },
            },
        }

    myUpdatePlanFunction(pricePlan2)
        .then(plan => {
            // plan updated
            console.log("Plan: ",plan)
        })
        .catch((error) => {
            // plan not updated 
            const updateError = error;
        });
}

The backend wixPricingPlansBackend.updatePlan is failing with limited error info - it just says FORBIDDEN. I am the logged in admin of the site so shouldn’t be a permissions issue (as I have “Managing Pricing Plans” permissions as the owner)…or am I missing something else here in how I’ve formatted the object. I’m puzzled.

The object is getting to the back-end, as the console.log works, but then immediately fails in both preview and on the live site.

Screenshot 2024-05-24 at 12.30.23

According to the API, you only need the _id and the fields requiring update, but while troubleshooting, I built the whole object by pulling it down in the backend and modifying to remove fields that aren’t in the API, but I get the same error then.

Can anyone help?

Simon

Try converting newPrice to a string with 2 decimal formatting before assigning it as the value of the price?

If you $w('#priceInput').value is a number field, it might result as an integer and the doc states that the price should be a string.

The AI in the IDE figured this out for me. I had to elevate the permissions for this function, despite the API not calling out the need.

export const myUpdatePlanFunction = webMethod(Permissions.Admin, async (planInfo) => {
  console.log("Received planInfo in backend: ", planInfo)
  const elevateUpdatePlan = elevate(wixPricingPlansBackend.updatePlan);
  let updatedPlan = await elevateUpdatePlan(planInfo);
  return updatedPlan
});

Doesn’t appear to be called out in the API…

Simon.

Thanks but I had already tried this. Turned out to be permissions related and when I elevated the permissions, it works fine. Needs to be flagged in the API though.

1 Like