Help with form submit after payment

Hello, I’ve created a custom form in EditorX which submits to a collection on submit. I then added a payment requirement for submitting, however the data submits to the database whether the payment is completed or not.

I’m trying to configure it so it first checks that payment was successful before submitting the data, but I’m a bit out if my depth and can’t get it working.

My code so far:
import wixPay from ‘wix-pay’;
import {createPaymentForProduct} from ‘backend/BE_PayAPI’;

export function button1_click( event ) {
let payment = await createPaymentForProduct(itemId);
await wixPay.startPayment((payment.id), { “termsAndConditionsLink”: “https://www.wix.com/”});
await wixData.insert(‘dataset1’, item);
});
}

If anyone can help show me where I might be going wrong, that would be great thanks :slight_smile:

Hello! Sounds like a problem with the data being stored before the asynchronous functions are finished executing. Maybe making button1_click an async function and see if that helps.

thanks Emmy, does this look better?

import wixPay from ‘wix-pay’;
import {createPaymentForProduct} from ‘backend/BE_PayAPI’;

export function button1_click( async ) {
let payment = await createPaymentForProduct(itemId);
await wixPay.startPayment((payment.id), { “termsAndConditionsLink”: “https://www.wix.com/”});
await wixData.insert(‘dataset1’, item);
});
}

Hi again,
the export function might look something like

export async function button1_click(event) {
    ...
}

you just need to add the “async” keyword before your function!