Page 1 of 3

How to charge Stripe customer from server code.

Posted: Wed Jan 28, 2015 9:49 pm
by Jon Haider

I'm trying to use server code to charge an already created customer using reference to the customer# (not the card token). Here's what I've tried (unsuccessfully):
pre
var url = "https://api.stripe.com/v1/charges/";
var postData = {
"parameters": {
"currency": "usd",
"amount": 1200,
"customer": "cus_XXXXXXXXXXXX"
},
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer sk_test_XXXXXXXXXXXXXXXXXXXXX"
}
};
var XHRResponse = XHR.send("POST", url, postData);
response.success(XHRResponse, "json");
/pre

I then noticed that Appery uses a proxy to invoke the Stripe_charge service. Can I call the Appery proxy from server code? If so, how would I do that, passing the appropriate json info about the transaction?

pre
var StripePayment_Settings = {
"apiKey": "sk_test_XXXXXXXXXXXXXXXXXXXXX",
"token": "token"
}

var url = "https://api.appery.io/rest/1/proxy/tunnel";
var postData = {
"proxyHeaders": {
'appery-proxy-url': 'https://api.stripe.com/v1/charges',
'appery-transformation': 'checkTunnel', //is this where I'd send a json object?
'appery-key': '1422474530915',
'appery-rest': '2615010'
},
'dataType': 'json',
'type': 'post',
'contentType': 'application/x-www-form-urlencoded',
'serviceSettings': StripePayment_Settings
};

var XHRResponse = XHR.send("POST", url, postData);
response.success(XHRResponse, "JSON");

/pre

Any help would be appreciated...
Thank you for the direction!


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 7:43 pm
by Illya Stepanov

Hi Jon -

[quote:]Can I call the Appery proxy from server code?[/quote]
Server code is actually a proxy, cause it doesn't have same headers like browser do when it makes a request to 3rd party service.


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 7:45 pm
by Jon Haider

Thank you, Illya.

So, is it possible to call Stripe api from server? I just need to initiate the "Charge" function from the server.
Do you know if this feasible?


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 7:56 pm
by Illya Stepanov

Yes, this is possible.


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 7:59 pm
by Jon Haider

Great. Any direction and where I can look for more info?
As mentioned above, I've tried using an XHR call, but this doesn't work.

pre
var url = "https://api.stripe.com/v1/charges/";
var postData = {
"parameters": {
"currency": "usd",
"amount": 1200,
"customer": "cus_XXXXXXXXXXXX"
},
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer sk_test_XXXXXXXXXXXXXXXXXXXXX"
}
};
var XHRResponse = XHR.send("POST", url, postData);
response.success(XHRResponse, "json");
/pre

Also, from what I understand, the Stripe API works with js-node, but it looks like the Appery server doesn't...


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 9:37 pm
by Jon Haider

Something seems odd. My script is only 15 lines long, and I get this error:

"29.1.2015, 1:34:41 PM: Script chargeCustomer: Error ( @ 43 : 18 ) - throw e;"

How can I have an error @43 when my script is only 14 lines? Is the error from Appery's XHR function?

When I use XHR2, I don't get this error, I get a
"Unrecognized request URL (POST: /v1/charges/). Please see https://stripe.com/docs"

This is seriously frustrating.


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 10:02 pm
by Jon Haider

Update: it is currently working. I had two errors. 1, I should use the XHR2 command, and 2, I needed to remove the last slash in the URL.

This code now works:

pre
var url = "https://api.stripe.com/v1/charges";
var postData = {
"parameters": {
"currency": "usd",
"amount": 1200,
"customer": "cus_XXXXXXXXXXXX"
},
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer sk_test_XXXXXXXXXXXXXXXXXXXXX"
}
};
var XHRResponse = XHR2.send("POST", url, postData);
response.success(XHRResponse, "json");
/pre

Thanks for the help Illya :)

After talking with Stripe, they indicated that you should NEVER invoke the charge service from the device. Only the cardToken service should be invoked from device, and all other (createCustomer, createCharge) functions should be done from the server side...


How to charge Stripe customer from server code.

Posted: Thu Jan 29, 2015 10:09 pm
by Illya Stepanov

Glad it works, Jon! Thanks for the update!


How to charge Stripe customer from server code.

Posted: Thu Oct 13, 2016 7:11 am
by EJLD

Hi Jon, thanks for your note, it's helpful to me.
One question regarding Stripe is about how to get the Customer's id.
I could get my card's token.
I could charge that card using the token.
but when it comes to server's js code to get the customer's id, I am a bit lost.
would you have any direction or even the appropriate code ?
thank you very much in advance,
Eric

PS: in case you guys at appery have the anwer, you are welcome too :)


How to charge Stripe customer from server code.

Posted: Thu Oct 13, 2016 7:28 am
by EJLD

Jon, Appery's guys,
I found the CURL on the Stripe site itself.
so, all fine.

in case somebody needs it

https://stripe.com/docs/api/curl#crea...

thks again for this thread
Eric