如何将订单ID传递给PayPal按钮?

发布于 2025-01-26 11:30:50 字数 1477 浏览 1 评论 0原文

我通过PHP SDK创建订单。

像这样

$result = json_decode((string) $response->getBody());

echo $result->id; // id of the created order

,现在我拥有此订单ID。但是...如何将订单ID传递给PayPal按钮? 像这样

`paypal.Buttons({

    // Set up the transaction
    createOrder: function(data, actions) { //I don't need create order,I have created an order
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: '88.44'
                }
            }]
        });
    },

    // Finalize the transaction
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(orderData) {
            // Successful capture! For demo purposes:
            console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
            var transaction = orderData.purchase_units[0].payments.captures[0];
            alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');

            // Replace the above to show a success message within this page, e.g.
            // const element = document.getElementById('paypal-button-container');
            // element.innerHTML = '';
            // element.innerHTML = '<h3>Thank you for your payment!</h3>';
            // Or go to another URL:  actions.redirect('thank_you.html');
        });
    }


}).render('#paypal-button-container');

I create an order by my php sdk.

like this

$result = json_decode((string) $response->getBody());

echo $result->id; // id of the created order

and now i have this order id. but ... How to pass order ID to Paypal Button?
like this

`paypal.Buttons({

    // Set up the transaction
    createOrder: function(data, actions) { //I don't need create order,I have created an order
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: '88.44'
                }
            }]
        });
    },

    // Finalize the transaction
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(orderData) {
            // Successful capture! For demo purposes:
            console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
            var transaction = orderData.purchase_units[0].payments.captures[0];
            alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');

            // Replace the above to show a success message within this page, e.g.
            // const element = document.getElementById('paypal-button-container');
            // element.innerHTML = '';
            // element.innerHTML = '<h3>Thank you for your payment!</h3>';
            // Or go to another URL:  actions.redirect('thank_you.html');
        });
    }


}).render('#paypal-button-container');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2025-02-02 11:30:50

在您的服务器上创建两个路由,一个以创建订单(并返回结果JSON),而将订单ID作为参数并捕获它(并返回JSON结果)。

这两种路线都应返回/输出仅JSON(无html或文本)。

将这两条路线与此批准流配对: https://developer.paypal.com/demo/checkout/patern/pattern/server

Create two routes on your server, one to create an order (and return the resulting JSON), and one that takes an order id as a parameter and captures it (and returns the JSON result).

Both of these routes should return/output only JSON (no HTML or text).

Pair those two routes with this approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文