简体   繁体   English

如何在PayPal Checkout集成中将变量从js传递到php?

[英]How to pass variable from js to php in PayPal checkout Integration?

I'm creating a shopping site with HTML and PHP. 我正在用HTML和PHP创建购物网站。 It's my first time doing web development. 这是我第一次进行Web开发。

I'm trying to add PayPal Checkout to my site. 我正在尝试将PayPal Checkout添加到我的网站。 I got the code from paypal developer and it works payments are successful. 我从Paypal开发人员那里获得了代码,并且可以成功付款。 If the payment is successful I need to insert order data into my database with php. 如果付款成功,我需要使用php将订单数据插入数据库。

How do I pass a javascript variable defined by me indicating the payment approval to PHP? 如何传递我定义的javascript变量来指示对PHP的付款批准?

I've seen it done with ajax but I dont know how to implement it. 我已经看到它用ajax完成,但我不知道如何实现它。

Here is my code. 这是我的代码。 I added the jquery CDN since it seems to be necessary but I don't know if need anything else. 我添加了jQuery CDN,因为它似乎是必需的,但我不知道是否需要其他任何东西。

 <!DOCTYPE html> <head> <!-- Add meta tags for mobile and IE --> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </head> <body> <!-- Set up a container element for the button --> <div id="paypal-button-container"></div> <!-- Include the PayPal JavaScript SDK --> <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD"></script> <script> // Render the PayPal button into #paypal-button-container paypal.Buttons({ style: { layout: 'horizontal', color: 'blue', shape: 'rect' }, // Set up the transaction createOrder: function(data, actions) { return actions.order.create({ purchase_units: [{ amount: { value: '17' } }] }); }, // Finalize the transaction onApprove: function(data, actions) { return actions.order.capture().then(function(details) { // Show a success message to the buyer alert('Transaction completed by ' + details.payer.name.given_name + '!'); //Variable to confirm successful payment on php //? //? }); } }).render('#paypal-button-container'); </script> </body> 

PHP can only be seen on the server. PHP仅在服务器上可见。 So you should send a value to the server with fetch (recommended) or XMLHTTPRequest . 因此,您应该使用fetch(推荐)XMLHTTPRequest将值发送到服务器。

Create a PHP file in which you can receive a $_GET or $_POST variable and process the rest of the payment. 创建一个PHP文件,您可以在其中接收$_GET$_POST变量并处理其余的付款。 In this example I call the file ajax_file.php , but you can name it whatever you want. 在此示例中,我将文件ajax_file.php ,但您可以根据需要命名。

Add the fetch to your onApprove method. fetch添加到onApprove方法中。 We'll be sending with the POST method so that it is a bit easier to send data through the body property. 我们将使用POST方法进行发送,以便通过body属性发送数据更加容易。 GET can do it too but works a little bit different. GET也可以做到,但工作方式略有不同。

// Finalize the transaction
onApprove: function(data, actions) {                
    return actions.order.capture().then(function(details) {
        // Show a success message to the buyer
        alert('Transaction completed by ' + details.payer.name.given_name + '!');

        // URL which to send the value to.
        const url = 'http://yoursite.com/ajax_file.php';

        // Message to send. In this example an object with a state property.
        // You can change the properties to whatever you want.
        const message = { status: 'success' };

        // Send it to the URL with the POST method 
        // and send the message in JSON format.
        fetch(url, {
            method: 'POST',
            body: JSON.stringify(message)
        }).catch(function(error) {
            console.log(error); // Display error if there is one.
        });

    });
}

And in your PHP file, for example ajax_file.php . 在您的PHP文件中,例如ajax_file.php
There you check the value. 在此检查值。 See if it's sent and if it's the right value and continue the flow from there. 查看它是否已发送以及它是否是正确的值,然后从那里继续流程。

// Check if status send.
$status = isset( $_POST[ 'status'] ) ? $_POST[ 'status' ] : false;

// Check the value of status
if ( $status === 'succes' ) {

    // Status is a success.
    // Continue your form flow.

}

It's not clear in your question what should happen next, but I hope you can figure that out from here. 您的问题尚不明确下一步该怎么办,但我希望您可以从这里弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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