简体   繁体   中英

Post to PHP script after successful paypal payment

I have an html form for registering for a club. I then have a php script that I want to have called that will send an email to me with the registration info. What I need to have happen is have the PHP script called once I get a successful payment through paypal. Under sandbox I am successfully hitting the 'onApprove' callback. I just wonder how I call the PHP file and post the data from the form. Any help would be appreciated. Or if there's a better way to do it, please let me know.

HTML:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  background-color: black;
}

* {
  box-sizing: border-box;
}

/* Add padding to containers */
.container {
  padding: 16px;
  background-color: white;
}

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for the submit button */
.registerbtn {
  background-color: #000000;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

.registerbtn:hover {
  opacity: 1;
}

/* Add a blue text color to links */
a {
  color: dodgerblue;
}

/* Set a grey background color and center the text of the "sign in" section */
.signin {
  background-color: #f1f1f1;
  text-align: center;
}
</style>
</head>
<body>

<form method="post" name="emailregistration" action="form-to-email.php">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to register.</p>
    <hr>

    <label for="email"><b>Email*</b></label>
    <input type="text" placeholder="user@email.com" name="email" id="email" required>

    <label for="name"><b>Name (First Last)*</b></label>
    <input type="text" placeholder="John Smith" name="name" id="name" required>

    <label for="address"><b>Address*</b></label>
    <input type="text" placeholder="555 S Main, Prosper, TX 75078" name="address" id="address" required>

    <label for="phone"><b>Phone Number*</b></label>
    <input type="text" placeholder="555-555-5555" name="phone" id="phone" required>

    <label for="age"><b>Age*</b></label>
    <input type="text" placeholder="35" name="age" id="age" required>

    <br>
    <br>

    <label><b>Experience Level-I played in...*</b></label>
    <br>
    <br>
    <input type="radio" name="experience" id="neverPlayed" value="never" required>
    <label for="neverPlayed">I've never played organized football</label><br>
    <input type="radio" name="experience" id="middleSchool" value="middleSchool" required>
    <label for="middleSchool">Middle School</label><br>
    <input type="radio" name="experience" id="highSchool" value="highSchool" required>
    <label for="highSchool">High School</label><br>
    <input type="radio" name="experience" id="College" value="college" required>
    <label for="college">College</label><br>
    <input type="radio" name="experience" id="semiPro" value="semiPro" required>
    <label for="semiPro">Semi-Pro</label><br>
    <input type="radio" name="experience" id="professional" value="professional" required>
    <label for="professional">Professional</label><br>

    <br>
    <br>

    <label><b>Team Designation*</b></label>
    <br>
    <br>
    <input type="radio" name="team" id="captain" value="captain" required>
    <label for="captain">I wish to Captain a team (you will receive an additional email requesting more information)</label><br>
    <input type="radio" name="team" id="placed" value="placed" required>
    <label for="placed">I wish to be placed on a currently formed team (team name and captain's name required next)</label><br>
    <input type="radio" name="team" id="added" value="added" required>
    <label for="added">I wish to be added to a new team</label><br>

    <br>
    <br>

    <label for="formedteam"><b>If you answered "I wish to be placed on a currently formed team" above, please provide the team name and captain's name.</b></label>
    <input type="text" placeholder="Your answer" name="formedteam" id="formedteam">

    <label for="request"><b>Special Request (i.e. To be placed on the same team as another player)</b></label>
    <input type="text" placeholder="Your answer" name="request" id="request">

    <label for="emergency"><b>Emergency Contact Name/Phone Number/Relationship</b></label>
    <input type="text" placeholder="Your answer" name="emergency" id="emergency">

    <label for="emergency2"><b>2nd Emergency Contact Name/Phone Number/Relationship</b></label>
    <input type="text" placeholder="Your answer" name="emergency2" id="emergency2">

    <hr>
   

    <button type="submit" class="registerbtn">Register</button>
    <div>
    <script src="https://www.paypal.com/sdk/js?client-id=[key]">
    </script>
  
    <div id="paypal-button-container"></div>
  
    <script>
        paypal.Buttons({
            createOrder: function(data, actions) {
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: '69.99'
                        }
                    }]
                });
            },
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                  $.post("form-to-email.php", {
                      value: document.forms[emailregistration]
                  }, function(data) {
                    window.location.href = 'index';
                    alert("Success");
                  });

                    // document.forms["emailregistration"].submit(function() {
                    //   alert("Success");
                    // });
                    
                });
            }
        }).render('#paypal-button-container');
    </script>
    </div>
  </div>
  
  
  
</form>

</body>
</html>

PHP Script:

<?php
if(!isset($_POST['submit']))
{
    echo "error; you need to submit the form!";
}
$email = $_POST['email'];
$name = $_POST['name'];

if(empty($name))
{
    echo "Name and email are mandatory!";
}

$email_from = "email@email.com";
$email_subject = "New Registration";
$email_body = "There is a new registration from $name.\n email- $email";
$to = "email@email.com";
$headers = "From: $email_from \r\n";

mail($to, $email_subject, $email_body, $headers);
?>

In order to make a call to the PHP file after a successful payment, you could easily do a call to the file using jQuery's .post() method.

Below is an example of what the post call would look in your JavaScript code. Using this will successfully call your PHP file once the JavaScript is invoked and you can use the data payload to return the data from the PHP file back to the original page.

  $.post("yourfile.php",
  {
    value1: "Red",
    value2: "Blue"
  },
  function(data){
    alert("Data: " + data);
  });

For a better understanding of the jQuery.post() method, you can view the documentation here:

https://api.jquery.com/jQuery.post/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM