2013-04-25 15:29:17 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-11-25 09:42:25 -08:00
|
|
|
Cu.import("resource://gre/modules/JNI.jsm");
|
2013-04-25 15:29:17 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
|
|
|
|
|
|
|
function paymentSuccess(aRequestId) {
|
2013-11-18 13:46:09 -08:00
|
|
|
return function(aResult) {
|
|
|
|
closePaymentTab(aRequestId, function() {
|
|
|
|
cpmm.sendAsyncMessage("Payment:Success", { result: aResult,
|
|
|
|
requestId: aRequestId });
|
|
|
|
});
|
|
|
|
}
|
2013-04-25 15:29:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function paymentFailed(aRequestId) {
|
2013-11-18 13:46:09 -08:00
|
|
|
return function(aErrorMsg) {
|
2013-04-25 15:29:17 -07:00
|
|
|
closePaymentTab(aRequestId, function() {
|
2013-11-18 13:46:09 -08:00
|
|
|
cpmm.sendAsyncMessage("Payment:Failed", { errorMsg: aErrorMsg,
|
2013-04-25 15:29:17 -07:00
|
|
|
requestId: aRequestId });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let paymentTabs = {};
|
2013-11-25 09:42:25 -08:00
|
|
|
let cancelTabCallbacks = {};
|
|
|
|
function paymentCanceled(aRequestId) {
|
|
|
|
return function() {
|
|
|
|
paymentFailed(aRequestId)();
|
|
|
|
}
|
|
|
|
}
|
2013-04-25 15:29:17 -07:00
|
|
|
function closePaymentTab(aId, aCallback) {
|
|
|
|
if (paymentTabs[aId]) {
|
2013-11-25 09:42:25 -08:00
|
|
|
paymentTabs[aId].browser.removeEventListener("TabClose", cancelTabCallbacks[aId]);
|
|
|
|
delete cancelTabCallbacks[aId];
|
|
|
|
|
2013-04-25 15:29:17 -07:00
|
|
|
// We ask the UI to close the selected payment flow.
|
|
|
|
let content = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
if (content) {
|
|
|
|
content.BrowserApp.closeTab(paymentTabs[aId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentTabs[aId] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
function PaymentUI() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PaymentUI.prototype = {
|
|
|
|
get bundle() {
|
2014-07-29 00:27:00 -07:00
|
|
|
if (!this._bundle) {
|
|
|
|
this._bundle = Services.strings.createBundle("chrome://browser/locale/payments.properties");
|
|
|
|
}
|
|
|
|
return this._bundle;
|
2013-04-25 15:29:17 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
confirmPaymentRequest: function confirmPaymentRequest(aRequestId,
|
|
|
|
aRequests,
|
|
|
|
aSuccessCb,
|
|
|
|
aErrorCb) {
|
|
|
|
let _error = this._error(aErrorCb);
|
|
|
|
|
|
|
|
let listItems = [];
|
|
|
|
|
|
|
|
// If there's only one payment provider that will work, just move on without prompting the user.
|
|
|
|
if (aRequests.length == 1) {
|
2014-02-10 05:48:55 -08:00
|
|
|
aSuccessCb.onresult(aRequestId, aRequests[0].type);
|
2013-04-25 15:29:17 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, let the user select a payment provider from a list.
|
|
|
|
for (let i = 0; i < aRequests.length; i++) {
|
2014-02-10 05:48:55 -08:00
|
|
|
let request = aRequests[i];
|
2013-04-25 15:29:17 -07:00
|
|
|
let requestText = request.providerName;
|
|
|
|
if (request.productPrice) {
|
|
|
|
requestText += " (" + request.productPrice[0].amount + " " +
|
|
|
|
request.productPrice[0].currency + ")";
|
|
|
|
}
|
2013-06-03 09:20:45 -07:00
|
|
|
listItems.push({ label: requestText });
|
2013-04-25 15:29:17 -07:00
|
|
|
}
|
|
|
|
|
2013-06-03 09:20:45 -07:00
|
|
|
let p = new Prompt({
|
|
|
|
window: null,
|
2013-04-25 15:29:17 -07:00
|
|
|
title: this.bundle.GetStringFromName("payments.providerdialog.title"),
|
2013-06-03 09:20:45 -07:00
|
|
|
}).setSingleChoiceItems(listItems).show(function(data) {
|
|
|
|
if (data.button > -1 && aSuccessCb) {
|
2014-02-10 05:48:55 -08:00
|
|
|
aSuccessCb.onresult(aRequestId, aRequests[data.button].type);
|
2013-06-03 09:20:45 -07:00
|
|
|
} else {
|
|
|
|
_error(aRequestId, "USER_CANCELED");
|
|
|
|
}
|
2013-04-25 15:29:17 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_error: function(aCallback) {
|
|
|
|
return function _error(id, msg) {
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback.onresult(id, msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
showPaymentFlow: function showPaymentFlow(aRequestId,
|
|
|
|
aPaymentFlowInfo,
|
|
|
|
aErrorCb) {
|
|
|
|
let _error = this._error(aErrorCb);
|
|
|
|
|
|
|
|
// We ask the UI to browse to the selected payment flow.
|
|
|
|
let content = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
if (!content) {
|
|
|
|
_error(aRequestId, "NO_CONTENT_WINDOW");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: For now, known payment providers (BlueVia and Mozilla Market)
|
|
|
|
// only accepts the JWT by GET, so we just add it to the URI.
|
|
|
|
// https://github.com/mozilla-b2g/gaia/blob/master/apps/system/js/payment.js
|
|
|
|
let tab = content.BrowserApp.addTab(aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
|
|
|
|
|
|
|
|
// Inject paymentSuccess and paymentFailed methods into the document after its loaded.
|
2014-02-05 12:15:44 -08:00
|
|
|
tab.browser.addEventListener("DOMWindowCreated", function loadPaymentShim() {
|
2013-04-25 15:29:17 -07:00
|
|
|
let frame = tab.browser.contentDocument.defaultView;
|
|
|
|
try {
|
2013-11-25 09:42:25 -08:00
|
|
|
frame.wrappedJSObject.mozPaymentProvider = {
|
|
|
|
__exposedProps__: {
|
|
|
|
paymentSuccess: 'r',
|
|
|
|
paymentFailed: 'r',
|
|
|
|
mnc: 'r',
|
|
|
|
mcc: 'r',
|
|
|
|
},
|
|
|
|
|
|
|
|
_getNetworkInfo: function(type) {
|
|
|
|
let jni = new JNI();
|
|
|
|
let cls = jni.findClass("org/mozilla/gecko/GeckoNetworkManager");
|
|
|
|
let method = jni.getStaticMethodID(cls, "get" + type.toUpperCase(), "()I");
|
|
|
|
let val = jni.callStaticIntMethod(cls, method);
|
|
|
|
jni.close();
|
|
|
|
|
|
|
|
if (val < 0)
|
|
|
|
return null;
|
|
|
|
return val;
|
|
|
|
},
|
|
|
|
|
|
|
|
get mnc() {
|
|
|
|
delete this.mnc;
|
|
|
|
return this.mnc = this._getNetworkInfo("mnc");
|
|
|
|
},
|
|
|
|
|
|
|
|
get mcc() {
|
|
|
|
delete this.mcc;
|
|
|
|
return this.mcc = this._getNetworkInfo("mcc");
|
|
|
|
},
|
|
|
|
|
|
|
|
paymentSuccess: paymentSuccess(aRequestId),
|
|
|
|
paymentFailed: paymentFailed(aRequestId)
|
|
|
|
};
|
2013-04-25 15:29:17 -07:00
|
|
|
} catch (e) {
|
|
|
|
_error(aRequestId, "ERROR_ADDING_METHODS");
|
|
|
|
} finally {
|
2014-02-05 12:15:44 -08:00
|
|
|
tab.browser.removeEventListener("DOMWindowCreated", loadPaymentShim);
|
2013-04-25 15:29:17 -07:00
|
|
|
}
|
2013-11-11 13:56:10 -08:00
|
|
|
}, true);
|
2013-04-25 15:29:17 -07:00
|
|
|
|
|
|
|
// Store a reference to the tab so that we can close it when the payment succeeds or fails.
|
|
|
|
paymentTabs[aRequestId] = tab;
|
2013-11-25 09:42:25 -08:00
|
|
|
cancelTabCallbacks[aRequestId] = paymentCanceled(aRequestId);
|
|
|
|
|
|
|
|
// Fail the payment if the tab is closed on its own
|
|
|
|
tab.browser.addEventListener("TabClose", cancelTabCallbacks[aRequestId]);
|
2013-04-25 15:29:17 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
cleanup: function cleanup() {
|
|
|
|
// Nothing to do here.
|
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{3c6c9575-f57e-427b-a8aa-57bc3cbff48f}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue])
|
|
|
|
}
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);
|