Executing a server action from within some custom javascript

I have place some javascript into my page (it’s code provided by Stripe for the payment processing) and it has an if…else for whether the payment was successful or not. This is the code snippet:

  function runStripe() {
    var cardholderName = document.getElementById('inp_regPhotoConsentParentFullName');
    var cardButton = document.getElementById('card-button');
    console.log("Triggered");
    requestAnimationFrame(function() {
      stripe.handleCardPayment(
        dmx.parse("serverconnectform2.data.apiStripePaymentIntent.data.client_secret"),
        cardElement, {
          payment_method_data: {
            billing_details: {name: cardholderName.value}
          }
        }
      ).then(function(result) {
        if (result.error) {
          // Display error.message in your UI.
          alert('Aah, there was a problem with your payment. Please try again.');
        } else {
          // The payment has succeeded. Show the thank you page.
          window.location.replace("/thankyou.php");
        }
      });
    });
  }

I have put in some javascript to redirect to a thank you page but I also want to send out a couple of emails - one to the site owner and one to the customer. I’ve created the server action with the email mailer and send mail actions but I don’t know how to call that. I want it called when the payment has succeeded.

The function is called with this code in my form tag:

onsuccess="runStripe()"

Can anyone help me?

Also, what’s the best way to send some data to the server action so I can include details in the emails when done this way?

TIA.

Community Page
Last updated: