Roles & Stores

import wixStores from 'wix-stores-backend';

import wixPaidPlans from 'wix-paid-plans';

import wixUsersBackend from 'wix-users-backend';

import {roles} from 'wix-users-backend';

 

export async function wixStores_onOrderPaid(event) {

 

// WixStores Product IDs

const productidbasico = "58d4eb34-...-c14e0d2c0619"

const productidcontrole = "72dc9d0f-...-5e29c1921a9a"

const productidenterprise = "746453d4-...-f1224d2bb65e"

 

// Member Role IDs

const RoleIdBasico = "6d648993-...-373e365940eb"

const RoleIdControle = "0b661d48-...-6ff18dc89f22"

const RoleIdEnterprise = "fdc0c708-...-e14ef9fe4b6f"

 

const memberId = event.buyerInfo.id;

const productId = event.lineItems[0].productId;

 

if (productId === productidbasico) {

let roleId = RoleIdBasico

assignRole(roleId, memberId)

}

 

if (productId === productidcontrole) {

let roleId = RoleIdControle

assignRole(roleId, memberId)

}

 

if (productId === productidenterprise) {

let roleId = RoleIdEnterprise

assignRole(roleId, memberId)

}

}

 

export async function assignRole(planId, memberId) {

 

// Plan IDs

const planIdBasico = "ef0fca3b-...-4e88e82add7b"

const planIdControle = "dd49daff-...-6ba743da6c2d"

const planIdEnterprise = "dcb2b8f3-...-b7c21bc2d1e7"

 

function planRole(pId){

if (pId === planIdBasico) {

let RoleIdBasico = planIdBasico

return RoleIdBasico

}

if (pId === planIdControle) {

let RoleIdControle = planIdControle

return RoleIdControle

}

if (pId === planIdEnterprise) {

let RoleIdEnterprise = planIdEnterprise

return RoleIdEnterprise

}

}

 

let options = {

"suppressAuth": true,

"suppressHooks": true

};

 

let roleId = await planRole(planId)

 

return roles.assignRole(roleId, memberId, options)

.then( () => {

return true

})

.catch((error) => {

return error

});

}