I can't make the Function purchaseplan() work

I wanted to make a really simple button, in a custom Plans page, that allows the client to go through the payment just like the pricing plans standard page. I added the function but it seems that it can’t read the Plan ID.

I’ve tried a lot of things, I came back to the standard code to see if anyone actually has a more simple solution. I just want it to read the ID of the plan so I can “copy and paste” the same code in other buttons.

try wrapping the planId in quotes like so:

let planId = "ccbb1c91-e273-48b7-aa27-bf6f656a86b3"

Thanks for the reply, strange that I did that but only know stopped givin the red erro beneath it.

So now I executed the code and I’m trying to use the button and keeps givin the error: " Error: Failed to fetch" on the console.log

any tips?

  1. Your plan is a non-free-plan?
  2. You typed in correct ID ?
  3. The plan does exist?

1 - Yes / for 99
2- Yes / I got it from the PaidPlans Collection
3 - Yes / The only active

You’ve got your onClick event handler confused. You can either put your onClick code in the page’s onReady(), or as a separate function (outside of the onReady). But not both, and not one inside of the other. See Where do I put my code for an explanation.

Your code should look something like this:

import wixPaidPlans from 'wix-paid-plans';

$w.onReady(function () {

});

export function myPurchaseButton_click(event) {
    let planId = '< plan ID for a non-free plan >';
    wixPaidPlans.purchasePlan(planId)
        .then((myPurchase) => {
            let myOrderId = myPurchase.orderId;
            let myWixPayOrderId = myPurchase.wixPayOrderId;
            let myWixPayStatus = myPurchase.wixPayStatus;
        });
}

The above shows the onClick in a separate function as the sample snippet in the purchasePlan() API illustrates.