使用ajax将Paypal-Buttons(客户端)链接到服务器响应(PHP)

问题描述 投票:0回答:1

我想在我的网站上使用Paypal,因此我正在遵循教程here。到目前为止,一切正常:通过composer下载了SDK。创建了类PayPalClientCreateOrder

在本节的最后,需要使用javascript将CreateOrder的结果链接到按钮:

    <script>
    paypal.Buttons({
        createOrder: function() {
            return fetch('test_paypal_create_paypal_transaction.php', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                }
            }).then(function(res) {
                return res.json();
            }).then(function(data) {
                return data.orderID; // Use the same key name for order ID on the client and server
            });
        },
        onApprove: function(data, actions) {
            // This function captures the funds from the transaction.
            return actions.order.capture().then(function(details) {
                // This function shows a transaction success message to your buyer.
                alert('Transaction completed by ' + details.payer.name.given_name);
            });
        }
    }).render('#paypal-button-container');
    //This function displays Smart Payment Buttons on your web page.
</script>

这是php文件(test_paypal_create_paypal_transaction.php):

require __DIR__ . '/../../vendor/autoload.php';

use MyProject\PayPay\CreateOrder;

$response = CreateOrder::createOrder(false);
echo json_encode($response);

CreateOrder类工作正常。我可以说出贝宝的回应。这个问题似乎不存在。当我按Paypal按钮时,我还可以在Firefox-Dev-Tools中看到XHR-Request已正确完成。按钮旁边还有一个XHR-Response。但是似乎我使用了错误的格式或错误的数据部分,这些数据被返回到fetch() -Function。

有人有想法吗?

感谢您的帮助!

Tim

php ajax paypal xmlhttprequest paypal-rest-sdk
1个回答
0
投票

好,我找到了解决方法。

© www.soinside.com 2019 - 2024. All rights reserved.