简体   繁体   中英

How to Window.alert when PayPal buy button clicked

I am integrating PayPal Smart buttons using PHP. When Buy button clicked, I want to make window.alert and ask something from user. How can I do it?

Here you see the second script when I write something inside of that one, buttons disappear

this is my script:

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

try this, work for me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM