Automatic discount for certain pricing plan members code fix

I need to apply a coupon automatically for certain pricing plans (member discount).

I know that there is an app that offers this, but they don’t quite offer what I need.

I’m not great at coding (I can do pretty basic things). I have the following code, would someone be able to tell me where the error is.

import { checkout } from 'wix-pricing-plans-backend';
import wixUsersBackend from 'wix-users-backend';

export async function myApplyCouponFunction(orderId, couponCode, userId, planId) {
  try {
    const userSubscriptions = await wixUsersBackend.getUserSubscriptions(userId);
    const subscribedPlans = userSubscriptions.items.map((item) => item.planId);
    if (subscribedPlans.includes(planId)) {
      const discountedOrder = await checkout.applyCoupon(orderId, couponCode);
      return discountedOrder;
    } else {
      throw new Error('User is not subscribed to the required plan.');
    }
  } catch (error) {
    console.error(error);
  }
}

export async function myCheckSubscriptionFunction(userId, planId) {
  try {
    const userSubscriptions = await wixUsersBackend.getUserSubscriptions(userId);
    const subscribedPlans = userSubscriptions.items.map((item) => item.planId);
    return subscribedPlans.includes(planId);
  } catch (error) {
    console.error(error);
  }
}

Can you share the error message you’re seeing?

It doesn’t actually show any errors, it just doesn’t work. I haven’t tried it any more, but the issue could be me and not placing the correct ids in the correct areas.