单击PayPal购买按钮时如何Window.alert

时间:2019-05-17 20:39:54

标签: javascript php paypal

我正在使用PHP集成PayPal Smart按钮。单击“购买”按钮时,我要创建window.alert并向用户询问一些内容。我该怎么办?

在这里我看到第二个脚本时,按钮消失了

这是我的脚本:

 <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD"></script> 

<script>

    var name =  "<?php echo $_SESSION['name']; ?>"
    var surname = "<?php echo $_SESSION['surname']; ?>"
    var email_address = "<?php echo $_SESSION['email']; ?>"

    paypal.Buttons({

    createOrder: function(data, actions)
    {
        return actions.order.create
        ({
                purchase_units: [{
                                  amount: {
                                    value: '120'
                                  }
                                }],


                payer: {
                name: {
                    given_name: name,
                    surname: surname
                    },
                    email_address: email_address
                }


        });
    },

    onApprove: function(data, actions) 
    {
        return actions.order.capture().then(function(details) 
        {

            alert('Transaction completed by ' + details.payer.name.given_name);

        return fetch('/paypal-transaction-complete',
        {
              method: 'post',
              headers: 
              {
                'content-type': 'application/json'
              },
              body: JSON.stringify
              ({
                orderID: data.orderID
              })
        });
        });
    },

    onCancel: function (data)
    {   
          alert("You have cancelled the payment!");
          setTimeout("location.href = 'http://localhost/index.php';",100);    
    }

  }).render('#paypal-button-container');
  </script>

1 个答案:

答案 0 :(得分:0)

尝试一下,为我工作:

<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD"></script>
<script>
    var name = "<?php echo $_SESSION['name']; ?>"
    var surname = "<?php echo $_SESSION['surname']; ?>"
    var email_address = "<?php echo $_SESSION['email']; ?>"
    paypal.Buttons({
        createOrder: function(data, actions) {
            alert('you are Creating and Order see all the Content.');
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: '120'
                    }
                }],
                payer: {
                    name: {
                        given_name: name,
                        surname: surname
                    },
                    email_address: email_address
                }
            });
        },
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                alert('Transaction completed by ' + details.payer.name.given_name);
                return fetch('/paypal-transaction-complete', {
                    method: 'post',
                    headers: {
                        'content-type': 'application/json'
                    },
                    body: JSON.stringify({
                        orderID: data.orderID
                    })
                });
            });
        },
        onCancel: function(data) {
            alert("You have cancelled the payment!");
            setTimeout("location.href = 'http://localhost/index.php';", 100);
        }
    }).render('#paypal-button-container');
</script>