1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const { interfaces: Ci, utils: Cu } = Components;
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10 Cu.import("resource://gre/modules/Services.jsm");
12 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
13 "@mozilla.org/childprocessmessagemanager;1",
16 function paymentSuccess(aRequestId) {
17 return function(aResult) {
18 closePaymentWindow(aRequestId, function() {
19 cpmm.sendAsyncMessage("Payment:Success", { requestId: aRequestId,
25 function paymentFailed(aRequestId) {
26 return function(aErrorMsg) {
27 closePaymentWindow(aRequestId, function() {
28 cpmm.sendAsyncMessage("Payment:Failed", { requestId: aRequestId,
29 errorMsg: aErrorMsg });
36 function closePaymentWindow(aId, aCallback) {
38 payments[aId].handled = true;
39 payments[aId].win.close();
46 function PaymentUI() {}
48 PaymentUI.prototype = {
49 classID: Components.ID("{ede1124f-72e8-4a31-9567-3270d46f21fb}"),
50 QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]),
52 confirmPaymentRequest: function(aRequestId, aRequests, aSuccessCb, aErrorCb) {
53 // If there's only one payment provider that will work, just move on
54 // without prompting the user.
55 if (aRequests.length == 1) {
56 aSuccessCb.onresult(aRequestId, aRequests[0].type);
62 // Otherwise, let the user select a payment provider from a list.
63 for (let i = 0; i < aRequests.length; i++) {
64 let request = aRequests[i];
65 let requestText = request.providerName;
66 if (request.productPrice && Array.isArray(request.productPrice)) {
67 // We should guess the user currency and use that instead.
68 requestText += " (" + request.productPrice[0].amount + " " +
69 request.productPrice[0].currency + ")";
71 items.push(requestText);
76 let bundle = Services.strings.
77 createBundle("chrome://webapprt/locale/webapp.properties");
78 let result = Services.prompt.
79 select(null, bundle.GetStringFromName("paymentDialog.title"),
80 bundle.GetStringFromName("paymentDialog.message"),
81 items.length, items, selected);
83 aSuccessCb.onresult(aRequestId,
84 aRequests[selected.value].type);
86 aErrorCb.onresult(aRequestId, "USER_CANCELLED");
90 showPaymentFlow: function(aRequestId, aPaymentFlowInfo, aErrorCb) {
91 let win = Services.ww.
93 "chrome://webapprt/content/webapp.xul",
95 "chrome,dialog=no,resizable,scrollbars,centerscreen",
98 // Store a reference to the window so that we can close it when the payment
100 payments[aRequestId] = { win: win, handled: false };
102 // Inject paymentSuccess and paymentFailed methods into the document after
104 win.addEventListener("DOMContentLoaded", function onDOMContentLoaded() {
105 win.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
107 let browserElement = win.document.getElementById("content");
109 setAttribute("src", aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
111 browserElement.addEventListener("DOMWindowCreated",
112 function onDOMWindowCreated() {
113 browserElement.removeEventListener("DOMWindowCreated",
117 win.document.getElementById("content").contentDocument.defaultView
118 .wrappedJSObject.mozPaymentProvider = {
123 paymentSuccess: paymentSuccess(aRequestId),
124 paymentFailed: paymentFailed(aRequestId)
129 let winObserver = function(aClosedWin, aTopic) {
130 if (aTopic == "domwindowclosed") {
131 // Fail the payment if the window is closed.
132 if (aClosedWin == win) {
133 Services.ww.unregisterNotification(winObserver);
134 if (payments[aRequestId] && !payments[aRequestId].handled) {
135 aErrorCb.onresult(aRequestId, "USER_CANCELLED");
141 Services.ww.registerNotification(winObserver);
144 cleanup: function() {
148 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);