【问题标题】:Paypal Button - Add action before popup window opens [duplicate]Paypal 按钮 - 在弹出窗口打开之前添加操作 [重复]
【发布时间】:2019-01-17 10:48:40
【问题描述】:

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

我一直在尝试执行以下操作:

$('.paypal-button').on( "click", function() {
  alert('Please choose a delivery address');
});

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

$('.paypal-button').prop("disabled",true);

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

<!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>

【问题讨论】:

    标签: javascript paypal alert sandbox disable


    【解决方案1】:

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

    【讨论】:

    • 似乎曾经有一些关于链接验证的内容,但现在没有了
    【解决方案2】:

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

    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();
          }
    
      });       
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多