Nexus上的Android Pay Mobile Web意外错误

时间:2017-12-11 16:26:33

标签: javascript android android-pay

我开始尝试让Android付费在我们的某个网站上运行。我已经设置了一个基本的设置脚本来在测试站点上创建一个vanilla付款。

在用户点击触发Android Pay的元素之前,一切似乎都很顺利。当用户点击时,操作系统会“向上滑动”,但在显示任何内容之前它就会消失,我会收到错误消息:

“申请失败

发生了意外错误。请稍后再试。“

控制台或调试器中没有任何内容可以指示错误的性质。

这是JS:

<script>

    if (window.PaymentRequest) {

        var request = createPaymentRequest();

      request.canMakePayment()
          .then(function (result) {
            if (result) {
              addPayWithGoogleButton();
            }
          })
          .catch(function (err) {
            showErrorForDebugging(
                'canMakePayment() error! ' + err.name + ' error: ' + err.message);
          });
    } else {
      showErrorForDebugging('PaymentRequest API not available.');
    }

    /**
     * Add a purchase button alongside the existing checkout option
     */
    function addPayWithGoogleButton() {
      var payWithGoogleButton = document.createElement('button');
      payWithGoogleButton.appendChild(document.createTextNode('Purchase'));
      payWithGoogleButton.addEventListener('click', onPayWithGoogleClicked);
      document.getElementById('checkout').appendChild(payWithGoogleButton);
    }

    /**
     * Show purchase chooser when buy button clicked
     */
    function onPayWithGoogleClicked() {
      createPaymentRequest()
          .show()
          .then(function(response) {
            // Dismiss payment dialog
            response.complete('success');
            handlePaymentResponse(response);
          })
          .catch(function(err) {
            showErrorForDebugging('show() error! ' + err.name + ' error: ' + err.message);
          });
    }

    /**
     * Configure support for the Google Payments API
     *
     * @returns {object} data attribute suitable for PaymentMethodData
     */
    function getGooglePaymentsConfiguration() {
      return {
        // Merchant ID available after approval by Google.
        // 'merchantId':'01234567890123456789',
        'environment': 'TEST',
        'apiVersion': 1,
        'allowedPaymentMethods': ['CARD', 'TOKENIZED_CARD'],
        'paymentMethodTokenizationParameters': {
          'tokenizationType': 'PAYMENT_GATEWAY',
          // Check with your payment gateway on the parameters to pass.
          'parameters': {}
        },
        'cardRequirements': {
          'allowedCardNetworks': ['AMEX', 'DISCOVER', 'MASTERCARD', 'VISA'],
          'billingAddressRequired': true,
          'billingAddressFormat': 'MIN'
        },
        'phoneNumberRequired': true,
        'emailRequired': true,
        'shippingAddressRequired': true
      };
    }

    /**
     * Create a PaymentRequest
     *
     * @returns {PaymentRequest}
     */
    function createPaymentRequest() {
      // Support Google Payment API.
      var methodData = [{
        'supportedMethods': ['https://google.com/pay'],
        'data': getGooglePaymentsConfiguration()
      }];

      var details = {
        total: {label: 'Test Purchase', amount: {currency: 'USD', value: '1.00'}}
      };

      return new PaymentRequest(methodData, details);
    }

    /**
     * Process a PaymentResponse
     *
     * @param {PaymentResponse} response returned when user approves the purchase request
     */
    function handlePaymentResponse(response) {
      var formattedResponse = document.createElement('pre');
      formattedResponse.appendChild(
          document.createTextNode(JSON.stringify(response.toJSON(), null, 2)));
      document.getElementById('checkout')
          .insertAdjacentElement('afterend', formattedResponse);
    }

    /**
     * Display an error message
     *
     * @param {string} text message to display
     */
    function showErrorForDebugging(text) {
      var errorDisplay = document.createElement('code');
      errorDisplay.style.color = 'red';
      errorDisplay.appendChild(document.createTextNode(text));
      var p = document.createElement('p');
      p.appendChild(errorDisplay);
      document.getElementById('checkout').insertAdjacentElement('afterend', p);
    }
</script>

1 个答案:

答案 0 :(得分:1)

对于遇到此问题的其他人 - 教程代码似乎有问题。我们通过更改代码来修复它:

function createPaymentRequest() {
  // Support Google Payment API.
  var methodData = [{
    'supportedMethods': ['https://google.com/pay'],
    'data': getGooglePaymentsConfiguration()
  }];

  var details = {
    total: {label: 'Test Purchase', amount: {currency: 'USD', value: '1.00'}}
  };

  return new PaymentRequest(methodData, details);
}

到此:

function createPaymentRequest() {
      // Support Google Payment API.
      var methodData = [{
        'supportedMethods': 'basic-card',
        'data': getGooglePaymentsConfiguration()
      }];

      var details = {
        total: {label: 'Test Purchase', amount: {currency: 'USD', value: '1.00'}}
      };

      return new PaymentRequest(methodData, details);
    }