import Stripe from 'stripe';
const key = require("stripe")("sk_test_XXXXXXXXX");
export function subscribe(cus, items) {
return key.subscriptions.create({
customer: cus,
items: items
}, function(err, subscription) {
if(err) {
console.log(err.message); //this is working
return err; //not working :(
}
console.log(subscription.id); //this is working
return subscription; //not working :(
}
);
}
I cannot figure out how to send the JSON response from stripe back to the page when using NPM to create subscriptions through stripe. Its working flawlessly with Fetch.
You might have already seen this, however Yisrael has done a page all about this:
Ignore all the posts in it from James!
Scroll down near the end and you will also spot this question too:
How can i make it that if the payment is okay it will take the customer to a thank you page? In the PayNow() function, usewix-location.to()to redirect to a thank you page after the Charge ID is received
Actually that’s related to fetch and I’m already able to return the reponse using fetch however when using NPM I can console.log the data but I’m having troubles returning it to the page.
export function subscribe(cus, items) {
key.subscriptions.create({
customer: cus,
items: items
}, function(err, subscription) {
if(err) {
console.log(err.message); //this is good
return err; //not good
}
console.log(subscription.id); //this is good
return subscription; //not good
}
);
}
Yes my apologies to you Shan, I was too quick in posting my original reply and didn’t read your post fully about sending the json response, I read your post and instantly thought of Yisrael’s post as I had seen it myself a few days previously and posted it up thinking it would help.
Anyways, I do very much hope that a Wix Mod like Yisrael or Sam gets involved with this forum post and you get the help that you need to fix your issue.
I have been pulling my hair out for the past 6 days now. In the docs its clearly shown that putting a return at the start of the function will return the response to the page. I have tried the same method with other apis and its working. I don’t know what the hell is wrong with Stripe’s NPM.
Hi, I believe the problem is the code is using “node-style” callbacks. You can’t return values from them. You can find solutions online to convert it to a promise, for example: javascript - How do I convert an existing callback API to promises? - Stack Overflow (look at the top answer, the solution after the title “3. Node style callback (“nodeback”)”)
Thank you for your help but I had already tried using async/await. Actually it was one of the first things I tried but the response received by the page remains undefined