无法更新Square Cash应用中的总价值

发布于 2025-02-07 23:00:21 字数 3757 浏览 1 评论 0原文

我使用的是Square Web Dayments SDK,并且初始化现金应用程序付款。首先(在初始化阶段),我确定了总金额并初始化现金应用程序付费,这呈现现金应用程序付费按钮。这就是初始化的方式:

cashAppInitialize: function(payments) {
      var $this = this;
      new Promise(function (resolve, reject) {
        var amount = $this.getTotalAmount().toString();
        var currency = $this.getCurrency();
        
        var paymentRequest = payments.paymentRequest({
          countryCode: 'US',
          currencyCode: currency,
          total: {
            amount: amount,
            label: 'Total',
            pending: false
          }
        });
        var cashAppPay = payments.cashAppPay(paymentRequest, {
          redirectURL: window.location.href,
          referenceId: ((Math.random() + 1).toString(36).substring(7))+Math.random(),
        });
        if (cashAppPay) {
          resolve(cashAppPay);
        } else {
          reject(cashAppPay);
        }
      }).then(function (cashAppPay) {
        $this.cashAppPayInstance = cashAppPay;
        var buttonOptions = {
          shape: 'semiround',
          width: 'full',
          size: 'medium'
        };
        if ($$('#cash-app-pay') && $$('#cash-app-pay').length > 0) {
          cashAppPay.attach('#cash-app-pay', buttonOptions);
        }
        try {
          if (cashAppPay) {
            cashAppPay.addEventListener('ontokenization', (event) => {
              var { tokenResult } = event.detail;
              var tokenStatus = tokenResult.status;
              if (tokenStatus === 'OK') {
                $this.attachPaymentMethodToForm(tokenResult.details.method);
                $this.attachTokenToForm(tokenResult.token);
                $this.resubmitForm();
              }
            });
          }
        } catch (e) {
          var errorMessage = $this.getErrorMessage('cashApp', e);
          $this.handlePaymentMethodAlerts('cashApp', errorMessage);
          console.log(e);
        }
      }).catch(function (error) {
        var errorMessage = $this.getErrorMessage('cashApp', error);
        $this.handlePaymentMethodAlerts('cashApp', errorMessage);
        console.log(error);
      });
    },

运行此方法后,将在页面上渲染现金应用程序付费按钮,当我单击它时,我可以看到QR码进行付款。同时,我正在聆听上面的 Ontokenization 事件的响应,该事件不断发送带有IT中总金额的请求

“

我想做的就是这样做更新此“金额”值。但是,PaymentRequest实例中的“更新”方法始终返回 False 初始化现金应用付款时。因此,我无法更新总金额。

我尝试过的另一个解决方法是在 square.payments()。CashApppay中调用“销毁”方法。但是,它似乎也无法正常工作。尽管我从DOM销毁了相关的现金应用按钮,但我无法重新初步。在下面您可以找到我收到的控制台错误:

“

这就是我销毁和回忆 cashappinitialize 的方式。评论的代码应该更新金额值:

updatePaymentRequest: function() {
          var $this = this;
          var amount = paymentTotal.toString();

          if ($this.cashAppPayInstance) {
            $this.cashAppPayInstance.destroy().then(function() {
              $this.cashAppInitialize($this.payments);
            });
            return;
          }

          // var params = {
          //   countryCode: 'US',
          //   currencyCode: $this.getCurrency(),
          //   total: {
          //     amount: amount,
          //     label: 'Total',
          //     pending: false
          //   }
          // };

          // var updateSuccessful = $this.cashAppPaymentRequest.update(params);
          // console.log('sd', updateSuccessful);
        },

是否有可能更新发送给现金应用程序付费的总金额,并用更新的金额更改QR码?任何帮助都将受到赞赏。

I'm using Square web payments SDK and I initialize Cash App Pay. At first (in initialization phase), I determine the total amount and initialize Cash App Pay, which renders the Cash App Pay button. This is how it is initialized:

