j3k0 / cordova-non-renewing-subscription

Simple API for Non-Renewing Subscriptions based on Fovea's Cordova Purchase Plugin
MIT License
19 stars 6 forks source link

saving and loading from backend server #19

Closed kedai closed 6 years ago

kedai commented 7 years ago

Hi Could not grasp how to go about this.

for saveExpiryDate: what do we return from the backend? posted the userid, server save the expiryDate, and return - what? in json format?

for loadExpiryDate: get from server, server check the db for userid and expiryDate: if successful - what to return? (error='null', date={'dateExpiry':'10/20/2017 at 9:17:02 PM'}) if not - ?

Thanks for the help

cenkdominic commented 6 years ago

Hey, did you ever solve this?

jussihuotari commented 6 years ago

You can return whatever you need/want from your backend. You parse the server's response in the success section of your ajax call or pass the response to your callback function for parsing.

For example, in the docs the loadExpiryDate function "parses" the expiry date string from the server's json response (data.expiryDate), and passes the date string to the callback function (callback(null, data.expiryDate);).

In this case the server's json response would be something like {'expiryDate': '2018-01-22'} for a successful request and some http status error code, e.g. 404, for an unsuccessful request.

The relevant excerpt from the doc:

var loadExpiryDate = function(callback) {
    $.ajax({
        type: 'GET',
        url: 'http://somewhere.com/something.php?user_id=12345',
        success: function(data) {
            callback(null, data.expiryDate);
        },
        error: function() {
            callback('An error occurred');
        }
    })
};

For a save request the server could simply respond with an ok 200 or error 404 http status code.