Allow a client to cancel a Plan/Membership on their own

We are working on My Plans feature that would allow to view and manage (cancel) orders for members in Members area. In the meantime, we have released Corvid API that allows to create a custom page that would show plans and cancel them.
You can read the documentation here https://www.wix.com/corvid/reference/wix-paid-plans.html#cancelOrder .
Way to allow site members to cancel their own orders:

  1. For the members to be able to cancel their orders first you’ll need to be able to show them the orders so they could choose what to cancel. Way to have orders’ data is to save it in your own DB dataset in corvid ( https://support.wix.com/en/article/creating-a-database-collection ) every time the member purchases a plan.
    E.g. of the data that can be collected and saved on every purchase of the plan:
    “planName”: plan.name ,
    “planId”: plan._id,
    “orderId”: purchaseResponse.orderId,
    “memberId”: user.id ,
    “dateCreated”: Date().toString()
    “plan” is the plan to be purchased,
    “purchaseResponse” is the response we get after calling .purchasePlan ( https://www.wix.com/corvid/reference/draft/wix-paid-plans.html#purchasePlan ) method because it’s return value is PurchaseResult ( https://www.wix.com/corvid/reference/draft/wix-paid-plans.html#PurchaseResult ) ,
    “user” - currently logged in user https://www.wix.com/corvid/reference/draft/wix-users.html#currentUser .
    Example of the code used on dynamic page of custom package picker where member can purchase a plan (notice a field called " canceled " - it is needed so you would be able to track if member canceled the order or not, it is obvious that on the purchase we need to set it to false ):
    import paidPlans from ‘wix-paid-plans’;
    import wixData from ‘wix-data’;
    import wixUsers from ‘wix-users’;
    $w.onReady( function () {
    $w(‘#dynamicDataset’).onReady( () => {
    const plan = $w(“#dynamicDataset”).getCurrentItem();
    // purchase a plan
    $w(“#button2”).onClick( (event) => {
    const purchase = paidPlans.purchasePlan(plan._id).then(purchaseResponse => {
    // get current member (who is purchasing the plan)
    let user = wixUsers.currentUser;
    // collect all the data you want to save to your collection
    let toInsert = {
    “planName”: plan.name ,
    “planId”: plan._id,
    “orderId”: purchaseResponse.orderId,
    “memberId”: user.id ,
    “canceled”: false,
    “dateCreated”: Date().toString()
    };
    // insert the data into “subscriptions” collection that you’ve created (how to https://support.wix.com/en/article/creating-a-database-collection )
    wixData.insert(“subscription”, toInsert)
    })
    });
    });
    });
  2. Once you start saving members’ orders data you’ll have the necessary information to be able to allow them to cancel their orders. Cancellation can be done calling .cancelOrder method ( https://www.wix.com/corvid/reference/draft/wix-paid-plans.html#cancelOrder ) which takes orderId (which you are saving to your DB dataset (field " “orderId”: purchaseResponse.orderId " in the previous example)).
    You can create members area page Subscriptions on your site where you’d show logged-in member’s orders:
    code example:
    import wixPaidPlans from ‘wix-paid-plans’;
    import wixData from ‘wix-data’;
    import wixUsers from ‘wix-users’;
    import wixWindow from ‘wix-window’;
    $w.onReady( function () {
    // https://www.wix.com/corvid/reference/wix-users.html#currentUser
    const user = wixUsers.currentUser;
    // filter dataset so only currently logged-in user’s orders would be visible ( https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFilter )
    $w(“#dataset1”).setFilter( wixData.filter()
    .eq(“memberId”, user.id )
    )
    .then( () => {
    $w(‘#dataset1’).onReady( () => {
    $w(“#button1”).onClick( (event) => {
    const $item = $ w.at (event.context);
    const subscription = $item(“#dataset1”).getCurrentItem();
    const orderId = subscription.orderId;
    // Cancel the specific order of the logged-in user - https://www.wix.com/corvid/reference/wix-paid-plans.html#cancelOrder
    wixPaidPlans.cancelOrder(orderId)
    .then( () => {
    // Get details for updating status of existing subscription (“subscription” dataset you’ve created before)
    const toUpdate = {
    “_id”: subscription._id,
    “planName”: subscription.planName,
    “planId”: subscription.planId,
    “orderId”: subscription.orderId,
    “memberId”: user.id ,
    “dateCreated”: subscription.dateCreated,
    “canceled”: true , // Set cancellation to true
    “dateUpdated”: Date().toString()
    };
    // Update existing subscription in collection to show that the subscription is canceled
    wixData.update(“subscription”, toUpdate);
    // OPTIONAL - Let user know that cancellation succeeded by opening some lightbox
    wixWindow.openLightbox(“planCanceled”)
    })
    // Do something in the case of error - e.g. open lightbox that says that cancellation failed
    . catch ((err) => {
    wixWindow.openLightbox(“cancelFailed”);
    });
    })
    })
    } )
    . catch ( (err) => {
    // in the case $w(“#dataset1”).setFilter(…) fails
    console.log(err);
    } );
    })
    Let me know if you will have questions at antanasd (eta) wix.com.