webapprt/PaymentUIGlue.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const { interfaces: Ci, utils: Cu } = Components;
michael@0 8
michael@0 9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 10 Cu.import("resource://gre/modules/Services.jsm");
michael@0 11
michael@0 12 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
michael@0 13 "@mozilla.org/childprocessmessagemanager;1",
michael@0 14 "nsIMessageSender");
michael@0 15
michael@0 16 function paymentSuccess(aRequestId) {
michael@0 17 return function(aResult) {
michael@0 18 closePaymentWindow(aRequestId, function() {
michael@0 19 cpmm.sendAsyncMessage("Payment:Success", { requestId: aRequestId,
michael@0 20 result: aResult });
michael@0 21 });
michael@0 22 };
michael@0 23 }
michael@0 24
michael@0 25 function paymentFailed(aRequestId) {
michael@0 26 return function(aErrorMsg) {
michael@0 27 closePaymentWindow(aRequestId, function() {
michael@0 28 cpmm.sendAsyncMessage("Payment:Failed", { requestId: aRequestId,
michael@0 29 errorMsg: aErrorMsg });
michael@0 30 });
michael@0 31 };
michael@0 32 }
michael@0 33
michael@0 34 let payments = {};
michael@0 35
michael@0 36 function closePaymentWindow(aId, aCallback) {
michael@0 37 if (payments[aId]) {
michael@0 38 payments[aId].handled = true;
michael@0 39 payments[aId].win.close();
michael@0 40 payments[aId] = null;
michael@0 41 }
michael@0 42
michael@0 43 aCallback();
michael@0 44 }
michael@0 45
michael@0 46 function PaymentUI() {}
michael@0 47
michael@0 48 PaymentUI.prototype = {
michael@0 49 classID: Components.ID("{ede1124f-72e8-4a31-9567-3270d46f21fb}"),
michael@0 50 QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]),
michael@0 51
michael@0 52 confirmPaymentRequest: function(aRequestId, aRequests, aSuccessCb, aErrorCb) {
michael@0 53 // If there's only one payment provider that will work, just move on
michael@0 54 // without prompting the user.
michael@0 55 if (aRequests.length == 1) {
michael@0 56 aSuccessCb.onresult(aRequestId, aRequests[0].type);
michael@0 57 return;
michael@0 58 }
michael@0 59
michael@0 60 let items = [];
michael@0 61
michael@0 62 // Otherwise, let the user select a payment provider from a list.
michael@0 63 for (let i = 0; i < aRequests.length; i++) {
michael@0 64 let request = aRequests[i];
michael@0 65 let requestText = request.providerName;
michael@0 66 if (request.productPrice && Array.isArray(request.productPrice)) {
michael@0 67 // We should guess the user currency and use that instead.
michael@0 68 requestText += " (" + request.productPrice[0].amount + " " +
michael@0 69 request.productPrice[0].currency + ")";
michael@0 70 }
michael@0 71 items.push(requestText);
michael@0 72 }
michael@0 73
michael@0 74 let selected = {};
michael@0 75
michael@0 76 let bundle = Services.strings.
michael@0 77 createBundle("chrome://webapprt/locale/webapp.properties");
michael@0 78 let result = Services.prompt.
michael@0 79 select(null, bundle.GetStringFromName("paymentDialog.title"),
michael@0 80 bundle.GetStringFromName("paymentDialog.message"),
michael@0 81 items.length, items, selected);
michael@0 82 if (result) {
michael@0 83 aSuccessCb.onresult(aRequestId,
michael@0 84 aRequests[selected.value].type);
michael@0 85 } else {
michael@0 86 aErrorCb.onresult(aRequestId, "USER_CANCELLED");
michael@0 87 }
michael@0 88 },
michael@0 89
michael@0 90 showPaymentFlow: function(aRequestId, aPaymentFlowInfo, aErrorCb) {
michael@0 91 let win = Services.ww.
michael@0 92 openWindow(null,
michael@0 93 "chrome://webapprt/content/webapp.xul",
michael@0 94 "_blank",
michael@0 95 "chrome,dialog=no,resizable,scrollbars,centerscreen",
michael@0 96 null);
michael@0 97
michael@0 98 // Store a reference to the window so that we can close it when the payment
michael@0 99 // succeeds or fails.
michael@0 100 payments[aRequestId] = { win: win, handled: false };
michael@0 101
michael@0 102 // Inject paymentSuccess and paymentFailed methods into the document after
michael@0 103 // its loaded.
michael@0 104 win.addEventListener("DOMContentLoaded", function onDOMContentLoaded() {
michael@0 105 win.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
michael@0 106
michael@0 107 let browserElement = win.document.getElementById("content");
michael@0 108 browserElement.
michael@0 109 setAttribute("src", aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
michael@0 110
michael@0 111 browserElement.addEventListener("DOMWindowCreated",
michael@0 112 function onDOMWindowCreated() {
michael@0 113 browserElement.removeEventListener("DOMWindowCreated",
michael@0 114 onDOMWindowCreated);
michael@0 115
michael@0 116
michael@0 117 win.document.getElementById("content").contentDocument.defaultView
michael@0 118 .wrappedJSObject.mozPaymentProvider = {
michael@0 119 __exposedProps__: {
michael@0 120 paymentSuccess: 'r',
michael@0 121 paymentFailed: 'r'
michael@0 122 },
michael@0 123 paymentSuccess: paymentSuccess(aRequestId),
michael@0 124 paymentFailed: paymentFailed(aRequestId)
michael@0 125 };
michael@0 126 }, true);
michael@0 127 });
michael@0 128
michael@0 129 let winObserver = function(aClosedWin, aTopic) {
michael@0 130 if (aTopic == "domwindowclosed") {
michael@0 131 // Fail the payment if the window is closed.
michael@0 132 if (aClosedWin == win) {
michael@0 133 Services.ww.unregisterNotification(winObserver);
michael@0 134 if (payments[aRequestId] && !payments[aRequestId].handled) {
michael@0 135 aErrorCb.onresult(aRequestId, "USER_CANCELLED");
michael@0 136 }
michael@0 137 }
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 Services.ww.registerNotification(winObserver);
michael@0 142 },
michael@0 143
michael@0 144 cleanup: function() {
michael@0 145 },
michael@0 146 }
michael@0 147
michael@0 148 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);

mercurial