Paid Plans - onPlanPurchased

Hi all,
I’m using the Wix Paid Plans and I need to call an external API in order to automate the purchase process.

How do I do that ?

From what I understood, I need to catch an onPlanPurchased event and call a fetch function to to my POST.

Tried to add it in “events.js” in the Backend section.
Tried to add it to the site code and also to the “Plans & Pricing” page code.

Nothing seem to work.

Please advise on how to do it correctly.

Thanks,
Alex

You can see more about the Wix Paid Plans API here.
https://www.wix.com/corvid/reference/wix-paid-plans.html
https://www.wix.com/corvid/reference/wix-paid-plans-backend.html

Note that backend events don’t work when previewing your site.

As for working with Wix Fetch and Wix HTTP Functions, then you can see more about that here.

https://www.wix.com/corvid/reference/wix-fetch.html
https://support.wix.com/en/article/corvid-accessing-third-party-services-with-the-fetch-api

https://www.wix.com/corvid/reference/wix-http-functions.html
https://support.wix.com/en/article/corvid-exposing-a-site-api-with-http-functions
How to Use HTTP Functions to Expose Your Site’s API | Corvid by Wix

Finally, see this Wix example about exposing a site.
https://www.wix.com/corvid/forum/community-discussion/example-myapi-and-myapiclient

My POST works.
My problem is that I can’t catch this event.
When I try to catch a different event, such as, membershipPlanPickerTpa1_viewportEnter, it works and my POST works.

I implemented the wixPaidPlans_onPlanPurchased(event) and it never stops there.

I put it on the ‘pick a plan’ page.
Where should I put this function ?

If you want to be using Wix Paid Plans events, then it needs to be all in a events.js in your backend as stated in the Wix Paid Plans Backend API Reference.
https://www.wix.com/corvid/reference/wix-paid-plans-backend.Events.html

To add a paid plan event handler, add an events.js file to the Backend section of your site if one does not already exist. All event handler functions for your site are defined in this file.

Note that backend events don’t work when previewing your site.

https://www.wix.com/corvid/reference/wix-paid-plans-backend.Events.html#onPlanPurchased

Examples

An event when a paid plan is purchased

This example adds purchased plan event data to a manually-created planEvents database. The database has a data field.

When a plan is purchased and the onPlanPurchased event is triggered, the JSON format of the object is inserted into the data field.

// Place this code in the events.js file
// of your site's Backend section.


// For inserting data about a plan transaction:
import wixData from 'wix-data';



export function wixPaidPlans_onPlanPurchased(event) {

  // Insert a title reflecting the type of transaction, and
  // the event's order object (json) into 
  // the collection's data field.
  if (event.order.price.amount === 0) {
     let orderData = {
       "title": "Free plan purchased",
       "data": event.order
     };
     wixData.insert("planEvents", orderData);
  } else {
     let orderData = {
       "title": "Regular plan purchased",
       "data": event.order
     };
     wixData.insert("planEvents", orderData); 
  }
}


/*  Event object for a free, one-month purchase, ordered using Thailand baht currency:
*
*   When the purchase is free, the `wixPayOrderId` is blank,
*   the `price.amount` is 0, and the paymentStatus is marked `PAID`. 
*
*   {  
*    "order":{  
*      "paymentStatus":"PAID",
*      "validUntil":"2019-09-12T05:43:53.246Z",
*      "price":{  
*         "currency":"THB",
*         "amount":0
*      },
*      "cancellationReason":"CANCELLATION_REASON_UNDEFINED",
*      "validFrom":"2019-08-12T05:43:53.246Z",
*      "planName":"valid 1 week",
*      "wixPayOrderId":"",
*      "recurring":false,
*      "id":"b8401bab-8e5d-4bf6-944b-b2d56698d4c9",
*      "dateCreated":"2019-08-12T05:43:53.246Z",
*      "status":"ACTIVE",
*      "roleId":"",
*      "planDescription":"Platinum Plan",
*      "memberId":"42d90dcb-b9ad-47be-9a36-488be3dec679",
*      "orderType":"ONLINE",
*      "planId":"a52f41cc-8129-4812-9e1c-fafa2807a25d",
*      "validFor":{  
*         "forever":false,
*         "period":{  
*            "amount":1,
*            "unit":"MONTH"
*         }
*      }
*   }
*  }
*
*
*  Event object for a purchase that is valid until the user cancels:
*
*   When the purchase is valid until the user cancels, `validFor.forever` is true, and 
*   `validFor.forever.period.amount` is 0.
*
*   {  
*    "order":{  
*      "paymentStatus":"PAID",
*      "validUntil":"2019-09-12T05:43:53.246Z",
*      "price":{  
*         "currency":"USD",
*         "amount":0
*      },
*      "cancellationReason":"CANCELLATION_REASON_UNDEFINED",
*      "validFrom":"2019-08-12T05:43:53.246Z",
*      "planName":"valid 1 week",
*      "wixPayOrderId":"a52f41cc-8129-4812-9e1c-fafa2807a25d",
*      "recurring":false,
*      "id":"b8401bab-8e5d-4bf6-944b-b2d56698d4c9",
*      "dateCreated":"2019-08-12T05:43:53.246Z",
*      "status":"ACTIVE",
*      "roleId":"",
*      "planDescription":"Gold Plan",
*      "memberId":"42d90dcb-b9ad-47be-9a36-488be3dec679",
*      "orderType":"ONLINE",
*      "planId":"a52f41cc-8129-4812-9e1c-fafa2807a25d",
*      "validFor":{  
*         "forever":true,
*         "period":{  
*            "amount":0,
*            "unit":"MONTH"
*         }
*      }
*   }
* }
*
*/

