Start a new topic

How do I return from custom endpoint?

I am processing a stripe payment in a custom endpoint which means I have to create a REST request, I am doing something like this:



req.request({uri: 'https://api.stripe.com/v1/customers/' + user.stripeCustomer.id + '/subscription',

method: 'POST',

form: {

plan: request.body.plan,

prorate: true

},

headers:{

Authorization:"Bearer " + secret

}},

function(error, res, body){

if (error){

response.body = {error: error.message};

response.complete(434);

} else {

collectionAccess.collection('user')

.update({"username": request.username},

{ $set: {

"plan": {id:request.body.plan},

"stripeCustomer": JSON.parse(body)

}

});

response.complete(res.status);

}

});

});



The problem is that it times out when I call it on the client. I can fix this by adding a response.complete(200) but then I will lose any returned values from the stripe API. How do I make the endpoint wait for the stripe api call to wait before I can return?

The problem is that there are a couple time consuming processes going on here. If the Stripe API is taking more than 2 seconds, it simply won't work.

Otherwise, you could separate out the processes, have the first request you're making return something that your collectionAccess uses to then update.
@caroline you are right but I wish there was an easier way to do this. I guess one work around is to add some sort of loop in my code to keep hitting kinvey until I get a success. something like



call kinvey to process payment

kinvey returns right away but keeps running the stripe call



my code checks once a second for 5 seconds

is stripe call successful, if yes proceed

if no, try again until 5 seconds elapsed, then assume failure



this is pretty bad and will probably end up adding more strain on your servers than the 2-second limit. not to mention all the extra code i have to write just to make it work and it won't be perfect.



What is the reasoning behind the 2 second limit and why 2 seconds? Do you have other customers integrating with stripe that are having this issues?



Any other suggestions?
Can you run some timing tests to Stripe outside of Kinvey to determine the typical amount of time for a request to be processed?
Login or Signup to post a comment