收到 onApprove PayPal 后发送邮件

Send mail after receive onApprove PayPal

我正在尝试在收到 onApprove 时发送邮件,但我不知道该怎么做。所以,这是我的 index.html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Fede Pistone</title>
    <!-- Internos -->
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <!-- Externos -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>
  <body>
    <h3>Formulario de Contacto</h3>
    <div class="container">
      <form method="POST" id="myForm" action="enviarmail.php" >
        <label for="nombre">Nombre:</label>
        <input type="text" id="nombre" name="nombre" placeholder="Ingresa tu nombre...">
        <label for="apellido">Apellido</label>
        <input type="text" id="apellido" name="apellido" placeholder="Ingresa tu apellido...">
        <label for="email">Email</label>
        <input type="text" id="email" name="email" placeholder="Ingresa tu email...">
        <label for="mensaje">Mensaje:</label>
        <textarea id="mensaje" name="mensaje" placeholder="Ingresa tu consulta..." style="height:200px"></textarea>
        <div id="smart-button-container">
          <div style="text-align: center;">
            <div id="paypal-button-container" name="divPayPal"></div>
          </div>
        </div>
      </form>
    </div>
    <p>Al presionar el botón, se abrirá un formulario en el cuál deberá abonar un monto de u$s20. <br>Recuerde que al presionarlo, usted está aceptando esto. <br>Una vez procesado el pago, recibirá un mail con un link para una reunión virtual con un horario específico. <br>Por favor, consulte en su bandeja de entrada y/o spam.</p>

    <script src="https://www.paypal.com/sdk/js?client-id=sb&enable-funding=venmo&currency=USD" data-sdk-integration-source="button-factory"></script>
    <script>
      function initPayPalButton() {
        paypal.Buttons({
          style: {
            shape: 'rect',
            color: 'gold',
            layout: 'vertical',
            label: 'paypal',

          },

          createOrder: function(data, actions) {
            return actions.order.create({
              purchase_units: [{"description":"Ejemplo de botón","amount":{"currency_code":"USD","value":20}}]
            });
          },

          onApprove: function(data, actions) {
            var nombre = document.getElementById('nombre').value;
            var apellido = document.getElementById('apellido').value;
            var nombreJunto = nombre + " " + apellido;
            var mailForm = document.getElementById('email').value;
            var mensajeForm = document.getElementById('mensaje').value;

            $.ajax({
              url: "enviarmail.php",
              method: "POST",
              data: {action: 'e-mail', nombre: nombreJunto, email: mailForm, mensaje: mensajeForm},
              dataType: "json",
              success: function(response){
                if(response.status == 200){
                  console.log(response + "status: " + response.status);
                  const element = document.getElementById('paypal-button-container');
                  element.innerHTML = '';
                  element.innerHTML = '<h3>Gracias, nos estaremos comunicando contigo a la brevedad.</h3>';
                }else{
                  console.log(response);
                }
              }
            })
          },

          onError: function(err) {
            const element = document.getElementById('paypal-button-container');
            element.innerHTML = '';
            element.innerHTML = '<h3>Ha ocurrido un error, reintente más tarde. </h3>';
            console.log(err);
          }
        }).render('#paypal-button-container');
      }
      initPayPalButton();
    </script>
  </body>
</html>

这是我用来发送邮件的 php 文件:

<?php
    function json_output($status = 200, $msg = 'OK', $data = null){
      header("Content-Type: application/json; charset=UTF-8");
      return json_encode([
        'status' => $status,
        'msg' => $msg,
        'data' => $data
      ]);
      die;
     }
    if(isset($_POST["nombre"]) && isset($_POST["email"]) && isset($_POST["mensaje"]) ){
            $to = "francojoelbalsamo@gmail.com";
            $subject = "Datos de formulario de contacto";
            $contenido .= "Nombre: ".$_POST["nombre"]."\n";
            $contenido .= "Apellido: ".$_POST["apellido"]."\n";
            $contenido .= "Email: ".$_POST["email"]."\n\n";
            $contenido .= "Mensaje: ".$_POST["mensaje"]."\n\n";
            $header = "From: francojoelbalsamo@gmail.com\nReply-To:".$_POST["email"]."\n";
            $header .= "Mime-Version: 1.0\n";
            $header .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
            $header .= "Content-Transfer-Encoding: 8bit\r\n";
            if(mail($to, $subject, $contenido ,$header)){
                json_output();
            }else{
                json_output();
            }
    }
?>

所以,这是我收到 onApprove:

onApprove: function(data, actions)

php 文件工作正常,因为我正在做测试并且工作完美,现在我需要在收到 onAppove 后发送邮件,但我是新手,我不知道该怎么做,所以谁能告诉我怎么做?

但是,当我收到此结果时,如何发送 mail

好吧,在阅读评论后,我尝试使用 AJAX function 发送邮件但无法正常工作,因为邮件从未发送过,所以这是我修改的新代码:

$.ajax({
  url: "enviarmail.php",
  method: "POST",
  data: {action: 'e-mail', nombre: nombreJunto, email: mailForm, mensaje: mensajeForm},
  dataType: "json",
  success: function(response){
    if(response.status == 200){
      console.log(response + "status: " + response.status);
      const element = document.getElementById('paypal-button-container');
      element.innerHTML = '';
      element.innerHTML = '<h3>Gracias, nos estaremos comunicando contigo a la brevedad.</h3>';
    }
    else{
      console.log(response);
    }
  }
})

我编辑的新 php 文件:

<?php
    function json_output($status = 200, $msg = 'OK', $data = null){
      header("Content-Type: application/json; charset=UTF-8");
      return json_encode([
        'status' => $status,
        'msg' => $msg,
        'data' => $data
      ]);
      die;
     }
    if(isset($_POST["nombre"]) && isset($_POST["email"]) && isset($_POST["mensaje"]) ){
            $to = "francojoelbalsamo@gmail.com";
            $subject = "Datos de formulario de contacto";
            $contenido .= "Nombre: ".$_POST["nombre"]."\n";
            $contenido .= "Apellido: ".$_POST["apellido"]."\n";
            $contenido .= "Email: ".$_POST["email"]."\n\n";
            $contenido .= "Mensaje: ".$_POST["mensaje"]."\n\n";
            $header = "From: francojoelbalsamo@gmail.com\nReply-To:".$_POST["email"]."\n";
            $header .= "Mime-Version: 1.0\n";
            $header .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
            $header .= "Content-Transfer-Encoding: 8bit\r\n";
            if(mail($to, $subject, $contenido ,$header)){
                json_output();
            }else{
                json_output();
            }
    }
?>

谁能告诉我为什么这不能正常工作?

Set up standard payments guide 中,'Add and modify the code' 部分有说明如何使用服务器的说明,link 说明了必要的资源——包括 REST API 创建和捕获订单的实现,以及使用 fetch().

调用服务器路由的演示代码

发送电子邮件应该在这样的服务器路由中完成,在它向调用方传播成功的捕获响应时 JavaScript。

看了你们的回复,我终于成功发邮件了。我添加了以下内容:

onApprove: function(data, actions) {
            var nombre = document.getElementById('nombre').value;
            var apellido = document.getElementById('apellido').value;
            var nombreJunto = nombre + " " + apellido;
            var mailForm = document.getElementById('email').value;
            var mensajeForm = document.getElementById('mensaje').value;
            return actions.order.capture().then(function(orderData) {

            //all details
            console.log('Capturando resultados', orderData, JSON.stringify(orderData, null, 2));

            //show a msg
            const element = document.getElementById('paypal-button-container');
            element.innerHTML = '';
            element.innerHTML = '<h3>Thanks for your purchase.</h3>';

            $.ajax({
              type: 'POST',
              url: 'enviarmaildos.php',
              dataType: "json",
              data:{nombre:nombreJunto, email:mailForm, mensaje:mensajeForm},
              success: function(response) {
                  alert(response.success);
                },
                error: function(xhr, status, error){
                  console.log(xhr);
                }
              });

            if (document.referrer !== document.location.href) {
                setTimeout(function() {
                    document.location.reload()
              }, 5000);
            }
          });
        },