Skip to content

Commit

Permalink
Add support for custom headers to validator call
Browse files Browse the repository at this point in the history
  • Loading branch information
mcroker committed Jan 29, 2021
1 parent eda72d7 commit baae021
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ declare namespace IapStore {
(product: IStoreProduct, callback: IValidatorCallback): void;
}

export interface IValidatorTarget {
url: string;
headers?: { [token: string]: string };
}

/** See https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#storeorderproduct-additionaldata for details */
export type IAndroidProrationMode =
'IMMEDIATE_WITH_TIME_PRORATION'
Expand Down Expand Up @@ -269,13 +274,14 @@ declare namespace IapStore {
* Set this attribute to either:
* - the URL of your purchase validation service: Fovea's receipt validator (https://billing.fovea.cc) or your own service.
* - a custom validation callback method.
* - an object contaning url and headers { url: 'https//...', headers: { Authorization: 'Bearer ' + authToken } }
*
* Note that a receipt validation server is required to properly handle subscriptions and refunds.
*
* See https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#-storevalidator
*/
validator: string | IValidator;

validator: string | IValidator | IValidatorTarget;
/**
* Set to true to automatically clean up the queue of transactions.
*
Expand Down
6 changes: 6 additions & 0 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ store.utils = {
if (xhr.readyState === 4)
store.utils.callExternal('ajax.done', doneCb);
};
if (options.customHeaders) {
Object.keys(options.customHeaders).forEach(function (header) {
store.log.debug('ajax -> adding custom header: ' + header );
xhr.setRequestHeader( header, options.customHeaders[header]);
});
}
xhr.setRequestHeader("Accept", "application/json");
store.log.debug('ajax -> send request to ' + options.url);
if (options.data) {
Expand Down
6 changes: 4 additions & 2 deletions src/js/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
/// ** Validation error codes are [documented here](#validation-error-codes).
///
store.validator = null;
store.validatorCustomHeaders = null;

var validationRequests = [];
var timeout = null;
Expand Down Expand Up @@ -164,8 +165,9 @@ function runValidation() {

// Post
store.utils.ajax({
url: store.validator,
url: (typeof store.validator === 'string') ? store.validator : store.validator.url,
method: 'POST',
customHeaders: (typeof store.validator === 'string') ? null : store.validator.headers,
data: data,
success: function(data) {
store.log.debug("validator success, response: " + JSON.stringify(data));
Expand Down Expand Up @@ -313,7 +315,7 @@ store._validator = function(product, callback, isPrepared) {
return;
}

if (typeof store.validator === 'string') {
if (typeof store.validator === 'string' || typeof store.validator === 'object') {
validationRequests.push({
product: product,
callback: callback
Expand Down
12 changes: 10 additions & 2 deletions www/store-android.js
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ store.off = function(callback) {
/// ** Validation error codes are [documented here](#validation-error-codes).
///
store.validator = null;
store.validatorCustomHeaders = null;

var validationRequests = [];
var timeout = null;
Expand Down Expand Up @@ -1656,8 +1657,9 @@ function runValidation() {

// Post
store.utils.ajax({
url: store.validator,
url: (typeof store.validator === 'string') ? store.validator : store.validator.url,
method: 'POST',
customHeaders: (typeof store.validator === 'string') ? null : store.validator.headers,
data: data,
success: function(data) {
store.log.debug("validator success, response: " + JSON.stringify(data));
Expand Down Expand Up @@ -1805,7 +1807,7 @@ store._validator = function(product, callback, isPrepared) {
return;
}

if (typeof store.validator === 'string') {
if (typeof store.validator === 'string' || typeof store.validator === 'object') {
validationRequests.push({
product: product,
callback: callback
Expand Down Expand Up @@ -2829,6 +2831,12 @@ store.utils = {
if (xhr.readyState === 4)
store.utils.callExternal('ajax.done', doneCb);
};
if (options.customHeaders) {
Object.keys(options.customHeaders).forEach(function (header) {
store.log.debug('ajax -> adding custom header: ' + header );
xhr.setRequestHeader( header, options.customHeaders[header]);
});
}
xhr.setRequestHeader("Accept", "application/json");
store.log.debug('ajax -> send request to ' + options.url);
if (options.data) {
Expand Down
12 changes: 10 additions & 2 deletions www/store-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,7 @@ store.off = function(callback) {
/// ** Validation error codes are [documented here](#validation-error-codes).
///
store.validator = null;
store.validatorCustomHeaders = null;

var validationRequests = [];
var timeout = null;
Expand Down Expand Up @@ -1677,8 +1678,9 @@ function runValidation() {

// Post
store.utils.ajax({
url: store.validator,
url: (typeof store.validator === 'string') ? store.validator : store.validator.url,
method: 'POST',
customHeaders: (typeof store.validator === 'string') ? null : store.validator.headers,
data: data,
success: function(data) {
store.log.debug("validator success, response: " + JSON.stringify(data));
Expand Down Expand Up @@ -1826,7 +1828,7 @@ store._validator = function(product, callback, isPrepared) {
return;
}

if (typeof store.validator === 'string') {
if (typeof store.validator === 'string' || typeof store.validator === 'object') {
validationRequests.push({
product: product,
callback: callback
Expand Down Expand Up @@ -2850,6 +2852,12 @@ store.utils = {
if (xhr.readyState === 4)
store.utils.callExternal('ajax.done', doneCb);
};
if (options.customHeaders) {
Object.keys(options.customHeaders).forEach(function (header) {
store.log.debug('ajax -> adding custom header: ' + header );
xhr.setRequestHeader( header, options.customHeaders[header]);
});
}
xhr.setRequestHeader("Accept", "application/json");
store.log.debug('ajax -> send request to ' + options.url);
if (options.data) {
Expand Down
12 changes: 10 additions & 2 deletions www/store-windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ store.off = function(callback) {
/// ** Validation error codes are [documented here](#validation-error-codes).
///
store.validator = null;
store.validatorCustomHeaders = null;

var validationRequests = [];
var timeout = null;
Expand Down Expand Up @@ -1656,8 +1657,9 @@ function runValidation() {

// Post
store.utils.ajax({
url: store.validator,
url: (typeof store.validator === 'string') ? store.validator : store.validator.url,
method: 'POST',
customHeaders: (typeof store.validator === 'string') ? null : store.validator.headers,
data: data,
success: function(data) {
store.log.debug("validator success, response: " + JSON.stringify(data));
Expand Down Expand Up @@ -1805,7 +1807,7 @@ store._validator = function(product, callback, isPrepared) {
return;
}

if (typeof store.validator === 'string') {
if (typeof store.validator === 'string' || typeof store.validator === 'object') {
validationRequests.push({
product: product,
callback: callback
Expand Down Expand Up @@ -2829,6 +2831,12 @@ store.utils = {
if (xhr.readyState === 4)
store.utils.callExternal('ajax.done', doneCb);
};
if (options.customHeaders) {
Object.keys(options.customHeaders).forEach(function (header) {
store.log.debug('ajax -> adding custom header: ' + header );
xhr.setRequestHeader( header, options.customHeaders[header]);
});
}
xhr.setRequestHeader("Accept", "application/json");
store.log.debug('ajax -> send request to ' + options.url);
if (options.data) {
Expand Down

0 comments on commit baae021

Please sign in to comment.