Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscription deferral for Google play #59

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ iap.cancelSubscription("google", payment, function (error, response) {
});
```

### Subscription deferral ( Google Play only )

Google exposes [an API for deferral](https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/defer) of recurring suscriptions. This might be
be used to extend a user's subscription purchase until a specified future expiration time ( useful to grant your users some free days or months ).

```javascript
var deferralInfo = {
expectedExpiryTimeMillis: 1546616722237,
desiredExpiryTimeMillis: 1547716722237,
antonio0 marked this conversation as resolved.
Show resolved Hide resolved
};
iap.deferSubscription("google", payment, deferralInfo, function (error, response) {
/* your code */
});
```

## Supported platforms

### Amazon
Expand Down
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ exports.cancelSubscription = function (platform, payment, cb) {
cb(null, result);
});
};


exports.deferSubscription = function (platform, payment, deferralInfo, cb) {
function syncError(error) {
process.nextTick(function () {
cb(error);
});
}

if (!payment) {
return syncError(new Error('No payment given'));
}

var engine = platforms[platform];

if (!engine) {
return syncError(new Error('Platform ' + platform + ' not recognized'));
}

if (!engine.deferSubscription) {
return syncError(new Error('Platform ' + platform +
' does not have deferSubscription method'));
}

engine.deferSubscription(payment, deferralInfo, function (error, result) {
if (error) {
return cb(error);
}

cb(null, result);
});
};
59 changes: 59 additions & 0 deletions lib/google/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function validatePaymentAndParseKeyObject(payment) {
return keyObject;
}

function validateDeferralInfo(deferralInfo) {
assert.equal(typeof deferralInfo, 'object', 'deferralInfo must be an object');
assert.equal(typeof deferralInfo.expectedExpiryTimeMillis, 'number', 'expectedExpiryTimeMillis must be a number');
assert.equal(typeof deferralInfo.desiredExpiryTimeMillis, 'number', 'desiredExpiryTimeMillis must be a number');
assert(deferralInfo.desiredExpiryTimeMillis > deferralInfo.expectedExpiryTimeMillis, 'desiredExpiryTimeMillis must be greater than expectedExpiryTimeMillis');
}


exports.verifyPayment = function (payment, cb) {
var keyObject;

Expand Down Expand Up @@ -133,3 +141,54 @@ exports.cancelSubscription = function (payment, cb) {
});
});
};


exports.deferSubscription = function (payment, deferralInfo, cb) {
var keyObject;

try {
keyObject = validatePaymentAndParseKeyObject(payment);
validateDeferralInfo(deferralInfo);
} catch (error) {
return process.nextTick(function () {
cb(error);
});
}

jwt.getToken(keyObject.client_email, keyObject.private_key, apiUrls.publisherScope, function (error, token) {
if (error) {
return cb(error);
}

var requestUrl = apiUrls.purchasesSubscriptionsDefer(
payment.packageName,
payment.productId,
payment.receipt,
token
);

var options = {
json: {
deferralInfo
}
};
https.post(requestUrl, options, function (error, res, resultString) {
if (error) {
return cb(error);
}

if (res.statusCode !== 200) {
return cb(new Error('Received ' + res.statusCode + ' status code with body: ' + resultString));
}

var resultObject;
try {
resultObject = JSON.parse(resultString);
} catch (e) {
return cb(e);
}

return cb(null, resultObject);
});
});
};
14 changes: 12 additions & 2 deletions lib/google/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ exports.purchasesSubscriptionsGet = function (packageName, productId, receipt, a
);
};


// Android Subscriptions URLs & generators
exports.purchasesSubscriptionsCancel = function (packageName, productId, receipt, accessToken) {
var urlFormat = 'https://www.googleapis.com/androidpublisher/v2/applications/%s/purchases/subscriptions/%s/tokens/%s:cancel?access_token=%s';

Expand All @@ -51,3 +49,15 @@ exports.purchasesSubscriptionsCancel = function (packageName, productId, receipt
encodeURIComponent(accessToken) // API access token
);
};

exports.purchasesSubscriptionsDefer = function (packageName, productId, receipt, accessToken) {
var urlFormat = 'https://www.googleapis.com/androidpublisher/v2/applications/%s/purchases/subscriptions/%s/tokens/%s:defer?access_token=%s';

return util.format(
urlFormat,
encodeURIComponent(packageName), // application package name
encodeURIComponent(productId), // productId
encodeURIComponent(receipt), // purchase token
encodeURIComponent(accessToken) // API access token
);
};