cashAppInitialize: function(payments) {
      var $this = this;
      new Promise(function (resolve, reject) {
        var amount = $this.getTotalAmount().toString();
        var currency = $this.getCurrency();
        
        var paymentRequest = payments.paymentRequest({
          countryCode: 'US',
          currencyCode: currency,
          total: {
            amount: amount,
            label: 'Total',
            pending: false
          }
        });
        var cashAppPay = payments.cashAppPay(paymentRequest, {
          redirectURL: window.location.href,
          referenceId: ((Math.random() + 1).toString(36).substring(7))+Math.random(),
        });
        if (cashAppPay) {
          resolve(cashAppPay);
        } else {
          reject(cashAppPay);
        }
      }).then(function (cashAppPay) {
        $this.cashAppPayInstance = cashAppPay;
        var buttonOptions = {
          shape: 'semiround',
          width: 'full',
          size: 'medium'
        };
        if ($('#cash-app-pay') && $('#cash-app-pay').length > 0) {
          cashAppPay.attach('#cash-app-pay', buttonOptions);
        }
        try {
          if (cashAppPay) {
            cashAppPay.addEventListener('ontokenization', (event) => {
              var { tokenResult } = event.detail;
              var tokenStatus = tokenResult.status;
              if (tokenStatus === 'OK') {
                $this.attachPaymentMethodToForm(tokenResult.details.method);
                $this.attachTokenToForm(tokenResult.token);
                $this.resubmitForm();
              }
            });
          }
        } catch (e) {
          var errorMessage = $this.getErrorMessage('cashApp', e);
          $this.handlePaymentMethodAlerts('cashApp', errorMessage);
          console.log(e);
        }
      }).catch(function (error) {
        var errorMessage = $this.getErrorMessage('cashApp', error);
        $this.handlePaymentMethodAlerts('cashApp', errorMessage);
        console.log(error);
      });
    },

After running this method, Cash App Pay button will be rendered on the page, and when I click on it, I'm able to see the QR code for payment. In the meantime, I'm listening the response from the ontokenization event above, which constantly sends a request with the total amount information in it

cashapptotal

What I'd like to do is that to update this 'amount' value. However, the 'update' method in paymentRequest instance always returns false when Cash App Pay is initialized. Thus, I am not able to update the total amount.

Another workaround I've tried is to call the 'destroy' method in Square.payments().cashAppPay. However, it also does not seem to work as expected. Although I destroyed the related Cash App button from the DOM, I was not able to reinitialize it. Below you can find the console errors I received:

error

This is how I destroy and recall cashAppInitialize. The commented out code was supposed to update the amount value:

updatePaymentRequest: function() {
          var $this = this;
          var amount = paymentTotal.toString();

          if ($this.cashAppPayInstance) {
            $this.cashAppPayInstance.destroy().then(function() {
              $this.cashAppInitialize($this.payments);
            });
            return;
          }

          // var params = {
          //   countryCode: 'US',
          //   currencyCode: $this.getCurrency(),
          //   total: {
          //     amount: amount,
          //     label: 'Total',
          //     pending: false
          //   }
          // };

          // var updateSuccessful = $this.cashAppPaymentRequest.update(params);
          // console.log('sd', updateSuccessful);
        },

Is it somehow possible to update the total amount that's sent to Cash App Pay, and change the QR code with the updated amount? Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2025-02-14 23:00:21

这对我有用,尽管更新您的代码以类似于 docs

        try{
            cashAppPay.attach('#cash-app-pay', buttonOptions);
        } catch (e) {
            console.log("Initialize error",e)
            //get the exact message and match it to the if statement
            //the exact error i got when trying to update the amount before destroying was as per my .match() in the if statement below
            console.log(e.message)
            const error_msg = e.message
            if(error_msg.match('already rendered')){
                await cashAppPay.destroy();
                await cashAppPay.attach('#cash-app-pay', buttonOptions);
            }
    }

基本上,您要销毁先前的QR码,并在升级金额值或任何所需选项时附加新的QR码。我只放置了正确的代码,以适合您的代码,如果您使用异步,请根据文档等待。然后,物品的写作和筑巢往往过多

。希望您找到比我的更好的解决方案。

This worked for me although update your code to resemble the docs

        try{
            cashAppPay.attach('#cash-app-pay', buttonOptions);
        } catch (e) {
            console.log("Initialize error",e)
            //get the exact message and match it to the if statement
            //the exact error i got when trying to update the amount before destroying was as per my .match() in the if statement below
            console.log(e.message)
            const error_msg = e.message
            if(error_msg.match('already rendered')){
                await cashAppPay.destroy();
                await cashAppPay.attach('#cash-app-pay', buttonOptions);
            }
    }

basically you are to destroy the previous qr code and attach a new one when upating the amount value or any of its required options. I've only placed the correct code to try suit your code if you use async await as per the docs. Thenables tend to be too much writing and nesting

Good luck. Hope you find a better solution than mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文