第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在PayPal結賬集成中將變量從js傳遞到php?

如何在PayPal結賬集成中將變量從js傳遞到php?

PHP
不負相思意 2022-01-02 19:43:58
我正在用 HTML 和 PHP 創(chuàng)建一個購物網站。這是我第一次做 Web 開發(fā)。我正在嘗試將 PayPal Checkout 添加到我的網站。我從貝寶開發(fā)人員那里得到了代碼,付款成功了。如果付款成功,我需要使用 php 將訂單數(shù)據(jù)插入到我的數(shù)據(jù)庫中。如何將我定義的表示付款批準的 javascript 變量傳遞給 PHP?我已經看到它用 ajax 完成,但我不知道如何實現(xiàn)它。這是我的代碼。我添加了 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>
查看完整描述

1 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

PHP 只能在服務器上看到。因此,您應該使用fetch(推薦)或XMLHTTPRequest向服務器發(fā)送一個值。


創(chuàng)建一個 PHP 文件,您可以在其中接收$_GET或$_POST變量并處理剩余的付款。在本例中,我將文件ajax_file.php命名為 ,但您可以隨意命名它。


添加fetch到您的onApprove方法中。我們將使用該POST方法發(fā)送,以便通過該body屬性發(fā)送數(shù)據(jù)更容易一些。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.

        });


    });

}

在您的 PHP 文件中,例如ajax_file.php.

在那里你檢查價值。查看它是否已發(fā)送以及它是否是正確的值,然后從那里繼續(xù)流程。


// 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.


}

您的問題不清楚接下來會發(fā)生什么,但我希望您能從這里弄清楚。


查看完整回答
反對 回復 2022-01-02
  • 1 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號