首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Paypal Button -在弹出窗口打开之前添加操作

Paypal Button -在弹出窗口打开之前添加操作
EN

Stack Overflow用户
提问于 2018-08-10 05:29:33
回答 2查看 4.5K关注 0票数 0

我正在添加Paypal按钮到我的网站。但是,我想要做的是,在按钮运行之前,用户需要填写某些信息(地址框)。如果用户已经填写了信息,当点击Paypal按钮时,它会提示用户登录到Paypal (正常流程)。否则,用户将收到一个alertalert('Please choose a delivery address');

我一直在尝试做一些类似以下的事情:

代码语言:javascript
运行
复制
$('.paypal-button').on( "click", function() {
  alert('Please choose a delivery address');
});

甚至尝试禁用该按钮(尽管这不是最理想的),但发现它不能基于this other answer工作:

代码语言:javascript
运行
复制
$('.paypal-button').prop("disabled",true);

这是Paypal沙盒代码(你需要一个真正的沙盒凭证):

代码语言:javascript
运行
复制
<!DOCTYPE html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>

<body>
    <div id="paypal-button-container"></div>

    <script>
        paypal.Button.render({

            env: 'sandbox', // sandbox | production

            // PayPal Client IDs - replace with your own
            // Create a PayPal app: https://developer.paypal.com/developer/applications/create
            client: {
                sandbox:    'ABCDEcFpQtjWTOUtWKbyN_bDt4OgqatestlewfBP4-33iV8e1GWU6liB2XYZ',
                production: '<insert production client id>'
            },

            // Show the buyer a 'Pay Now' button in the checkout flow
            commit: true,

            // payment() is called when the button is clicked
            payment: function(data, actions) {

                // Make a call to the REST api to create the payment
                return actions.payment.create({
                    payment: {
                        transactions: [
                            {
                                amount: { total: '0.01', currency: 'USD' }
                            }
                        ]
                    }
                });
            },

            // onAuthorize() is called when the buyer approves the payment
            onAuthorize: function(data, actions) {

                // Make a call to the REST api to execute the payment
                return actions.payment.execute().then(function() {
                    window.alert('Payment Complete!');
                });
            }

        }, '#paypal-button-container');

    </script>
</body>
EN

回答 2

Stack Overflow用户

发布于 2019-03-28 02:37:02

new docs提供了onInitonClick选项,但是正如所讨论的here,您不能通过脚本触发弹出窗口,因为它会被阻止。因此,异步方法是错误的,因为它在验证失败时短暂地显示弹出窗口。我以这种方式实现了同步验证:

代码语言:javascript
运行
复制
onInit: function(data, actions) {
  // Disable the buttons
  actions.disable();
  // Listen for changes
  $("input[aria-required=true]").change(function() {
      var valid=true;
      $("input[aria-required=true]").each(function(){
          if($(this).val()=="") valid=false;
      });
      if (valid==true) {
        actions.enable();
      } else {
        actions.disable();
      }

  });       
}

我试图通过侦听器使用验证(wpcf7内置的),但似乎没有办法。

票数 1
EN

Stack Overflow用户

发布于 2018-08-13 21:03:53

您可以使用PayPal按钮的validate:onClick:属性。有一个example on the demo site说明了这是如何工作的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51776022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档