Jon Haider
Posts: 0
Joined: Thu Oct 16, 2014 2:53 pm

How to charge Stripe customer from server code.

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!

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

How to charge Stripe customer from server code.

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.

Jon Haider
Posts: 0
Joined: Thu Oct 16, 2014 2:53 pm

How to charge Stripe customer from server code.

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?

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

How to charge Stripe customer from server code.

Yes, this is possible.

Jon Haider
Posts: 0
Joined: Thu Oct 16, 2014 2:53 pm

How to charge Stripe customer from server code.

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...

Jon Haider
Posts: 0
Joined: Thu Oct 16, 2014 2:53 pm

How to charge Stripe customer from server code.

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.

Jon Haider
Posts: 0
Joined: Thu Oct 16, 2014 2:53 pm

How to charge Stripe customer from server code.

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...

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

How to charge Stripe customer from server code.

Glad it works, Jon! Thanks for the update!

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

How to charge Stripe customer from server code.

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 :)

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

How to charge Stripe customer from server code.

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

Return to “Issues”