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 paymentCallback(aRequestId, "Payment:Success");
20 function paymentFailed(aRequestId) {
21 return paymentCallback(aRequestId, "Payment:Failed");
24 function paymentCallback(aRequestId, aMsg) {
25 return function(aResult) {
26 closePaymentWindow(aRequestId, function() {
27 cpmm.sendAsyncMessage(aMsg, { result: aResult,
28 requestId: aRequestId });
35 function closePaymentWindow(aId, aCallback) {
37 payments[aId].handled = true;
38 payments[aId].win.close();
45 function PaymentUI() {}
47 PaymentUI.prototype = {
48 classID: Components.ID("{ede1124f-72e8-4a31-9567-3270d46f21fb}"),
49 QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]),
51 confirmPaymentRequest: function(aRequestId, aRequests, aSuccessCb, aErrorCb) {
52 // If there's only one payment provider that will work, just move on
53 // without prompting the user.
54 if (aRequests.length == 1) {
55 aSuccessCb.onresult(aRequestId, aRequests[0].wrappedJSObject.type);
61 // Otherwise, let the user select a payment provider from a list.
62 for (let i = 0; i < aRequests.length; i++) {
63 let request = aRequests[i].wrappedJSObject;
64 let requestText = request.providerName;
65 if (request.productPrice && Array.isArray(request.productPrice)) {
66 // We should guess the user currency and use that instead.
67 requestText += " (" + request.productPrice[0].amount + " " +
68 request.productPrice[0].currency + ")";
70 items.push(requestText);
75 let bundle = Services.strings.
76 createBundle("chrome://webapprt/locale/webapp.properties");
77 let result = Services.prompt.
78 select(null, bundle.GetStringFromName("paymentDialog.title"),
79 bundle.GetStringFromName("paymentDialog.message"),
80 items.length, items, selected);
82 aSuccessCb.onresult(aRequestId,
83 aRequests[selected.value].wrappedJSObject.type);
85 aErrorCb.onresult(aRequestId, "USER_CANCELLED");
89 showPaymentFlow: function(aRequestId, aPaymentFlowInfo, aErrorCb) {
90 let win = Services.ww.
92 "chrome://webapprt/content/webapp.xul",
94 "chrome,dialog=no,resizable,scrollbars,centerscreen",
97 // Store a reference to the window so that we can close it when the payment
99 payments[aRequestId] = { win: win, handled: false };
101 // Inject paymentSuccess and paymentFailed methods into the document after
103 win.addEventListener("DOMContentLoaded", function onDOMContentLoaded() {
104 win.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
106 let browserElement = win.document.getElementById("content");
108 setAttribute("src", aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
110 browserElement.addEventListener("DOMWindowCreated",
111 function onDOMWindowCreated() {
112 browserElement.removeEventListener("DOMWindowCreated",
116 win.document.getElementById("content").contentDocument.defaultView
117 .wrappedJSObject.mozPaymentProvider = {
122 paymentSuccess: paymentSuccess(aRequestId),
123 paymentFailed: paymentFailed(aRequestId)
128 let winObserver = function(aClosedWin, aTopic) {
129 if (aTopic == "domwindowclosed") {
130 // Fail the payment if the window is closed.
131 if (aClosedWin == win) {
132 Services.ww.unregisterNotification(winObserver);
133 if (payments[aRequestId] && !payments[aRequestId].handled) {
134 aErrorCb.onresult(aRequestId, "USER_CANCELLED");
140 Services.ww.registerNotification(winObserver);
143 cleanup: function() {
147 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);