Skip to content
This repository was archived by the owner on Oct 19, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/js/index-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

const inAppPurchase = { utils };

const createIapError = (reject) => {
return (err = {}) => {
err.message = err.errorCode === 2 ? 'USER_CANCELLED' : err.message;
return reject(err);
};
};

const nativeCall = (name, args = []) => {
return new Promise((resolve, reject) => {
window.cordova.exec((res) => {
resolve(res);
}, (err) => {
reject(err);
}, 'PaymentsPlugin', name, args);
window.cordova.exec(resolve, createIapError(reject), 'PaymentsPlugin', name, args);
});
};

Expand Down
24 changes: 24 additions & 0 deletions test/index-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import assert from 'assert';

describe('iOS purchases', () => {

const execError = errorCode => (success, err, pluginName, name, args) => err({ errorCode });

before(() => {
GLOBAL.window = {};
GLOBAL.window.cordova = {};
Expand Down Expand Up @@ -163,6 +165,17 @@ describe('iOS purchases', () => {
}
});

it('should return a message of USER_CANCELLED when there is a PaymentCancelled error', async (done) => {
try {
GLOBAL.window.cordova.exec = execError(2)
await inAppPurchase.buy('com.test.prod1');
done(new Error('Call to #buy() suceeded but was expected to fail.'));
} catch (err) {
assert(err.message === 'USER_CANCELLED', 'should report USER_CANCELLED message');
done();
}
});

});

describe('#subscribe()', () => {
Expand All @@ -187,6 +200,17 @@ describe('iOS purchases', () => {
}
});

it('should return a message of USER_CANCELLED when there is a PaymentCancelled error', async (done) => {
try {
GLOBAL.window.cordova.exec = execError(2)
await inAppPurchase.subscribe('com.test.prod1');
done(new Error('Call to #subscribe() suceeded but was expected to fail.'));
} catch (err) {
assert(err.message === 'USER_CANCELLED', 'should report USER_CANCELLED message');
done();
}
});

});

describe('#consume()', () => {
Expand Down
15 changes: 10 additions & 5 deletions www/index-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ utils.validString = function (val) {

var inAppPurchase = { utils: utils };

var createIapError = function createIapError(reject) {
return function () {
var err = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

err.message = err.errorCode === 2 ? 'USER_CANCELLED' : err.message;
return reject(err);
};
};

var nativeCall = function nativeCall(name) {
var args = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

return new Promise(function (resolve, reject) {
window.cordova.exec(function (res) {
resolve(res);
}, function (err) {
reject(err);
}, 'PaymentsPlugin', name, args);
window.cordova.exec(resolve, createIapError(reject), 'PaymentsPlugin', name, args);
});
};

Expand Down