Submit form when payment is successful

Hey community !
i have a form with a “submit” button that also run a payment function.
i would like to submit the form if the payment is “successful”, this is the code i tried:


export function button4_click(event,{total,name}) {
 if ($w('#input29').valid && $w('#input30').valid && $w('#input31').valid &&
        $w('#input32').valid && $w('#radioGroup3').valid && $w('#input33').valid && $w('#input34').valid)
     createMyPayment("סכום לתשלום",(cart.totals.total).toString())
    .then( (payment) => {
      wixPay.startPayment(payment.id, {
 "showThankYouPage": false,
 "termsAndConditionsLink": "https://mysite.com/terms"
 
      })
      .then( (result) => {
 if (result.status === "Successful") {
            $w.submit('#wixForm3');

          } else if (result.status === "Pending") {
           console.log("pending")
         }
      } );
    } );
}

the form is submitted after the payment pop up is open, what im missing here :roll_eyes:

someone :disappointed_relieved:

Hi Anthony :raised_hand_with_fingers_splayed:

You can check if the payment status is successful or not like this.

await wixPay.startPayment(payment.id).then((result) => {
    if (result.status === 'Successful') {
        // If you're using a dataset.
        $w('#dataset').save();
        
        // If you're using data.
        wixData.insert('collection', item);
    }
})

The problem is that you’re not waiting until the pop up is closed, therefore the submission is fired immediately. use async/await to avoid this behavior.

Hope this helps~!
Ahmad

Hey Ahmad thx for your help ! :]
im not sure where to add the async/await in the code. i get an error if i add it to the function. can you please guide me ?

@anthony-tony-b Just above the await keyword, ad the async keyword here:

.then(async (payment) => {
    await wixPay.startPayment(payment.id, options).then((result) => {
        // Check if the payment is successful
    })
})

Hope this helps~!
Ahmad

Does this work with the booking page on wix, or only the wix-pay api? Is there a way to do it for the booking page?

No, it only works with wixPay, however, you can use wixPay on a custom form that schedules the appointment directly.

@ahmadnasriya I have a similar piece of code

My payment is designed to run on the form submit. I want to trigger an email when the form is submitted but ONLY if the payment is completed. Currently, the email sends regardless of whether the user finalizes their payment.

Should I be using a different set up?

Hi @anthony-tony-b did you ever solve this problem?
I have the same thing here