Hey guys:
I am experiencing a intermittent error: Error loading web module backend/stripeProxy.jsw: Bindings not found
. Sometimes I get it, sometimes I don’t ( on the same code ). For context I am building a custom checkout on Wix Studio and I am calling functions in a backend module ( for security reasons ).
However, sometimes when I call them I get the error: Bindings not found. I feel like this error is out of my control and is Wix’s fault.
Here is the code:
stripeProxy.jsw ( backend module that is causing problems )
import { fetch } from 'wix-fetch';
import { STRIPE_SECRET_KEY } from 'public/PayKeys';
const apiKey = STRIPE_SECRET_KEY; // DO NOT SHARE THIS KEY
export async function charge(token, ids, amounts) {
console.log("Sever Side: \n charge has been called \n token: " + token + "\n ids: " + ids + "\n amounts: " + amounts + "\n apiKey: " + apiKey + "\n");
const cart = await createCart(ids, amounts);
console.log(cart);
const response = await fetch("https://api.stripe.com/v1/charges", {
method: 'post',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + apiKey
},
body: encodeBody(token, cart)
});
if (response.status >= 200 && response.status < 300) {
// transaction successful - get charge ID
const ret = await response.json();
// let id = await setPaidStatus(userId, ret.id);
return { "chargeId": ret.id };
}
// transaction failed - return error type
let res = await response.json();
let err = res.error.type;
return { "error": err };
}
function encodeBody(token, cart) {
let encoded = "";
for (let [k, v] of Object.entries(cart)) {
encoded = encoded.concat(k, "=", encodeURI(v), "&");
}
encoded = encoded.concat("source=", encodeURI(token));
return encoded;
}
async function createCart(ids, amounts) {
let totalPrice = 0;
let description = "food order: ";
// Create an array to store all the promises returned by the wixData queries
let promises = [];
// Iterate over the IDs and amounts
ids.forEach((id, i) => {
// Push each query promise to the array
promises.push(
wixData.query("FoodList")
.eq("_id", id)
.find()
.then((results) => {
totalPrice += parseFloat(results.items[0].price) * amounts[i] / 100;
description += results.items[0].title + " x" + amounts[i] + ", ";
})
);
});
// Wait for all the promises to resolve
await Promise.all(promises);
// Round totalPrice to 2 decimal places
totalPrice = totalPrice.toFixed(2);
// Return the cart object
return {
"amount": totalPrice * 100, // convert to cents
"currency": "BGN",
"description": description
};
}
export async function issueOrder(ids, amounts) {
}
checkout.gwk2m.js ( front end page code ):
import wixPayFrontend from 'wix-pay-frontend';
import { local, session, memory } from "wix-storage-frontend";
import { STRIPE_PUBLISHABLE_KEY } from 'public/PayKeys';
import * as stripeProxy from 'backend/stripeProxy';
import * as stripeAPI from "public/stripeAPI";
import wixLocation from 'wix-location';
//
let ids = [];
let amounts = [];
function retrieveCart() {
ids = JSON.parse(session.getItem("CheckoutIds"));
amounts = JSON.parse(session.getItem("CheckoutAmounts"));
}
function displayCart() {
let i = 0;
ids.forEach(id => {
console.log(id + " x" + amounts[i++] + "\n");
});
}
$w.onReady(async function () {
console.log("script is running");
$w("#errorMessage").collapse();
retrieveCart();
displayCart();
});
$w("#PayNowButton").onClick(() => {
$w("#errorMessage").collapse();
$w("#PayNowButton").label = "Обработване...";
$w("#PayNowButton").disable();
payNow();
console.log("payNow has been executed");
});
export function payNow() {
stripeAPI.createToken(stripeAPI.encodeCard(createCard())) // ERROR
.then((token) => {
stripeProxy.charge(token, ids, amounts)
.then((response) => {
if (response.chargeId) {
console.log("Payment Successful");
console.log("Charge ID: " + response.chargeId);
$w("#PayNowButton").label = "Готово!";
wixLocation.to("/thankyou");
}
else {
$w("#PayNowButton").label = "Плати";
$w("#PayNowButton").enable();
$w("#errorMessage").expand();
console.log(response.error);
}
});
});
}
function createCard() {
let { month, year } = splitExpirationDate($w("#expiration").value);
return {
// @ts-ignore
"name": $w("#cardholder").value,
// @ts-ignore
"number": $w("#cardnum").value,
// @ts-ignore
"cvc": $w("#cvc").value,
// @ts-ignore
"exp_year": year,
// @ts-ignore
"exp_month": month
};
}
function splitExpirationDate(date) {
const [month, year] = date.split('/');
return { month, year };
}
From my understandings, the binding error occurs when the module is imported incorrectly or the path is different. But in this case it is completly correct ( see screenshot below ):
This is the problem. The bindings that I think are out of my control. I have also tried refreshing and reloading the project. But I haven’t had success.
Sometimes it works. It drives me mad.
Anyways, in advance, thank you guys for your attention and time!