Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Update payments to use the new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbasta committed May 3, 2013
1 parent fcd8de7 commit 7c7c5b4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 54 deletions.
4 changes: 3 additions & 1 deletion hearth/media/js/cache.js
Expand Up @@ -32,7 +32,9 @@ define('cache', ['rewriters', 'underscore'], function(rewriters, _) {
}

function bust(key) {
delete cache[key];
if (key in cache) {
delete cache[key];
}
}

function rewrite(matcher, worker, limit) {
Expand Down
10 changes: 8 additions & 2 deletions hearth/media/js/install.js
@@ -1,8 +1,8 @@
// Hey there! I know how to install apps. Buttons are dumb now.

define('install',
['apps', 'capabilities', 'jquery', 'login', 'notification', 'payments/payments', 'requests', 'urls', 'user', 'z'],
function(apps, caps, $, login, notification, payments, requests, urls, user, z) {
['apps', 'cache', 'capabilities', 'jquery', 'login', 'notification', 'payments/payments', 'requests', 'urls', 'user', 'z'],
function(apps, cache, caps, $, login, notification, payments, requests, urls, user, z) {
'use strict';

function _handler(func) {
Expand Down Expand Up @@ -48,6 +48,9 @@ define('install',
setTimeout(function() {
install(product);
}, 0);

// Bust the cache
cache.bust(urls.api.url('purchases'));
}

function purchaseError(product, msg) {
Expand Down Expand Up @@ -95,6 +98,9 @@ define('install',

function installSuccess(installer, product) {
z.win.trigger('app_install_success', [installer, product, true]);

// Bust the cache
cache.bust(urls.api.url('purchases'));
}

function installError(installer, product, msg) {
Expand Down
88 changes: 40 additions & 48 deletions hearth/media/js/payments/payments.js
@@ -1,91 +1,83 @@
define('payments/payments',
['capabilities', 'notification', 'requests'],
function(caps, notification, requests) {
['capabilities', 'notification', 'requests', 'settings', 'urls'],
function(caps, notification, requests, settings, urls) {

var product,
$def,
simulateNavPay = $('body').data('simulate-nav-pay');
var notify = notification.notification;

var _giveUp;
var _abortCheck = false;

var _giveUp;
function waitForPayment($def, product, webpayJWT, contribStatusURL) {
if (_abortCheck) {
return;
}
var selfArgs = arguments;
var nextCheck = window.setTimeout(function() {
var nextCheck = setTimeout(function() {
waitForPayment.apply(this, selfArgs);
}, 2000);
if (!_giveUp) {
_giveUp = window.setTimeout(function() {
_giveUp = setTimeout(function() {
_abortCheck = true;
$def.reject(null, product, 'MKT_INSTALL_ERROR');
}, 60000);
}
requests.get(contribStatusURL).done(function(result) {
requests.get(settings.api_url + urls.api.sign(contribStatusURL)).done(function(result) {
if (result.status == 'complete') {
window.clearTimeout(nextCheck);
window.clearTimeout(_giveUp);
clearTimeout(nextCheck);
clearTimeout(_giveUp);
$def.resolve(product);
}
}).fail(function() {
$def.reject(null, product, 'MKT_SERVER_ERROR');
});
}

if (simulateNavPay && !caps.navPay) {
if (settings.simulate_nav_pay && !caps.navPay) {
navigator.mozPay = function(jwts) {
var request = {
onsuccess: function() {
console.warning('handler did not define request.onsuccess');
console.warning('[payments][mock] handler did not define request.onsuccess');
},
onerror: function() {
console.warning('handler did not define request.onerror');
console.warning('[payments][mock] handler did not define request.onerror');
}
};
console.log('STUB navigator.mozPay received', jwts);
console.log('calling onsuccess() in 3 seconds...');
window.setTimeout(function() {
console.log('calling onsuccess()');
console.log('[payments][mock] STUB navigator.mozPay received', jwts);
console.log('[payments][mock] calling onsuccess() in 3 seconds...');
setTimeout(function() {
console.log('[payments][mock] calling onsuccess()');
request.onsuccess();
}, 3000);
return request;
};
console.log('stubbed out navigator.mozPay()');
}

function callNavPay($def, product, webpayJWT, contribStatusURL) {
var request = navigator.mozPay([webpayJWT]);
request.onsuccess = function() {
console.log('navigator.mozPay success');
waitForPayment($def, product, webpayJWT, contribStatusURL);
};
request.onerror = function() {
if (this.error.name !== 'cancelled') {
console.log('navigator.mozPay error:', this.error.name);
notification({
classes: 'error',
message: gettext('Payment failed. Try again later.'),
timeout: 5000
});
}
$def.reject(null, product, 'MKT_CANCELLED');
};
console.log('[payments] stubbed out navigator.mozPay()');
}

function beginPurchase(prod) {
if (!prod) return;
if ($def && $def.state() == 'pending') {
$def.reject(null, product, 'collision');
return;
}
$def = $.Deferred();
product = prod;
var $def = $.Deferred();
var product = prod;

console.log('[payments] Initiating transaction');

if (caps.navPay || simulateNavPay) {
requests.post(product.prepareNavPay, {}).done(function(result) {
callNavPay($def, product, result.webpayJWT, result.contribStatusURL);
if (caps.navPay || settings.simulate_nav_pay) {
requests.post(urls.api.url('prepare_nav_pay'), {app: product.slug}).done(function(result) {
console.log('[payments] Calling mozPay with JWT: ', result.webpayJWT);
var request = navigator.mozPay([result.webpayJWT]);
request.onsuccess = function() {
console.log('[payments] navigator.mozPay success');
waitForPayment($def, product, result.webpayJWT, result.contribStatusURL);
};
request.onerror = function() {
if (this.error.name !== 'cancelled') {
console.log('navigator.mozPay error:', this.error.name);
notify({
classes: 'error',
message: gettext('Payment failed. Try again later.'),
timeout: 5000
});
}
$def.reject(null, product, 'MKT_CANCELLED');
};
}).fail(function() {
$def.reject(null, product, 'MKT_SERVER_ERROR');
});
Expand Down
9 changes: 6 additions & 3 deletions hearth/media/js/urls.js
Expand Up @@ -48,18 +48,21 @@ define('urls',
'search': '/api/v1/apps/search/',
'feedback': '/api/v1/account/feedback/',
'terms_of_use': '/terms-of-use.html',
'privacy_policy': '/privacy-policy.html'
'privacy_policy': '/privacy-policy.html',

'prepare_nav_pay': '/api/v1/webpay/prepare/',
'payments_status': '/api/v1/webpay/status/{0}/'
};

var _device = _.once(function() {
var _device = function() {
if (caps.firefoxOS) {
return 'firefoxos';
} else if (caps.firefoxAndroid) {
return 'android';
} else {
return 'desktop';
}
});
};

var user = require('user');
function _userArgs(func) {
Expand Down

1 comment on commit 7c7c5b4

@mattbasta
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bug 867729)

Please sign in to comment.