xaunlopez
xaunlopez

Reputation: 398

Parse Cloud httpRequest Stripe Subscriptions at_period_end param

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

Upvotes: 1

Views: 364

Answers (1)

David Bella
David Bella

Reputation: 831

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.

Upvotes: 1

Related Questions