Hello
I have a form with an upload input which requires payment before the data can be submitted and the price they pay is dependent on the user's pricing plan.
I've found this tricky to achieve because of the following:
* getPricingPlans() is required to be within onReady()
* form submission needs to be nested within the onClick() function, which is outside the onReady()
Can anybody help explain the best way to approach this?
This is what I currently have:
import wixPay from 'wix-pay';
import {createDealPayment} from 'backend/pay';
import wixData from 'wix-data';
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
function PricingPlan(){
user.getPricingPlans()
.then( (pricingPlans) => {
let firstPlan = pricingPlans[0];
let planName = firstPlan.name;
let startDate = firstPlan.startDate;
let expiryDate = firstPlan.expiryDate;
return planName;
} );
}
$w.onReady(function () {
user.getPricingPlans()
.then( (pricingPlans) => {
let firstPlan = pricingPlans[0];
let planName = firstPlan.name;
let fee = 0;
switch (planName) {
case "Plan A":
fee = 95;
break;
case "Plan B":
fee = 80;
break;
case "Plan C":
fee = 60;
break;
}
$w('#textPricingPlan').text = 'Listing fee for ${planName}'; // doesn't work yet
$w('#textFee').text = '£${fee}'; // doesn't work yet
return fee // doesn't work yet
} );
});
export function continueButton_click(event) {
let toInsert = {
"title": $w("#title").value,
"region": $w("#region").value,
"purchasePrice": $w("#purchasePrice").value,
"photo": $w("#uploadImage").startUpload(),
};
createDealPayment(fee,1,fee) // doesn't work yet
.then((payment) => {
wixPay.startPayment(payment.id)
.then((result) => {
if (result.status === "Successful") {
wixData.insert("Deals", toInsert);
$w("#continueButton").label = "Submitted";
$w("#continueButton").style.backgroundColor = "#90C32F"
$w("#errorMsg").hide();
} else if(result.status === "Failed") {
$w("#errorMsg").show();
$w("#errorMsg").text = "Sorry, your card has been declined. Please try again or another card.";
} else if(result.status === "Pending") {
$w("#errorMsg").show();
$w("#errorMsg").text = "Your payment is pending.";
} else if(result.status === "Cancelled") {
$w("#errorMsg").show();
$w("#errorMsg").text = "Payment cancelled.";
};
})
})
}