I am learning the Corporate store tutorials on Appmaker. I am trying to customize the template by integrating a stripe payment method to it. I used stripe's checkout code template. I put this on a function and attached a onclick trigger to run this. I am running into a problem when I try to complete the charge process as shown here in step 2 of the official Stripe-Go checkout guide. Can someone explain the code template to complete the payment charge
Asked
Active
Viewed 288 times
-2
-
Can you please add more details about your problem? Do get some exception or just struggling to put all pieces together? – Pavel Shkleinik Feb 21 '18 at 21:49
-
I am struggling to put all pieces together. I figured out that all the code implementation should be in javascript and not in GO for: So I found the Javascript version of stripe's code template here. – Dennis Feb 22 '18 at 22:07
-
I used on this link (https://stripe.com/docs/checkout#integration-custom) template to open a check-out form and I used code template on this link(https://stripe.com/docs/stripe-js/elements/payment-request-button#create-payment-request-instance) to create the charge. after submitting the checkout form it seems the tokens are generated on my stripe account but the charges are not completed. – Dennis Feb 22 '18 at 22:14
-
`You can access the token ID with 'token.id'. Get the token ID to your server-side code for use.` Did you follow this instruction from the code sample? It seems that you are close to be done with your client side code, but you are missing your server side part. – Pavel Shkleinik Feb 23 '18 at 18:54
-
This where I need help, what's the code that I have to use to extract the token ID to my server. Can you show an example? – Dennis Feb 24 '18 at 19:12
-
Hey, I've posted answer below. Hope it helps! – Pavel Shkleinik Mar 06 '18 at 00:33
1 Answers
1
Here you go:
Client scripts
var AMOUNT = 51;
// Callback to handle token ready event
function onTokenReady(token) {
// Call server script
google.script.run
.withFailureHandler(function() {
// TODO: handle error
})
.withSuccessHanlder(function() {
// TODO: handle success
})
.charge(token.id, AMOUNT);
}
// Initializes Stripe client-side API
// To make it work add https://checkout.stripe.com/checkout.js
// as external JavaScript resource (see screenshot above)
function onAppStart() {
window.handler = StripeCheckout.configure({
key: 'pk_test_6pRNASCoBOKtIshFeQd4XMUh',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: onTokenReady
});
}
// Handles payment button's click event
function onButtonClick() {
handler.open({
name: 'Stripe.com',
description: '2 widgets',
zipCode: true,
amount: AMOUNT
});
}
Server scripts
function charge(tokenId, amount) {
var params = {
payload: {
amount: amount,
currency: 'usd',
description: 'Example charge',
source: tokenId
},
headers: {
// TODO: replace with real PLATFORM_SECRET_KEY
Authorization: 'Bearer sk_test_BQokikJOvBiI2HlWgH4olfQ2'
}
// uncomment to troubleshoot
// ,muteHttpExceptions: false
};
var response = UrlFetchApp.fetch('https://api.stripe.com/v1/charges', params);
console.log(response.getContentText());
}

Pavel Shkleinik
- 6,298
- 2
- 24
- 36
-
Hi Pavel, Thank you very much in helping me out put together the stripe payment for app-maker. The code works perfectly to pass the tokens. However its still failing to create charges.However the final request is still successful. Request failed for https://api.stripe.com/v1/charges returned code 400. Truncated server response: { "error": { "type": "invalid_request_error", "message": "No such token: tok_1C2rrZJUGYqFbcpsc9XjYpIl", "param": "source" } – Dennis Mar 07 '18 at 02:41
-
When we pass the "params" through UrlFetch.fetch to the stripe server, does it create charges? Again sorry if question is too fundamental. Sorry about my typos on the previous comments – Dennis Mar 07 '18 at 02:46
-
Don't worry, I figured out what the problem was. I made a mistake on my end...a typo again!.. Thank very very much again – Dennis Mar 07 '18 at 02:54
-
You are welcome! Don't hesitate to accept the question it can help developers like you in feature. – Pavel Shkleinik Mar 08 '18 at 00:08
-
Thanks again Pavel. There is one more minor issue that came up on this line of the code- google.script.run .withFailureHanlder(function() { // TODO: handle error }) .withSuccessHanlder(function() { // TODO: handle success }) .charge(token.id, AMOUNT); }. I was getting an error".withFailureHandler is not a function". I tried to nullify the the success and failure handlers by reading appscript docuemnt but it kept repeating the same error So in the end I removed both the handlers and just called ".charge(token.id, AMOUNT);" and it worked. – Dennis Mar 09 '18 at 00:46
-
I tried several ways to properly implement the .withFailureHandler and .with SuccessHandler functions but I was unsuccessful till now. Is it ok to remove the two handlers and just run the function that calls serverside script? – Dennis Mar 09 '18 at 00:48
-
It was a typo in the code snippets I've shared (Hanlder -> Handler) and I've just fixed it. Sorry about that. Btw, those handlers are optional and you can easily omit them, but generally it is a good practice to handle errors (and in this particular case I think user would like to know whether the transaction was successful or not). – Pavel Shkleinik Mar 09 '18 at 16:41
-