2016-06-16 38 views
0

我試圖運行支付請求API的示例,如https://www.youtube.com/watch?v=yelPlCVZLEE上所示。 我跟隨的過程,因爲他們描述和我也運行下面的代碼:PaymentRequest 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 這是說它的定義。 我做錯了什麼?

回答

1

你聯繫,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

+0

非常感謝您的回覆。我做了它,它的工作,但現在,按照https://www.w3.org/TR/payment-request/上的步驟,並通過執行以下調用API: –

+0

謝謝!我以爲我必須啓用'#web-payments'標誌 - 但不是!乾杯:) –

-1

非常感謝您的回覆。我做到了,它的工作,但現在,下面就https://www.w3.org/TR/payment-request/的步驟,並通過執行調用API後:

var supportedMethods=["visa","mastercard"]; 

var details = { 
    "items": [ 
{ 
    "id": "basket", 
    "label": "Sub-total", 
    "amount": { "currencyCode": "GBP", "value" : "55.00" }, // US$55.00 
}, 
{ 
    "id": "tax", 
    "label": "Sales Tax", 
    "amount": { "currencyCode": "GBP", "value" : "5.00" }, // US$5.00 
}, 
{ 
    "id": "total", 
    "label": "Total due", 
    "amount": { "currencyCode": "GBP", "value" : "60.00" }, // US$60.00 
} 
    ] 
}; 

var options = { 
    requestShipping: true 
}; 


//Constructing Payment Request 
var payment = new PaymentRequest(supportedMethods, details, options); 
payment.addEventListener("shippingaddresschange", function (changeEvent) { 
// Process shipping address change 
console.log("Constructing"); 
}); 

payment.show().then(function(paymentResponse) { 
    // Process paymentResponse 
    // paymentResponse.methodName contains the selected payment method 
    // paymentResponse.details contains a payment method specific response 
    paymentResponse.complete(true); 
    }).catch(function(err) { 
     console.error("Uh oh, something bad happened", err.message); 
    }); 

我被取消的請求。我正在使用本地機器。這是問題嗎?我是否應該實施https協議才能使其工作?我正在用我的筆記本電腦測試它,而不是用我的手機測試。 謝謝