[SOLVED] Sending Stripe's Response Back To Page Using NPM

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.

Can anyone help?
please

API Stripe API reference – Create a subscription – curl
This is Stripe’s Github for Node GitHub - stripe/stripe-node: Node.js library for the Stripe API.

Been 4 days and I’m falling into depression because of this :sweat: :sweat: :sweat:

P.S. The subscription is being created, thats not the issue, its only that I cannot send the response back to the page.

The Support Docs shows that you just need to return the response like below but I have tried it and it doesn’t work.

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, use wix-location.to() to redirect to a thank you page after the Charge ID is received

hey @givemeawhisky

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
  }
);
}

So I tried using the response that the backend receives to retrieve the subscription using fetch and then returning it to the page:

import Stripe from 'stripe';
import {fetch} from 'wix-fetch';

var key = require("stripe")("sk_test_XXXXX");

export function subscribe(cus, itms) {
 
return key.subscriptions.create({
    customer: cus,
    items: itms
  }, function(err, subscription) {
      if (err) {
        return err;
      }          
        let responseSid = subscription.id;
        getResponse(responseSid)
        .then( (response) => {
         console.log(response); //////////////working
         return response; ////////////////////not working
        });
      }
  );
}

export async function getResponse(responseSid) {
 
 const apiKey = "sk_test_XXXX"; 

 const response = await fetch("https://api.stripe.com/v1/subscriptions/" + responseSid, {
    method: 'post',
    headers: {
 "Content-Type": "application/x-www-form-urlencoded",
 "Authorization": "Bearer " + apiKey
    }
  });
 
 if (response.status >= 200 && response.status < 300) {
     const stripe = await response.json();
     return stripe;
  }
     return await response.text();
}

My Page code:

function payNow() {
  subscribe(cus, itms)
  .then((response) => {
    console.log(response);
  });
}

The fetch succeeds in retrieving the subscription and I can console.log it from the backend but again nothing can be sent to the frontend.

Anyone?

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.

By the way, in your code on this line:

 }, function(err, subscription) {

Should it not be like this instead:

},
function(err, subscription) {

Hey @givemeawhisky

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.

I really hope someone from Wix notices this.

keeping this thread alive

Seems that you might have an async issue.
Try adding ‘await’ before calling the create() function.

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

Hey @ohad-laufer

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

Hey @tomer-wix

I worked on this and now I am able to send the response back to the page using a .then which turns the function(err, charge) into a promise

import Stripe from 'stripe';

var key = require("stripe")("XXXXXXXXXXXXX");
export function payNow(price, currency, token) {
 
 return key.charges.create({
    amount: price,
    currency: currency,
    source: token
  })
  .then( function(error, charge) {
    if(error) {
       return error; //does not send a response
    }
       return charge; //works
  });
}

The problem is that the response is only sent back when the charge succeeds but not when there is an error.

Do you think you can point me in the right direction.