简体   繁体   English

如何重新呈现具有不同值的 PayPal 智能按钮?

[英]How to re-render PayPal Smart Buttons with different values?

I'm using PayPal Smart button on my web site, and after each payment is made the user is able to make another new payment with PayPal smart button.我在我的网站上使用 PayPal Smart 按钮,每次付款后,用户都可以使用 PayPal 智能按钮进行另一次新付款。 The problem is that once I've created my PayPal button I'm unable to set the new createOrder in it with new orderID.问题是,一旦我创建了我的 PayPal 按钮,我就无法使用新的 orderID在其中设置新的 createOrder。

So in my website I have the following function which init the PayPal button if there is yet a pending order or it's called when a new order is made and I get a new orderID.因此,在我的网站中,我有以下功能,如果尚有挂单,则初始化 PayPal 按钮,或者在创建新订单时调用该按钮并获得新订单 ID。

function initPayPal(orderID, publicKey) {
    var paymentID = $("#paypal-button-container").data("payment-id")
    var PAYPAL_SCRIPT =
        "https://www.paypal.com/sdk/js?client-id=" + publicKey + "&currency=EUR&intent=capture&commit=false&vault=true";
    var script = document.createElement("script");
    script.setAttribute("src", PAYPAL_SCRIPT);
    script.onload = function () {
        paypal
            .Buttons({
                style: {
                    shape: "rect",
                    color: "gold",
                    layout: "horizontal",
                    label: "paypal",
                    tagline: false,
                    height: 52,
                },
                createOrder: async function () {
                    $(".body-payments").hide();
                    $(".loader-payments").show();
                    const res = await fetch(
                        "/api/payment/paypal/" + paymentID + "/" + orderID,
                        {
                            method: "post",
                            headers: {
                                "content-type": "application/json",
                            },
                            credentials: "include",
                        }
                    );
                    const data = await res.json();
                    return data.id;
                },
                onApprove: async function (data) {
                    const res = await fetch(
                        "/api/payment/paypal/capture/" + paymentID + "/" + data.orderID,
                        {
                            method: "post",
                            headers: {
                                "content-type": "application/json",
                            },
                            credentials: "include",
                        }
                    );
                    if (localStorage.STBOrder) {
                        localStorage.removeItem("STBOrder");
                    }

                    $("#modalPayments").modal("hide");
                    $("#modalSuccess").modal("show");
                    $(".body-payments").show();
                    $(".loader-payments").hide();

                },
                onCancel: function (data) {
                    $(".body-payments").show();
                    $(".loader-payments").hide();
                },
            })
            .render("#paypal-button-container");
    };
    document.head.appendChild(script);
}

Client id is set dynamically as each user on its own page will get the payment on its own account.客户端 ID 是动态设置的,因为每个用户在其自己的页面上都将通过自己的帐户获得付款。 The issue here is that the button is initiated the first time all works fine, but if I dismiss the order and I call this function again it will create another button instead of setting new values to existing (already rendered) one.这里的问题是按钮第一次启动时一切正常,但如果我取消订单并再次调用此函数,它将创建另一个按钮,而不是为现有(已呈现)按钮设置新值。

There should be no need to init the button more than once.应该不需要多次初始化按钮。

Initialize the button one time (on page load).初始化按钮一次(在页面加载时)。

Then, inside createOrder, ensure paymentID and orderID reference non-local variables or are replaced with function calls that will return the correct value at the time you want them to, eg some kind of getOrderID() of yours, and $("#paypal-button-container").data("payment-id") , in place of your existing local variables然后,在 createOrder 中,确保 paymentID 和 orderID 引用非局部变量或替换为在您希望它们返回正确值的函数调用中,例如您的某种 getOrderID() 和$("#paypal-button-container").data("payment-id") ,代替您现有的局部变量


In the unlikely event that you really did need to render the buttons more than once, you could save a reference to myButtons = paypal.Buttons({..}) before calling .render() --- and then, later myButtons.close() before saving a new reference to and rendering a new buttons object.万一您确实确实需要多次渲染按钮,则可以在调用myButtons = paypal.Buttons({..})之前保存对myButtons = paypal.Buttons({..})引用,然后再调用 myButtons.close () 在保存对新按钮对象的新引用并呈现新按钮对象之前。 But again, there should be no reason to do that here.但同样,在这里应该没有理由这样做。

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

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