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

Commit

Permalink
Merge pull request #5 from kumar303/mozpay
Browse files Browse the repository at this point in the history
Provide callbacks for mozPay events (bug 981229)
  • Loading branch information
kumar303 committed Mar 11, 2014
2 parents 4f79ca2 + a222fe0 commit fc6ca73
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/fxpay.js
Expand Up @@ -8,6 +8,7 @@
throw err;
}
};
opt.oncheckpayment = opt.oncheckpayment || function() {};
opt.log = opt.log || window.console;
opt.mozPay = opt.mozPay || navigator.mozPay;
opt.maxTries = opt.maxTries || undefined;
Expand All @@ -33,14 +34,16 @@

var payReq = opt.mozPay([data.webpayJWT]);

payReq.onerror = function() {
log.error('mozPay: received onerror()');
// TODO: fire a callback here for apps.
payReq.onerror = function(err) {
log.error('mozPay: received onerror():', err);
opt.onpurchase(err);
};

payReq.onsuccess = function() {
log.debug('mozPay: received onsuccess()');
// TODO: fire a callback here for apps so they can update their UI.
// The payment flow has closed. Let's wait for
// payment verification.
opt.oncheckpayment();

function onTransaction(err, data) {
if (err) {
Expand Down
55 changes: 55 additions & 0 deletions tests/test-fxpay.js
Expand Up @@ -97,6 +97,61 @@ describe('fxpay', function () {
server.respond();
});

it('should call back when mozPay window closes', function (done) {

fxpay.purchase(123, {
oncheckpayment: function() {
done();
},
onpurchase: function(err) {
// Make sure we don't have an unexpected error.
assert.equal(err, null)
},
mozPay: mozPay
});

// Respond to fetching the JWT.
server.respondWith(
'POST',
/.*payments\/in\-app\/purchase\/product\/123/,
[200, {"Content-Type": "application/json"},
JSON.stringify({webpayJWT: '<jwt>',
contribStatusURL: '/transaction/XYZ'})]);
server.respond();

mozPay.returnValues[0].onsuccess(); // resolve the DOM request.

// Respond to polling the transaction.
server.respondWith(
'POST',
/.*\/transaction\/XYZ/,
[200, {"Content-Type": "application/json"},
JSON.stringify({state: 'COMPLETED'})]);
server.respond();
});

it('should call back with mozPay error', function (done) {

fxpay.purchase(123, {
onpurchase: function(err) {
assert.equal(err, 'DIALOG_CLOSED_BY_USER');
done();
},
mozPay: mozPay
});

// Respond to fetching the JWT.
server.respondWith(
'POST',
/.*payments\/in\-app\/purchase\/product\/123/,
[200, {"Content-Type": "application/json"},
JSON.stringify({webpayJWT: '<jwt>',
contribStatusURL: '/transaction/XYZ'})]);
server.respond();

mozPay.returnValues[0].onerror('DIALOG_CLOSED_BY_USER');
});

it('should report invalid transaction state', function (done) {
var productId = 123;

Expand Down

0 comments on commit fc6ca73

Please sign in to comment.