1
votes

I have been trying to cancel Stripe Plans through Parse Cloud Code with limited success. The problem I have been having is with the CC query to get the users Stripe ID, which I have saved in a Parse class called Payments. In the Payments class I also have a pointer to the user class with the user's Parse objectid.

I am very new to Java Script, Parse and Cloud code. So this might have a very easy fix. But I have spent a few days on this and read every post I could find but have had no success.

Here is the code I am using:

Parse.Cloud.define("cancel", function(request, response) {
var user = request.user;
var payments = Parse.Object.extend("Payments");
var query = new Parse.Query(payments);
query.include("parent")
query.equalTo("parent", {
                "__type": "Pointer",
                "className": "_user",
                "objectId": user.id
                });
query.find({
    success: function(results) {
          for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var customerStripeId = object.get("stripeID");
          var key = 'sk_test_ONbnGBHHcsmOMFtSifV3JD2S';
          var url = "https://api.stripe.com/v1/customers/" + customerStripeId + "/subscription";
            Parse.Cloud.httpRequest({
                method: 'DELETE',
                params: { at_period_end: true, key: key },
                url: url,
                success: function() {
                    response.success()
                },
                error: function(httpResponse) {
                    console.error('Delete failed with response code ' + httpResponse.status + "for" + user.id +" "+ customerStripeId);
                    response.failure()
                }}
            });
      },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
      }
    });
});

I have also tried getting the query using this code with no success:

Parse.Cloud.define("cancel", function(request, response) {
var user = request.user;
var payments = Parse.Object.extend("Payments");
var query = new Parse.Query(payments);
query.include("parent")
query.equalTo("parent", {
                "__type": "Pointer",
                "className": "_user",
                "objectId": user.id
                });

var customerStripeId = query.get("stripeID");
var key = 'sk_test_ONbnGBHHcsmOMFtSifV3JD2S';
var url = "https://api.stripe.com/v1/customers/" + customerStripeId + "/subscription";


Parse.Cloud.httpRequest({
    method: 'DELETE',
    params: { at_period_end: true, key: key },
    url: url,
    success: function() {
        response.success()
    },
    error: function(httpResponse) {
        console.error('Delete failed with response code ' + httpResponse.status + "for" + user.id +" "+ customerStripeId);
        response.error()
    }
});

});

So again, my problem is with setting the customerStripeId var. The rest of the code works well, I know this because if I manually put in a users stripe ID in the code for customerStripeId (and skip the query all together). The function works.

I would really appreciate any help with this issue.

Thanks in advance :)

1
In your cmd parse logs, try to look at the message saying sth like "Run cloud function cancel by user xxx".. Verify that the object id is correct in your case by looking it up in your browser data. - Ralphilius
Thanks for the comment Ralphilius. The objectId was correct. I now believe the problem was with the pointer. The first script in my question works like a charm now. cheers - Alirt

1 Answers

0
votes

For any one else with the same problem, I have finally figured it out. I believe the problem was with the pointer. I am now using "query.equalTo("parent", user);" and the cancellation is going through with no issues.