- SOLVED -Update field in dataset for logged in user if payment is successful

Hey Wix team,

I have an issue that I’m trying to solve… I have been working on a members area so that people can pay any outstanding bills and amend their booking if required to reduce work load.

Some details, so when the the user purchases a course they are sent to the payment screen which will at some point prompt them to either create an account, log in or guest check out


If they either log in or register and become logged in then upon payment the data is saved to a dataset ‘CourseAvailability’ with a filter for logged in user, both on the data being saved after payment…

and on the members area page ‘My Adventures’


This all works well and displays the correct data for the trip that has just been booked of rthe logged in user.
What I now need to happen is that when the user logs in to pay the remaining balance, they are directed to a lightbox with card payment (this is different to the original payment screen due to some complex forms being involved)
Payment goes through but the field for the ‘outstandingBalance’ doesn’t update to £0 and still shows that the payment is due. I haven’t used much of the .update code before so I’m struggling a bit due to the fact it needs to update specific to the logged in user.

This is part of the code to update the field ‘outstandingBalance’ in the ‘CourseAvailability’ dataset if the payment is successful

export function payNow(event) {
let thisTrip = $w("#courseAvailabilityDataset").getCurrentItem();
let toUpdate = {
 "_id":      thisTrip._id,
 "outstandingBalance":    "0",
};
    createToken(encodeCard(createCard()))
        .then((token) => {
            console.log("Card token: " + token);
            tokenResponse = parseFloat(token);
            charge(token, payment)
 //charge($w("#token").value,payment)
                .then((chargeResponse) => {
                    console.log(chargeResponse);
 if (tokenResponse > 300) {
                        $w("#textFail").text = "There was an error with your card.\nPlease try again later or contact us.";
                        $w("#textFail").show();
                        console.log("There was an error with your card.");
                        $w('#paymentPreLoaderBox').hide();
                    } else {
                            $w('#paymentPreLoaderBox').hide();
                            $w('#slideshow1').changeSlide(1);
                            wixData.update("CourseAvailability", toUpdate)
                            .then( (results) => {
 let item = results; //see item below
                                   })
                            .catch( (err) => {
 let errorMsg = err;
                        });
                    }
                })
                .catch(() => {
                    $w("#textFail").text = "Please Complete All Fields";
                    $w("#textFail").show();
                });
        });
}

It’s making my head hurt…

Thanks in advance

I managed to solve it!

export function payNow(event) {
let thisTrip = $w("#tripID").text; //this is the _id from the dataset
let finalBalance = $w('#finalBalanceText').text; //the field to update
let toUpdate = {
 "_id":    thisTrip,
 "outstandingBalance":   finalBalance,
};
    createToken(encodeCard(createCard()))
        .then((token) => {
            console.log("Card token: " + token);
            tokenResponse = parseFloat(token);
            charge(token, payment)
 //charge($w("#token").value,payment)
                .then((chargeResponse) => {
                    console.log(chargeResponse);
 if (tokenResponse > 300) {
                        $w("#textFail").text = "There was an error with your card.\nPlease try again later or contact us.";
                        $w("#textFail").show();
                        console.log("There was an error with your card.");
                        $w('#paymentPreLoaderBox').hide();
                    } else {
                            $w('#paymentPreLoaderBox').hide();
                            $w('#slideshow1').changeSlide(1);
                            wixData.query("CourseAvailability")
                            .eq("_id", thisTrip)
                            .find()
                            .then( (results) => {
                            let item = results.items[0];
                            item.outstandingBalance = "0"; // updated balance
                                wixData.update("CourseAvailability", item);
                                console.log(item)
                                      } )
                            .catch( (err) => {
                            let errorMsg = err;
                        } );
 
                    }
                })
                .catch(() => {
                    $w("#textFail").text = "Please Complete All Fields";
                    $w("#textFail").show();
                });
        });
}

Turns out I had to use a query and not just update as this deletes the other entries, anyway its working and the code if it helps anyone else.