ReactJS Paypal 订阅

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

我想在我的 ReactJS 应用程序上接受 PayPal 付款。付款方式为订阅。现在的问题是 onApprove paypal 窗口不会自动关闭。我想知道此时如何正确处理这个问题。

useEffect(() => {
    const script = document.createElement('script');
    script.src = "https://www.paypal.com/sdk/...;
    script.onload = () => {
        if (document.getElementById('paypal-button-container-...')) {
            window.paypal.Buttons({
                style: {
                    shape: 'rect',
                    color: 'black',
                    layout: 'vertical',
                    label: 'subscribe'
                },
                createSubscription: function(data, actions) {
                    return actions.subscription.create({
                        'plan_id': '...'
                    });
                },
                onApprove: function(data, actions) {
                    console.log("Zahlung erfolgreich genehmigt!", data);
                    alert(data.subscriptionID);
                },
                onCancel: function(data) {
                    console.log("Zahlung wurde vom Benutzer abgebrochen.", data);
                },
                onError: function(err) {
                    console.error("Beim Verarbeiten der Zahlung ist ein Fehler aufgetreten:", err);
                }
            }).render('#paypal-button-container-...');
        }
    }; // 3F8DR5FHWC74
    document.body.appendChild(script);
    return () => {
        document.body.removeChild(script);
    }
}, []);

reactjs paypal-rest-sdk paypal-subscriptions
1个回答
0
投票

在onApprove函数中,不要使用

alert()
。警报块。做点别的事吧。

对于服务器端通知,请订阅 Webhook 事件 PAYMENT.SALE.COMPLETED。

如果该通知本身不够快,还可以使用 onApprove 函数 调用服务器以使其执行 GET API 调用来检索订阅状态

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