I am trying to set up a form for users to purchase a product with 3 different price points and a promo code.
If the promo code is “beta” then it only charges $1 regardless of the item selected, otherwise it charges per the product selected.
Form-based payments don’t currently support a promo code like that supported for products, so I’m trying to custom code it.
Here’s what the user experience looks like:
I found a wix velo article on payments here , and replicated that code, but it’s not working right.
In Dev mode I get a Payments dialogue box with the item name & price (adjusted for promo code), but without any of the fields to continue with payment:
In production on the published site, it simply skips the payment box completely and just continues with the form submit (thank you confirmation page & email triggered).
You can see my code below. Any suggestions?
Note - since I am custom coding this, I do not have “payments” turned on for the form.
FRONT END
import wixPay from 'wix-pay';
import {createPaymentForProduct} from 'backend/BE_PayAPI';
export function button1_click(event) {
const promoCode = $w('#promoCode').value;
let item={name:"", price:50};
item.name=$w('#productSelected').value;
item.price = extractPrice(item.name);
createPaymentForProduct(promoCode,item.name,item.price).then(payment => {
wixPay.startPayment(payment.id);
});
}
function extractPrice (string) {
if (!string) return;
let stringArray= string.split(/\$|\)/);
return stringArray[1];
}
BACKEND
import wixPay from 'wix-pay-backend';
export function createPaymentForProduct(promo_code, product_name, product_price) {
let itemprice;
if (promo_code.match(/beta/i)) {
itemprice = 1;
} else {
itemprice =product_price;
}
return wixPay.createPayment( {
amount: itemprice,
items: [{name: product_name, price: itemprice}],
} );
}
Any help would be greatly appreciated.
Thank you much.