To use a third party API then you will need to expose your site to it as already mentioned in previous post and pass the returned JSON to your external API.

Plus, looking at your post, please note that viewportEnter is from the onViewport event handler function along with onViewportLeave as shown here.
https://www.wix.com/corvid/reference/$w.ViewportMixin.html
https://www.wix.com/corvid/reference/$w.Element.html#onViewportLeave
https://www.wix.com/corvid/reference/$w.Element.html#onViewportEnter

These are event handler functions which you add from the properties panel for that element or put it all in your code on your page and there are others like…

onMouse
https://www.wix.com/corvid/reference/$w.Element.html#onMouseIn
https://www.wix.com/corvid/reference/$w.Element.html#onMouseOut

onClick
https://www.wix.com/corvid/reference/$w.ClickableMixin.html

These event handler functions are not the same as events like the onPlanPurchased event from the Wix Paid Plans Backend API.

You can see more about working with event handler functions in these tutorials and pages here.
https://support.wix.com/en/article/corvid-working-with-the-properties-panel
https://support.wix.com/en/article/corvid-reacting-to-user-actions-using-events
https://support.wix.com/en/article/corvid-tutorial-adding-custom-interactivity-with-events
https://support.wix.com/en/article/how-to-change-the-text-label-of-a-button-with-events

Thank you for the answers but it still not working.

I implemented wixPaidPlans_onPlanPurchased(event) and put it in events.js and it never reaches there :frowning:

I even copy pasted the code you put here and it still doesn’t work.

How can I debug it ?

Anyone ?

I am exactly the same problem - event happens but the wixPaidPlans_onPlanPurchased(event) function is never triggered.

Did you find a solution?

I am having the same problem. I have followed this tutorial.

https://support.wix.com/en/article/corvid-tutorial-using-the-paid-plans-api-for-pricing-plan-ordering-and-payment

I have created an event.js file, added payment code, and I have tried orderPlan() and purchasePlan() (not in preview) and the wixPaidPlans_onPlanPurchased doesn’t seem to trigger.

Same problem here.

Anyone got this working?

Have you tried creating your own wixPaidPlans_onPlanPurchased(event) rather than pasting the reference material? How is the event triggered from the subscription page?

hi - i did it and it’s working great. my way to debug the events.js function was to console.log the event and view the output in the site monitoring page or (stackdriver).
didn’t find any issue, worked easly on two of my sites…

Can you please include the code here? I’ve just been struggling with this for quite a bit. Also, for the plans, I used the default template and whatnot for the plans, would that still fire the onPlanPurchased event?

@shimistudio Seconding what Fawwaz Hameed says. Could you share your code, please?

@hameedfawwaz sorry for not responding, didn’t got a notification on your comment…

here is the code, and output example:

export function wixPaidPlans_onPlanPurchased(event) {
    console.log("wixPaidPlans_onPlanPurchased:", event);
// ...
}

you can view your log in Google Operations (formerly Stackdriver).
connect it in your dashboard under “Developer tools” => “logs”

// log example:


[
   "wixPaidPlans_onPlanPurchased:",
   {
      "order":{
         "id":"f2be89bf-dc7d-4d6e-8b36-19739730214b",
         "planId":"d60b9393-d311-4ebb-bb77-4c95c764a378",
         "memberId":"9b20b2c2-697e-4129-b412-65d180ee73b6",
         "roleId":"d60b9393-d311-4ebb-bb77-4c95c764a378",
         "orderType":"OFFLINE",
         "status":"ACTIVE",
         "wixPayOrderId":"",
         "paymentStatus":"PAID",
         "price":{
            "amount":35,
            "currency":"ILS"
         },
         "planName":"במסלול חודשי",
         "planDescription":"(רק 35 ש\"ח לחודש* (למשך שנה",
         "recurring":true,
         "validFor":{
            "forever":false,
            "period":{
               "amount":12,
               "unit":"MONTH"
            }
         },
         "validFrom":"2021-03-19T09:36:50.580Z",
         "validUntil":"2022-03-19T09:36:50.580Z",
         "dateCreated":"2021-09-19T08:38:10.986Z",
         "cancellationReason":"CANCELLATION_REASON_UNDEFINED"
      }
   }
]

@rushfamilymail
@hameedfawwaz
Hi, added the code here.

@shimistudio Thank-you!