PayPal智能付款按钮 - 付款前验证运输地址?

发布于 2025-01-26 15:41:49 字数 2809 浏览 0 评论 0原文

PayPal允许添加一个没有数字的送货地址街。

据我所知,在付款完成之前,不可能在订单过程中检查此行为。

订单数据可在onapprove函数范围中可用。 但这为时已晚,因为付款完成了吗?

有没有办法添加地址验证?

<script src="https://www.paypal.com/sdk/js?client-id=abc123&disable-funding=credit,card,sepa,giropay&currency=EUR"></script>
<script>
paypal.Buttons({
    createOrder: function(data, actions) {
        return fetch('/paypal/order/create/', {
            method: 'post'
        }).then(function(res) {
            return res.json();
        }).then(function(orderData) {

            if (orderData.error)
            {
                return actions.reject();
            }

            return orderData.id;
        });
    },
    onShippingChange: function(data, actions) {
        
        if (data.shipping_address.country_code !== 'DE') 
        {
            return actions.reject();
        }

        return actions.resolve();
    },
    onApprove: function(data, actions) {
        
        return fetch('/paypal/order/' + data.orderID + '/capture/', {
            method: 'post'
        }).then(function(res) 
        {
            return res.json();
        }).then(function(orderData) {
        
            let re = /[0-9]/;
            
            // test
            if (re.exec(orderData.payer.address.address_line_1) === null)
            {
                // cancle payment on this place is to late, because payment is finished
            }

            var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
            
            if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
                return actions.restart(); 
            }

            if (errorDetail) 
            {
               return;
            }

            return fetch('/paypal/finish-order/' + data.orderID + '/',{
                headers: {
                    'Content-Type' : 'application/json',
                },
                body: JSON.stringify(orderData),
                method: 'post'
                }).then(function(result){
                    let dat = result.json();
                    return dat;
                }).then(function(data)
                {
                    if (data.error)
                    {
                        actions.reject();
                    }

                    if (data.destination)
                    {
                        actions.redirect(data.destination)
                    }
                });
        });
    },
    funding: {
        allowed: [ paypal.FUNDING.CARD ],
        disallowed: [ paypal.FUNDING.CREDIT ]
    },
    style: {
        size: 'medium',
        height: 40,
        label: 'checkout'
    }

}).render('#ppbut');
</script>

PayPal allows adding a delivery address street without a number.

As far as I know, it is not possible to check this behavior during the order process before the payment is finished.

The order data is available in the onApprove function scope.
But that's too late, because the payment is finished then?

Is there a way to add address validation?

<script src="https://www.paypal.com/sdk/js?client-id=abc123&disable-funding=credit,card,sepa,giropay¤cy=EUR"></script>
<script>
paypal.Buttons({
    createOrder: function(data, actions) {
        return fetch('/paypal/order/create/', {
            method: 'post'
        }).then(function(res) {
            return res.json();
        }).then(function(orderData) {

            if (orderData.error)
            {
                return actions.reject();
            }

            return orderData.id;
        });
    },
    onShippingChange: function(data, actions) {
        
        if (data.shipping_address.country_code !== 'DE') 
        {
            return actions.reject();
        }

        return actions.resolve();
    },
    onApprove: function(data, actions) {
        
        return fetch('/paypal/order/' + data.orderID + '/capture/', {
            method: 'post'
        }).then(function(res) 
        {
            return res.json();
        }).then(function(orderData) {
        
            let re = /[0-9]/;
            
            // test
            if (re.exec(orderData.payer.address.address_line_1) === null)
            {
                // cancle payment on this place is to late, because payment is finished
            }

            var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
            
            if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
                return actions.restart(); 
            }

            if (errorDetail) 
            {
               return;
            }

            return fetch('/paypal/finish-order/' + data.orderID + '/',{
                headers: {
                    'Content-Type' : 'application/json',
                },
                body: JSON.stringify(orderData),
                method: 'post'
                }).then(function(result){
                    let dat = result.json();
                    return dat;
                }).then(function(data)
                {
                    if (data.error)
                    {
                        actions.reject();
                    }

                    if (data.destination)
                    {
                        actions.redirect(data.destination)
                    }
                });
        });
    },
    funding: {
        allowed: [ paypal.FUNDING.CARD ],
        disallowed: [ paypal.FUNDING.CREDIT ]
    },
    style: {
        size: 'medium',
        height: 40,
        label: 'checkout'
    }

}).render('#ppbut');
</script>

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

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

发布评论

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

评论(1

拥抱影子 2025-02-02 15:41:49

当调用onapprove时,付款未完成。该订单仍需要捕获。

  • 在捕获之前,您可以根据需要显示订单审核步骤(例如,让客户验证新的总数,或从可用的运输方法中选择)。可以通过动态更新DOG来完成此步骤(重定向也可以工作,但最好避免)。
  • 如果地址无效,请相应地显示错误消息,并给出重新启动的选项;不要进行任何捕获。您不需要空白(没有方法),只需忘记该顺序ID而永远不会捕获它。
  • 您可以根据需要将订单修改为新的总计(根据需要,或基于您添加的审核步骤中的选择)。
  • 最后,继续捕获正常并显示成功。

如果您确实在批准后添加了用户审核步骤,则还应更改PayPal上的最后一个按钮,以说“继续”,而不是默认的“立即付款”以匹配您的行为。这只是文本更改,行为仅根据您在Onapprove中所做的事情进行更改,必须对应。要使文本更改,请在订单创建请求的顶级级别(不是在购买_units内部)中添加一个application_context对象,其中您在其中指定使用值继续的USER_ACTION参数。

When onApprove is called, the payment is not finished. The order still needs to be captured.

  • Before capture, you can show an order review step if desired (for the customer to verify a new total, for example, or select from available shipping methods). This step can be done by updating the DOM dynamically (a redirect can also work but is best avoided).
  • If the address is invalid, show an error message accordingly and give an option to restart; do not proceed with any capture. You do not need to void the order (there is no method), simply forget about that order id and never capture it.
  • You can patch the order to a new total as needed (automatically, or based on selections in the review step you add).
  • Finally, proceed with the capture as normal and show success.

If you do add a user review step after approval, you should also change the last button at PayPal to say "Continue" instead of the default "Pay Now" to match your behavior. This is only a text change, the behavior only changes based on what you do in onApprove, which must correspond. To make the text change, add an application_context object to the top level (not inside purchase_units) of your order creation request, in which you specify a user_action parameter with the value CONTINUE.

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