我正在尝试运行https://www.youtube.com/watch?v=yelPlCVZLEE上所示的付款请求API的示例。
我遵循了他们描述的过程,并且我还运行了以下代码:

function go() {
   console.log('Pay');
   var request = new PaymentRequest([{
            supportedMethods:['urn:payment:visa','urn:payment:mc','urn:payment:amex']
         }],
         {
            total: {
            label: "Total due",
            amount: { currencyCode: "USD", value: "60.00" }, // US$60.00
         }
     }
   );

request.show()
  .then(function(response) {
    // process transaction response here
    return response.complete(true);
  })
  .then(function() {
    alert("Buy!");
  })
  .catch(function(e) {
    alert(e.name);
  });
 }


并且出现以下错误:未捕获的ReferenceError:未定义PaymentRequest。

如果我从运行测试:
http://github.adrianba.net/paymentrequest-demo/tests/payment-tests.html
据说已经定义好了。
我做错了什么?

最佳答案

您链接的站点http://github.adrianba.net/paymentrequest-demo/tests/payment-tests.html提取一个文件:

<script src="../lib/paymentrequest.js"></script>


定义了自己的PaymentRequest实现:

function PaymentRequest(methodData,details,options) {
  // Constructor code
  if(!Array.isArray(methodData) || methodData.length===0) throw new TypeError("methodData must be a non-empty sequence of PaymentMethodData");
  methodData.forEach(d => {
  ...


http://github.adrianba.net/paymentrequest-demo/lib/paymentrequest.js

要在Chrome中获取PaymentRequest,必须在chrome:// flags /#enable-experimental-web-platform-features中启用它

关于javascript - 未定义PaymentRequest API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37856138/

10-09 14:58