PHP SDK - Call to confirmation email function breaks success message

Hi,

I’m sending a payment confirmation email to customers (currently in sandbox) for their payment. I have a mailer function added to process-payments.php and I am calling it if $response->isSuccess() just before the echo json_encode($response->getResult());. Here is the code snippet:

if ($response->isSuccess()) {
    sendConfirmationEmail();
    echo json_encode($response->getResult());
  } else {
    echo json_encode(array('errors' => $response->getErrors()));
  }

The payment is successful, and the email is sent, but the success message never appears on the payment page for the customer. What could be wrong?

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Payments API Webhooks
Create and Publish Invoices
Postman

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The issue might be due to the sendConfirmationEmail() function. If this function is causing an error or exception, it could prevent the success message from being displayed. To debug this, you could try commenting out the sendConfirmationEmail() function call and see if the success message appears. If it does, then the issue is likely within the sendConfirmationEmail() function. You should then debug this function to find the exact cause of the issue.

This answer was reviewed by @Bryan-Square.

Thanks. I found the issue. I had copied another email function that was part of a php ajax call anf it had an echo at the end, so that was overriding the echo for the success message. Everything is good now.

Glad to hear you figured it out and thanks for sharing your findings. :slightly_smiling_face: