Pay API accepts payment but does not always save to dataset

I am going a bit crazy. Our registration page used to work all the time—accepting payments and saving the registration info. Now, sometimes the info is not saved. Why? We successfully get the money but it needs to be matched to registration info obviously.

let course ;
let quantity ;

if ( $w ( “#radioGroup1” ). value === “course” ) { //regular course payment
course = $w ( “#text148” ). text ;
quantity = 1
let pricetext = $w ( “#text120” ). text ;
let price = parseInt ( pricetext );

console . log ( price ); 

createMyPayment ( course , price , quantity )
. then ( ( payment ) => {
wixPay . startPayment ( payment . id )
. then ( ( result ) => {
if ( result . status === “Successful” ) {
$w ( “#dataset3” ). setFieldValue ( “paymentStatus” , “Paid” );
$w ( “#dataset3” ). save ();

        $w ( "#text147" ). text  =  "Thank-you! You are now registered for "  +  $w ( "#text146" ). text ; 
        $w ( "#text147" ). show ()  // handle payment success 
      }  **else if**  ( result . status  ===  "Failed" ) { 
       // handle payment failure 
      }  **else if**  ( result . status  ===  "Pending" ) { 
        // handle payment pending 
      }  **else if**  ( result . status  ===  "Cancelled" ) { 
        // handle user closing payment panel 
      } 
    } ); 
} ); 

}

You can consider using backend events to save data so you never lost any of the data. On the frontend, user might not yet closed the “thank you” popup from the wix pay module, then, the setFieldValue() and all functions after that won’t run.

You can also pass a parameter to showThankYouPage : false