贝宝支付成功后发布到 PHP 脚本

Posted

技术标签:

【中文标题】贝宝支付成功后发布到 PHP 脚本【英文标题】:Post to PHP script after successful paypal payment 【发布时间】:2021-08-22 21:26:39 【问题描述】:

我有一个用于注册俱乐部的 html 表单。然后我有一个我想要调用的 php 脚本,它将向我发送一封包含注册信息的电子邮件。我需要做的是在我通过贝宝成功付款后调用 PHP 脚本。在沙箱下,我成功地点击了“onApprove”回调。我只是想知道如何调用 PHP 文件并从表单中发布数据。任何帮助,将不胜感激。或者如果有更好的方法,请告诉我。

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 脚本:

<?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);
?>

【问题讨论】:

developer.paypal.com/docs/api-basics/notifications/ipn @AbraCadaver 我不是在看如何知道是否付款,我只需要知道付款后如何调用 php 脚本。 它就是这么做的。 @AbraCadaver 我没有看到它告诉我该怎么做。 【参考方案1】:

为了在支付成功后调用 PHP 文件,您可以使用 jQuery 的 .post() 方法轻松调用该文件。

以下是 post 调用在 javascript 代码中的示例。一旦调用了 JavaScript,使用它将成功调用您的 PHP 文件,并且您可以使用 data 有效负载将 PHP 文件中的数据返回到原始页面。

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

为了更好地理解jQuery.post() 方法,您可以在此处查看文档:

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

【讨论】:

谢谢。我现在开始工作了,但现在它只是导航到一个空白页面,并显示它在 php 脚本上。我该如何防止呢? (即 www.mypage.com/script.php) @linuxer 您是说一旦调用该函数,浏览器就会重定向到 PHP 文件?浏览器控制台中是否出现任何可能与该问题相关的错误? 是的,这就是我要说的。它重定向。如果我添加 echo"$name" 则显示该值。我没有收到任何错误。 如果我调用 document.forms["emailregistration"].submit() 也会发生同样的事情 嗯,不知道是什么原因造成的。您可以发布页面的完整 HTML 吗?我想看看这是怎么回事,但我不相信 sn-ps 足以运行这些函数。

以上是关于贝宝支付成功后发布到 PHP 脚本的主要内容,如果未能解决你的问题,请参考以下文章

通过贝宝成功的自适应付款付款请求

Magento:支付完成后,paypal 标准支付未重定向到 Magento 成功页面

成功支付宝付款后无法重定向商家网站

贝宝支付数据传输

将输入数据从表单传输到贝宝付款,如果付款成功则保存

在 Browserstack 中运行后发布到 Cypress Dashboard