Home > Software design >  Send mail after receive onApprove PayPal
Send mail after receive onApprove PayPal

Time:11-18

i'm trying to send a mail when onApprove is received but i'dont know how to do that. So, this is my 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>

And this is my php file to send mails:

<?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 = "[email protected]";
            $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: [email protected]\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();
            }
    }
?>

So, here is when i receive onApprove:

onApprove: function(data, actions)

The php file works correctly 'cause i was doing test and works perfectly, now i need send mail after receive onAppove but i'm new in this things i don't know how to do that, so anyone can tell me how i do it?

But, how can i send mail when i receive this results?

Well, after read the comments i'm tried with AJAX function to send mail but not work correctly 'cause the mail never sends, so this is my new code modified:

$.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);
    }
  }
})

And my new php file edited:

<?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 = "[email protected]";
            $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: [email protected]\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();
            }
    }
?>

can anyone tells me why this not works correctly?

CodePudding user response:

Within the Set up standard payments guide, there are notes in the section of 'Add and modify the code' that explains how to use a server, and link to the necessary resources -- including REST API implementations of create and capture order, and demo code for calling server routes that implement them with fetch().

Sending an email should be done in such a server route, at the time it propagates a successful capture response to the calling JavaScript.

CodePudding user response:

After reading your responses, I have finally managed to send the mail. I have added the following:

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);
            }
          });
        },
  • Related