orders.listCurrentMemberOrders Throwing Error (MEMBER_REQUIRED) in backend

Question:
I am trying to get a list of the current member orders from the backend. The “onOrderCreated” event appears to be firing from my backend code properly. The event data appears to be correctly populated with the buyer/contact id’s…however the status is DRAFT. (Could this be the problem?). Since Wix does not have a test credit card capability… :cry:…I had to issue a 100% coupon for my testing. Anyway…once the event fires I want to get a list of the current member orders so I can cancel the old pricing plan order and just be left with the new one.

Product:
Wix Editor

What are you trying to achieve:
see above

What have you already tried:
Many syntactical variations.

Additional information:
Here is my code: (see comments for myLog results)


import { myLog } from 'backend/queries'; //writes to a my collection...works fine
import { orders } from 'wix-pricing-plans-backend';
 
export async function wixPricingPlans_onOrderCreated(event) {
var myPlan
var listedOrders

try {
   await myLog("Made it to the event function for Pricing Plans"); //written to log 
   await myLog(event) //event is populated with buyer: member and contact and other data 
             that looks correct.
   await myLog("orderId = ", event.entity._id); //is blank
   await myLog("eventId = ", event.metadata.id); //is blank
   await myLog("eventTime = ", event.metadata.eventTime); //is blank
   myPlan = event.entity.planName;
   await myLog("myPlan = " + myPlan); //populates correctly

    try {
          listedOrders = await orders.listCurrentMemberOrders();
          return listedOrders;
    } catch (error) {
          await myLog("listedOrders error = ", error); // error is blank
          await myLog(error) // error = { "details": {"applicationError": "description": "Forbidden", 
                 "code": "MEMBER_REQUIRED", "data": {} }}}
   }

} catch (error) {
         myLog("Error in onOrderPurchased: ", error.message);
}

}

Here is the event object this is passed on the firing of the onOrderCreated.
{
“metadata”: {
“id”: “87d689df-3500-47e7-931e-7ab3ba69a971”,
“entityId”: “8cb1d004-ba6a-41e8-9d02-53c84930a6ae”,
“eventTime”: “2024-01-12T19:52:41.134252074Z”,
“triggeredByAnonymizeRequest”: false
},
“entity”: {
“planPrice”: “19.99”,
“_id”: “8cb1d004-ba6a-41e8-9d02-53c84930a6ae”,
“_createdDate”: “2024-01-12T19:52:40.523Z”,
“subscriptionId”: “d385c467-3dc3-4943-afb4-a0c36cfff6b6”,
“pausePeriods”: ,
“_updatedDate”: “2024-01-12T19:52:40.523Z”,
“planName”: “EVOLVE PLAN ($19.99/mo) PaidMonthly@”,
“buyer”: {
“memberId”: “ad986dff-90f6-4fb8-bd9e-cccdd0437659”,
“contactId”: “ad986dff-90f6-4fb8-bd9e-cccdd0437659”
},
“lastPaymentStatus”: “UNPAID”,
“status”: “DRAFT”,
“orderMethod”: “UNKNOWN”,
“planDescription”: “Evolve Your Vision By Going Deeper In Understanding Your Unique Custom Ideas !”,
“wixPayOrderId”: “12cffc1a-dcf4-46bc-916f-29bccb75a962”,
“formData”: {
“submissionData”: {}
},
“statusNew”: “DRAFT”,
“autoRenewCanceled”: false,
“cycles”: [
{
“index”: 1,
“startedDate”: “2024-01-12T19:52:40.523Z”,
“endedDate”: “2024-02-12T19:52:40.523Z”
}
],
“type”: “ONLINE”,
“planId”: “342b7a73-5f24-4356-94c9-d715e66a7035”,
“startDate”: “2024-01-12T19:52:40.523Z”,
“currentCycle”: {
“index”: 1,
“startedDate”: “2024-01-12T19:52:40.523Z”,
“endedDate”: “2024-02-12T19:52:40.523Z”
},
“pricing”: {
“subscription”: {
“cycleDuration”: {
“count”: 1,
“unit”: “MONTH”
},
“cycleCount”: 0
},
“prices”: [
{
“duration”: {
“cycleFrom”: 1
},
“price”: {
“total”: “19.99”,
“proration”: “0”,
“fees”: ,
“currency”: “USD”,
“subtotal”: “19.99”,
“discount”: “0”
}
}
]
},
“priceDetails”: {
“planPrice”: “19.99”,
“subscription”: {
“cycleDuration”: {
“count”: 1,
“unit”: “MONTH”
},
“cycleCount”: 0
},
“total”: “19.99”,
“currency”: “USD”,
“subtotal”: “19.99”,
“discount”: “0”
}
}
}

Have you tried creating a separate backend function just for listing the current member orders outside of the wixPricingPlans_onOrderCreated() event?

Example

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

export async function myListCurrentMemberOrdersFunction(filters, sorting, paging) {
  try {
    const listedOrders = await orders.listCurrentMemberOrders(filters, sorting, paging);
    const firstOrderId = listedOrders.orders[0]._id;
    const firstOrderStatus = listedOrders.orders[0].status;

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

Try creating a separate function and if that works, use that function within the event. You can even modify the parameters to pass in the new pricing plan order and cancel all of the old ones.

Thomas…thank you so much for the suggestions. Yes I tried this and retried just for thoroughness. I am still getting the following error message:

“details”: {
“applicationError”: {
“description”: “Forbidden”,
“code”: “MEMBER_REQUIRED”,
“data”: {}
}
}

I appears to have problems finding the logged in user. I cannot seem to find in the documentation anywhere how to pass the logged in user to this function.

I have solved this another way, although not as eloquent…or foolproof…I put the code to clean up my subscriptions to only allow one Active Subscription on the page load of my home page…which is where the “Thank You” page redirects to after the completion of the order. Also added a note to the “Thank You” page instructing the user to click the Continue button in order to complete the transaction. This is not clean, but should work 95% of the time. The remaining 5% of the time the cleanup will be done the next time they visit the home page (at their next login).

If you need the memberId of the member who created the order, you should be able to access it using the onOrderCreated parameter via event.entity.buyer.memberId

For more details check out the onOrderCreated Parameters in the doc below:

Hi Jay

Got the same problem. The listCurrentMemberOrders doesn’t work with events.js, since the function isn’t directly called from a member, thus for some reason this error is thrown.

You’ll have to somehow find a way for your member to trigger the function from the frontend, then it should work.