Here is my comprehensive PARTIAL SOLUTION to the entire problem-- NO LIVE PRICE UPDATE :
I have done everything (custom pricing based on inputs, complete payment process ) except for the live update of the displayed price (it wasn’t necessary for my application, but I could figure it out if I had a specific application). This includes a solution to your question @gmfidelcorrales :
I have a form with a checkbox group (just multiple checkboxes put together) where you can click multiple items. It is for ‘coding’ lessons.
There are two parts to this-- front end and backend.
Here is the code I use for the form page (front end)
Don’t forget to connect your submit button to this script. (look at the video tutorial at the bottom)
//importing wixPay module and a backend module which I have written
import wixPay from 'wix-pay';
import {createPaymentForProduct} from 'backend/signup_PayAPI';
//defining price variables
var PRICE_FOR_ONE_WEEK = 10.0
var PRICE_FOR_TWO_WEEKS = 17.0
//name of the checkbox group
var elemName = "#checkboxGroup1"
//function called when the submit button is clicked
export function button1_click_1(event) {
var price;
//getting the checkBox element
var checkBox = $w(elemName);
//getting data from the checkBox (how many weeks have been selected)
var weeks = checkBox.selectedIndices.length
//simple if statement for price selection
if (weeks == 1){
price = PRICE_FOR_ONE_WEEK;
} else{
price = PRICE_FOR_TWO_WEEKS;
}
//calling backend function and passing it our price data
createPaymentForProduct(price).then(payment => {
//starts payment process, opening
wixPay.startPayment(payment.id);
});
}
Code for Backend (A .jsw file I made under the Backend Folder):
Also take a look at the video tutorial for this.
//import wixPay module
import wixPay from 'wix-pay-backend';
//function called by frontend-- takes price parameter
export function createPaymentForProduct(price){
//returns a sort of payment object
return wixPay.createPayment({
items: [{name: 'Coding Lessons', price: price, quantity: 1}],
amount: price
})
}
When the user clicks the submit button, the frontend calculates the price and sends it to the backend. Then, the backend returns a payment object which the frontend ‘executes’ and a window like this will popup (it will say the price at the top, but I cropped that part out):
NOTES:
-
In general, the frontend should not compute the price-- it should pass necessary info (the CheckBox group object or how many/which parts have been checked) to the backend which then computes price. This is general programming practice and will make your code cleaner/more organized/reusable.
-
I only put calculations in the frontend because it was a super simple program (basically I just didn’t feel like putting them in the backend).
-
Instead of having variables set with the prices, you should have a database that contains prices so they will be easy for you and others to change in the future.
-
I only used a CheckBox Group in this example. To extract data from different input sources, see the Corvid API. Here is a link to TextBoxes and other input sources can be seen on this page: https://www.wix.com/corvid/reference/$w/textbox
-
Sorry for the loose use of semicolons-- I ignored a number of programming conventions due to the simplicity of this script.
-
If you need Help with any part of this process (including updating a live display), let me know and I’ll see if we can work something out
RESOURCES:
The Corvid API - general info
VIDEO on front end to backend process (VERY HELPFUL)
Helpful article/tutorial for backend stuff
SUMMARY:
It surprised me that people were having this problem at all-- I figured there would be a streamlined process through Wix standard page setup (little to no coding) that would be able to deal with custom form pricing. The reason I solved the problem but left out the live price update was because it simply wasn’t necessary for the application I was hired for. However, it seems most people here are looking for the other stuff anyways. I had never used Wix before (but know how to program) and this problem took me about 2 days to solve. The system has been implemented on my friend’s website and has already processed multiple payments. Again, if you need help with specific tasks related to this (or more general stuff?) please contact my in the replies or by email. Although I only learned Wix a couple days ago I’ve found it quite intuitive and this problem has forced me to become comfortable with it. Good luck.
PS: If I have any typos or other errors please lmk.