2012-08-29 14:41:34 -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");
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [];
|
2012-08-29 14:41:34 -07:00
|
|
|
|
|
|
|
const PAYMENT_IPC_MSG_NAMES = ["Payment:Pay",
|
|
|
|
"Payment:Success",
|
|
|
|
"Payment:Failed"];
|
|
|
|
|
|
|
|
const PREF_PAYMENTPROVIDERS_BRANCH = "dom.payment.provider.";
|
2012-12-06 13:34:11 -08:00
|
|
|
const PREF_PAYMENT_BRANCH = "dom.payment.";
|
2012-08-29 14:41:34 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "prefService",
|
|
|
|
"@mozilla.org/preferences-service;1",
|
|
|
|
"nsIPrefService");
|
|
|
|
|
|
|
|
function debug (s) {
|
|
|
|
//dump("-*- PaymentManager: " + s + "\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
let PaymentManager = {
|
|
|
|
init: function init() {
|
|
|
|
// Payment providers data are stored as a preference.
|
|
|
|
this.registeredProviders = null;
|
|
|
|
|
|
|
|
this.messageManagers = {};
|
|
|
|
|
2012-12-06 13:34:11 -08:00
|
|
|
// The dom.payment.skipHTTPSCheck pref is supposed to be used only during
|
|
|
|
// development process. This preference should not be active for a
|
|
|
|
// production build.
|
|
|
|
let paymentPrefs = prefService.getBranch(PREF_PAYMENT_BRANCH);
|
|
|
|
this.checkHttps = true;
|
|
|
|
try {
|
|
|
|
if (paymentPrefs.getPrefType("skipHTTPSCheck")) {
|
|
|
|
this.checkHttps = !paymentPrefs.getBoolPref("skipHTTPSCheck");
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
|
2012-08-29 14:41:34 -07:00
|
|
|
for each (let msgname in PAYMENT_IPC_MSG_NAMES) {
|
|
|
|
ppmm.addMessageListener(msgname, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a message from the content process.
|
|
|
|
*/
|
|
|
|
receiveMessage: function receiveMessage(aMessage) {
|
|
|
|
let name = aMessage.name;
|
|
|
|
let msg = aMessage.json;
|
|
|
|
debug("Received '" + name + "' message from content process");
|
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case "Payment:Pay": {
|
|
|
|
// First of all, we register the payment providers.
|
|
|
|
if (!this.registeredProviders) {
|
|
|
|
this.registeredProviders = {};
|
|
|
|
this.registerPaymentProviders();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We save the message target message manager so we can later dispatch
|
|
|
|
// back messages without broadcasting to all child processes.
|
2012-12-03 12:44:58 -08:00
|
|
|
let requestId = msg.requestId;
|
|
|
|
this.messageManagers[requestId] = aMessage.target;
|
2012-08-29 14:41:34 -07:00
|
|
|
|
|
|
|
// We check the jwt type and look for a match within the
|
|
|
|
// registered payment providers to get the correct payment request
|
|
|
|
// information.
|
|
|
|
let paymentRequests = [];
|
|
|
|
let jwtTypes = [];
|
|
|
|
for (let i in msg.jwts) {
|
2012-12-03 12:44:58 -08:00
|
|
|
let pr = this.getPaymentRequestInfo(requestId, msg.jwts[i]);
|
2012-08-29 14:41:34 -07:00
|
|
|
if (!pr) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-07 16:02:10 -08:00
|
|
|
if (!(pr instanceof Ci.nsIDOMPaymentRequestInfo)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-29 14:41:34 -07:00
|
|
|
// We consider jwt type repetition an error.
|
|
|
|
if (jwtTypes[pr.type]) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(requestId,
|
|
|
|
"PAY_REQUEST_ERROR_DUPLICATED_JWT_TYPE");
|
2012-08-29 14:41:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
jwtTypes[pr.type] = true;
|
|
|
|
paymentRequests.push(pr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!paymentRequests.length) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(requestId,
|
|
|
|
"PAY_REQUEST_ERROR_NO_VALID_REQUEST_FOUND");
|
2012-08-29 14:41:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After getting the list of valid payment requests, we ask the user
|
|
|
|
// for confirmation before sending any request to any payment provider.
|
|
|
|
// If there is more than one choice, we also let the user select the one
|
|
|
|
// that he prefers.
|
|
|
|
let glue = Cc["@mozilla.org/payment/ui-glue;1"]
|
|
|
|
.createInstance(Ci.nsIPaymentUIGlue);
|
|
|
|
if (!glue) {
|
|
|
|
debug("Could not create nsIPaymentUIGlue instance");
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(requestId,
|
|
|
|
"INTERNAL_ERROR_CREATE_PAYMENT_GLUE_FAILED");
|
2012-08-29 14:41:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-03 12:44:58 -08:00
|
|
|
let confirmPaymentSuccessCb = function successCb(aRequestId,
|
|
|
|
aResult) {
|
2012-08-29 14:41:34 -07:00
|
|
|
// Get the appropriate payment provider data based on user's choice.
|
|
|
|
let selectedProvider = this.registeredProviders[aResult];
|
|
|
|
if (!selectedProvider || !selectedProvider.uri) {
|
|
|
|
debug("Could not retrieve a valid provider based on user's " +
|
|
|
|
"selection");
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"INTERNAL_ERROR_NO_VALID_SELECTED_PROVIDER");
|
2012-08-29 14:41:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let jwt;
|
|
|
|
for (let i in paymentRequests) {
|
|
|
|
if (paymentRequests[i].type == aResult) {
|
|
|
|
jwt = paymentRequests[i].jwt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!jwt) {
|
|
|
|
debug("The selected request has no JWT information associated");
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"INTERNAL_ERROR_NO_JWT_ASSOCIATED_TO_REQUEST");
|
2012-08-29 14:41:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-03 12:44:58 -08:00
|
|
|
this.showPaymentFlow(aRequestId, selectedProvider, jwt);
|
2012-08-29 14:41:34 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let confirmPaymentErrorCb = this.paymentFailed;
|
|
|
|
|
2012-12-03 12:44:58 -08:00
|
|
|
glue.confirmPaymentRequest(requestId,
|
|
|
|
paymentRequests,
|
2012-08-29 14:41:34 -07:00
|
|
|
confirmPaymentSuccessCb.bind(this),
|
|
|
|
confirmPaymentErrorCb.bind(this));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Payment:Success":
|
|
|
|
case "Payment:Failed": {
|
2012-12-03 12:44:58 -08:00
|
|
|
let mm = this.messageManagers[msg.requestId];
|
2012-08-29 14:41:34 -07:00
|
|
|
mm.sendAsyncMessage(name, {
|
2012-12-03 12:44:58 -08:00
|
|
|
requestId: msg.requestId,
|
2012-08-29 14:41:34 -07:00
|
|
|
result: msg.result,
|
|
|
|
errorMsg: msg.errorMsg
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to register payment providers stored as preferences.
|
|
|
|
*/
|
|
|
|
registerPaymentProviders: function registerPaymentProviders() {
|
|
|
|
let paymentProviders = prefService
|
|
|
|
.getBranch(PREF_PAYMENTPROVIDERS_BRANCH)
|
|
|
|
.getChildList("");
|
|
|
|
|
|
|
|
// First get the numbers of the providers by getting all ###.uri prefs.
|
|
|
|
let nums = [];
|
|
|
|
for (let i in paymentProviders) {
|
|
|
|
let match = /^(\d+)\.uri$/.exec(paymentProviders[i]);
|
|
|
|
if (!match) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
nums.push(match[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now register the payment providers.
|
|
|
|
for (let i in nums) {
|
|
|
|
let branch = prefService
|
|
|
|
.getBranch(PREF_PAYMENTPROVIDERS_BRANCH + nums[i] + ".");
|
|
|
|
let vals = branch.getChildList("");
|
|
|
|
if (vals.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let type = branch.getCharPref("type");
|
|
|
|
if (type in this.registeredProviders) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this.registeredProviders[type] = {
|
|
|
|
name: branch.getCharPref("name"),
|
|
|
|
uri: branch.getCharPref("uri"),
|
|
|
|
description: branch.getCharPref("description"),
|
|
|
|
requestMethod: branch.getCharPref("requestMethod")
|
|
|
|
};
|
|
|
|
debug("Registered Payment Providers: " +
|
|
|
|
JSON.stringify(this.registeredProviders[type]));
|
|
|
|
} catch (ex) {
|
|
|
|
debug("An error ocurred registering a payment provider. " + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for sending a Payment:Failed message to the parent process.
|
|
|
|
*/
|
2012-12-03 12:44:58 -08:00
|
|
|
paymentFailed: function paymentFailed(aRequestId, aErrorMsg) {
|
|
|
|
let mm = this.messageManagers[aRequestId];
|
2012-08-29 14:41:34 -07:00
|
|
|
mm.sendAsyncMessage("Payment:Failed", {
|
2012-12-03 12:44:58 -08:00
|
|
|
requestId: aRequestId,
|
2012-08-29 14:41:34 -07:00
|
|
|
errorMsg: aErrorMsg
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to get the payment request info according to the jwt
|
|
|
|
* type. Payment provider's data is stored as a preference.
|
|
|
|
*/
|
2012-12-03 12:44:58 -08:00
|
|
|
getPaymentRequestInfo: function getPaymentRequestInfo(aRequestId, aJwt) {
|
2012-08-29 14:41:34 -07:00
|
|
|
if (!aJwt) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId, "INTERNAL_ERROR_CALL_WITH_MISSING_JWT");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// First thing, we check that the jwt type is an allowed type and has a
|
|
|
|
// payment provider flow information associated.
|
|
|
|
|
|
|
|
// A jwt string consists in three parts separated by period ('.'): header,
|
|
|
|
// payload and signature.
|
|
|
|
let segments = aJwt.split('.');
|
|
|
|
if (segments.length !== 3) {
|
|
|
|
debug("Error getting payment provider's uri. " +
|
|
|
|
"Not enough or too many segments");
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"PAY_REQUEST_ERROR_WRONG_SEGMENTS_COUNT");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let payloadObject;
|
|
|
|
try {
|
|
|
|
// We only care about the payload segment, which contains the jwt type
|
|
|
|
// that should match with any of the stored payment provider's data and
|
|
|
|
// the payment request information to be shown to the user.
|
2013-02-19 00:19:40 -08:00
|
|
|
// Before decoding the JWT string we need to normalize it to be compliant
|
|
|
|
// with RFC 4648.
|
|
|
|
segments[1] = segments[1].replace("-", "+", "g").replace("_", "/", "g");
|
2012-08-29 14:41:34 -07:00
|
|
|
let payload = atob(segments[1]);
|
|
|
|
debug("Payload " + payload);
|
2012-11-07 16:02:10 -08:00
|
|
|
if (!payload.length) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId, "PAY_REQUEST_ERROR_EMPTY_PAYLOAD");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-29 14:41:34 -07:00
|
|
|
payloadObject = JSON.parse(payload);
|
2012-11-07 16:02:10 -08:00
|
|
|
if (!payloadObject) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"PAY_REQUEST_ERROR_ERROR_PARSING_JWT_PAYLOAD");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-29 14:41:34 -07:00
|
|
|
} catch (e) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"PAY_REQUEST_ERROR_ERROR_DECODING_JWT");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
|
|
|
|
2012-11-07 16:02:10 -08:00
|
|
|
if (!payloadObject.typ) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"PAY_REQUEST_ERROR_NO_TYP_PARAMETER");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!payloadObject.request) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"PAY_REQUEST_ERROR_NO_REQUEST_PARAMETER");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once we got the jwt 'typ' value we look for a match within the payment
|
2012-11-07 16:02:10 -08:00
|
|
|
// providers stored preferences. If the jwt 'typ' is not recognized as one
|
|
|
|
// of the allowed values for registered payment providers, we skip the jwt
|
|
|
|
// validation but we don't fire any error. This way developers might have
|
|
|
|
// a default set of well formed JWTs that might be used in different B2G
|
|
|
|
// devices with a different set of allowed payment providers.
|
2012-08-29 14:41:34 -07:00
|
|
|
let provider = this.registeredProviders[payloadObject.typ];
|
2012-11-07 16:02:10 -08:00
|
|
|
if (!provider) {
|
2012-08-29 14:41:34 -07:00
|
|
|
debug("Not registered payment provider for jwt type: " +
|
|
|
|
payloadObject.typ);
|
2012-11-07 16:02:10 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!provider.uri || !provider.name) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"INTERNAL_ERROR_WRONG_REGISTERED_PAY_PROVIDER");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We only allow https for payment providers uris.
|
2012-12-06 13:34:11 -08:00
|
|
|
if (this.checkHttps && !/^https/.exec(provider.uri.toLowerCase())) {
|
2012-11-07 16:02:10 -08:00
|
|
|
// We should never get this far.
|
2012-08-29 14:41:34 -07:00
|
|
|
debug("Payment provider uris must be https: " + provider.uri);
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"INTERNAL_ERROR_NON_HTTPS_PROVIDER_URI");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let pldRequest = payloadObject.request;
|
2012-11-07 16:02:10 -08:00
|
|
|
let request = Cc["@mozilla.org/payment/request-info;1"]
|
|
|
|
.createInstance(Ci.nsIDOMPaymentRequestInfo);
|
|
|
|
if (!request) {
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"INTERNAL_ERROR_ERROR_CREATING_PAY_REQUEST");
|
2012-11-07 16:02:10 -08:00
|
|
|
return true;
|
2012-08-29 14:41:34 -07:00
|
|
|
}
|
2012-11-07 16:02:10 -08:00
|
|
|
request.wrappedJSObject.init(aJwt,
|
|
|
|
payloadObject.typ,
|
|
|
|
provider.name);
|
2012-08-29 14:41:34 -07:00
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2012-12-03 12:44:58 -08:00
|
|
|
showPaymentFlow: function showPaymentFlow(aRequestId,
|
|
|
|
aPaymentProvider,
|
|
|
|
aJwt) {
|
2012-08-29 14:41:34 -07:00
|
|
|
let paymentFlowInfo = Cc["@mozilla.org/payment/flow-info;1"]
|
|
|
|
.createInstance(Ci.nsIPaymentFlowInfo);
|
|
|
|
paymentFlowInfo.uri = aPaymentProvider.uri;
|
|
|
|
paymentFlowInfo.requestMethod = aPaymentProvider.requestMethod;
|
|
|
|
paymentFlowInfo.jwt = aJwt;
|
|
|
|
|
|
|
|
let glue = Cc["@mozilla.org/payment/ui-glue;1"]
|
|
|
|
.createInstance(Ci.nsIPaymentUIGlue);
|
|
|
|
if (!glue) {
|
|
|
|
debug("Could not create nsIPaymentUIGlue instance");
|
2012-12-03 12:44:58 -08:00
|
|
|
this.paymentFailed(aRequestId,
|
|
|
|
"INTERNAL_ERROR_CREATE_PAYMENT_GLUE_FAILED");
|
2012-08-29 14:41:34 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-03 12:44:58 -08:00
|
|
|
glue.showPaymentFlow(aRequestId,
|
|
|
|
paymentFlowInfo,
|
|
|
|
this.paymentFailed.bind(this));
|
2012-08-29 14:41:34 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
observe: function observe(subject, topic, data) {
|
|
|
|
if (topic == "xpcom-shutdown") {
|
|
|
|
for each (let msgname in PAYMENT_IPC_MSG_NAMES) {
|
|
|
|
ppmm.removeMessageListener(msgname, this);
|
|
|
|
}
|
|
|
|
this.registeredProviders = null;
|
|
|
|
this.messageManagers = null;
|
|
|
|
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
PaymentManager.init();
|