【问题标题】:PHP MySQL PayPal Integration - PayPal button does not work unless amount is inputted manuallyPHP MySQL PayPal 集成 - 除非手动输入金额,否则 PayPal 按钮不起作用
【发布时间】:2023-03-10 03:50:02
【问题描述】:

当金额值被预先声明时,当我点击“使用 PayPal 支付按钮”时没有任何反应。但是,当我手动编辑金额时,PayPal 按钮突然起作用了。

输入字段如下所示:

<input name="amountInput" type="number" id="amount" value="'.$amount.'">

这是我从数据库中获取变量的方法:

$gymid = $row['gymID'];
$status = $row['status'];
$now = date("Y-m-d h:i:s");
$date1 = $row['start_event'];
$date2 = $row['end_event'];
$date1format = date_create($row['start_event']);
$date2format = date_create($row['end_event']);
$diff = abs(strtotime($date1) - strtotime($date2));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24) / (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));

我取这样的金额:

$rate = $row2['gymRate'];
$amount = $hours * $rate;

整个脚本如下:

<script>
  function initPayPalButton() {
    var description = document.querySelector('#smart-button-container #description');
    var amount = document.querySelector('#smart-button-container #amount');
    var descriptionError = document.querySelector('#smart-button-container #descriptionError');
    var priceError = document.querySelector('#smart-button-container #priceLabelError');
    var invoiceid = document.querySelector('#smart-button-container #invoiceid');
    var invoiceidError = document.querySelector('#smart-button-container #invoiceidError');
    var invoiceidDiv = document.querySelector('#smart-button-container #invoiceidDiv');

    var elArr = [description, amount];

    if (invoiceidDiv.firstChild.innerHTML.length > 1) {
      invoiceidDiv.style.display = "block";
    }

    var purchase_units = [];
    purchase_units[0] = {};
    purchase_units[0].amount = {};

    function validate(event) {
      return event.value.length > 0;
    }

    paypal.Buttons({
      style: {
        color: 'gold',
        shape: 'pill',
        label: 'pay',
        layout: 'horizontal',
        
      },

      onInit: function (data, actions) {
        actions.disable();

        if(invoiceidDiv.style.display === "block") {
          elArr.push(invoiceid);
        }

        elArr.forEach(function (item) {
          item.addEventListener('keyup', function (event) {
            var result = elArr.every(validate);
            if (result) {
              actions.enable();
            } else {
              actions.disable();
            }
          });
        });
      },

      onClick: function () {
        if (description.value.length < 1) {
          descriptionError.style.visibility = "visible";
        } else {
          descriptionError.style.visibility = "hidden";
        }

        if (amount.value.length < 1) {
          priceError.style.visibility = "visible";
        } else {
          priceError.style.visibility = "hidden";
        }

        if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") {
          invoiceidError.style.visibility = "visible";
        } else {
          invoiceidError.style.visibility = "hidden";
        }

        purchase_units[0].description = description.value;
        purchase_units[0].amount.value = amount.value;

        if(invoiceid.value !== '') {
          purchase_units[0].invoice_id = invoiceid.value;
        }
      },

      createOrder: function (data, actions) {
        return actions.order.create({
          purchase_units: purchase_units,
        });
      },

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

      onError: function (err) {
        console.log(err);
      }
    }).render('#paypal-button-container');
  }

  setInterval(function(){
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","response.php",false);
        xmlhttp.send(null);
        var timer = document.getElementById("response").innerHTML=xmlhttp.responseText;
        //refreshes the table in the page once the timer expires
        if(timer == "00:00"){
          window.location.reload(true);
        }
      }, 1000);
  initPayPalButton();
  </script>

金额尚未编辑时的页面如下所示:

在文本字段中手动编辑金额后,按钮起作用:

显然,金额输入字段应该被禁用以防止用户编辑。

【问题讨论】:

    标签: javascript php mysql paypal


    【解决方案1】:

    我通过更改解决了这个问题:

    onInit: function (data, actions) {
            actions.disable();
    

    收件人:

    onInit: function (data, actions) {
            actions.enable();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-09
      • 2013-08-30
      • 2015-11-22
      • 2019-03-27
      • 2012-06-21
      • 2014-09-22
      • 2020-05-12
      • 1970-01-01
      相关资源
      最近更新 更多