如何让cancelOrder()起作用? (wix-velo API)

问题描述 投票:0回答:1

这是 API 后端参考: https://www.wix.com/velo/reference/wix-pricing-plans-backend/orders/cancelorder

我已将 Wix 定价计划安装到我的 Wix 站点(无需代码),但它并没有让我的会员可以选择取消其定期计划。

我不想根据要求手动取消他们的计划,我希望根据要求进行编码以立即处理它(例如按钮的

onClick
)。

链接的参考文献给出了这个例子:

立即取消订单,绕过权限检查

import { orders } from 'wix-pricing-plans-backend';

/* Sample orderId value: 'a8c4a1b2-b5e8-4b33-9693-057ec93e9a27'
 * 
 * Sample effectiveAt value: 'IMMEDIATELY'
 * 
 * Sample options value:
 * {
 *   suppressAuth: true
 * }
 */

export async function myCancelOrderWithOptionsFunction(orderId, effectiveAt, options) {
  try {
    const order = await orders.cancelOrder(orderId, effectiveAt, options);

    return order;
  } catch (error) {
    console.error(error);
  }
}

// Returns a promise that resolves to void

我尝试过实现这一点,但我是从后端到前端代码的新手,并且不知道如何做到这一点。

我知道如何添加后端模块,但我不知道在那里添加什么代码以及在前端添加什么相应的代码。

希望大家能帮忙。

最终我需要将所有订单连接到

wixUsers.currentUser.id
,然后在这些
cancelOrder()
上运行 
orderId

函数

干杯。

javascript frontend backend velo
1个回答
0
投票

在过去的三四天里,我一直在为这个具体的实现而奋斗。我最初想在 onplanpurchased 事件上调用 cancelOrder (https://www.wix.com/velo/reference/wix-pricing-plans-backend/events/onplanpurchased)。这对我不起作用。相反,我在订阅页面上启动了一个自定义流程,当 URL 包含带有订单详细信息的查询字符串时,该流程将启动在线购买。我使用 checkout.startOnlinePurchase(myquerystringplanid) 开始使用用户选择的定价计划进行结账。在这个承诺中,我获得了当前用户的有效订阅orders.memberListOrders({"orderStatus": ["ACTIVE"]})。然后,我必须按 id 过滤出当前订单,然后使用 Web 模块在后端调用 cancelOrder。以下是代码片段:

// FRONT END CODE (Subscribe page inside $w.onReady)
import { checkout } from 'wix-pricing-plans-frontend';
import { orders } from 'wix-pricing-plans.v2';
import { removeAllExistingSub } from "backend/backend-cancel-all-current.web";
checkout.startOnlinePurchase(wixLocation.query.planid).then((checkoutdata) => {

            //console.log(checkoutdata)
            const checkOrderStatus = []
            const orderStatus = {
                orderStatuses: checkOrderStatus
            }
            orderStatus.orderStatuses.push("ACTIVE")
            orders.memberListOrders(orderStatus).then((order) => {
                var newOrder = order.orders.filter((curor) => curor._id !== checkoutdata.order._id)
                newOrder.forEach((ordertocancel) => {
                    console.log(ordertocancel)
                    removeAllExistingSub(ordertocancel)

                })
            })

        }).catch((err) => {
            console.log(err)
        })
// BACKEND FUNCTION CALL
import { Permissions, webMethod } from 'wix-web-module';
import { orders } from 'wix-pricing-plans.v2';
import { elevate } from 'wix-auth';
const elevatedCancelOrder = elevate(orders.cancelOrder);
export const removeAllExistingSub = webMethod(Permissions.Anyone,
    (subid) => {
        //let effectivenow = "IMMEDIATELY"
        console.log(subid)
        // The 2nd argument may complain that string "IMMEDIATELY" is not a valid type. The implementation still works despite this error. I opened a Velo ticket regarding this
        elevatedCancelOrder(subid._id, "IMMEDIATELY").catch((err) => {
            console.log("err cancelling")
            console.log(err)
        })
    });

请告诉我这是否有帮助。如有任何问题,请直接向我发送电子邮件:[电子邮件受保护]

© www.soinside.com 2019 - 2024. All rights reserved.