1

Having trouble getting the format right with Parse.Cloud.httpRequest for deleting a subscription at_period_end.

I am able to successfully make this request with PostMan using x-www-form-urlencoded, key 'at_period_end' value true. (Can't post a screenshot due to my reputation sorry)

Here is my cloud-code:

Parse.Cloud.httpRequest({
    method  : 'DELETE',
    url     : 'https://' + skey + ':@' + 'api.stripe.com/v1' + '/customers/' + request.params.customerId + '/subscriptions/' + request.params.subscriptionId,
    body    : {
      "at_period_end": true
    },
    success: function(httpResponse) {
      if (httpResponse.status === 200) {
        response.success(httpResponse);
      }
      else {
        response.error(httpResponse);
      }
    },
    error: function(httpResponse) {
      response.error(httpResponse);
    }
  });

I have played around with adding a headers object with Content-Type set, but unsuccessful.

I think this is just a formatting translation issue from what I correctly entered into PostMan, to what is in my httpRequest object...

I also can't find any great information on docs on the httpRequest method so its quite frustrating :(.

Thanks heaps

***** EDIT ****** SOLUTION:

Managed to solve this using url inline parameters:

  var options = request.params.options,
      url     = 'https://' + skey + ':@api.stripe.com/v1/customers/' + request.params.customerId + '/subscriptions/' + request.params.subscriptionId,
      keys;

  keys = Object.keys(options);

  // This is disgusting, I need to know a better way.
  for (var i = 0; i < keys.length; i++)
  {
    if (i === 0)
    {
      url += '?';
    }
    url += keys[i] + '=' + options[keys[i]];
    if (i !== keys.length - 1)
    {
      url += '&';
    }
  }

  Parse.Cloud.httpRequest({
    method  : 'DELETE',
    url     : url,
    success: function(httpResponse) {
      if (httpResponse.status === 200) {
        response.success(httpResponse);
      }
      else {
        response.error(httpResponse);
      }
    },
    error: function(httpResponse) {
      response.error(httpResponse);
    }
  });

if anyone could show me a better way to write this, that would be epic :)

Cheers

xaunlopez
  • 439
  • 1
  • 4
  • 13
  • Take a look at your Stripe logs (on the dashboard) and see what is actually being sent along. Does the sub get deleted? I wonder if parse isn't understanding `method: 'DELETE'` – rfunduk Apr 28 '15 at 19:24
  • Yeah the subscription does get deleted, just not with the param at_period_end. – xaunlopez Apr 30 '15 at 04:38
  • I managed to solve this with the inline style e.g ?at_period_end=true.. still... annoying to have to do that. – xaunlopez Apr 30 '15 at 04:39

1 Answers1

1

This one has always been particularly thorny for me, here is what I've been using that has worked:

Parse.Cloud.httpRequest({
  method: 'DELETE',
  url: 'https://api.stripe.com/v1/customers/' + request.params.stripeId + '/subscriptions/' + request.params.stripeSubscriptionId,
  headers: {
    'Authorization': 'Basic BASE_64_ENCODE_SECRET_KEY'
  },
  params: {
    at_period_end: true
  },
  success: function(httpResponse) {
    ...
  },
  error: function(httpResponse) {
    ...
  }
});

A couple of extra details here.

  • I initially had "Content-Type: application/json" as one of the headers, but this appears to not be correct, despite (I think) needing it in the past.
  • The base64 encode of your key can be generated with

    echo -e 'sk_live_ABCDEF123456:' | openssl base64

Don't forget the colon (:) at the end of the key, it matters.

This is just a detail however, and it looks like putting the secret key directly in the URL is working as well.

David Bella
  • 831
  • 5
  • 8