#CEO of Sadness - Stripe Export

1 messages · Page 1 of 1 (latest)

earnest shore
#

So I'm guessing this is a Node.JS server situation, right?

white phoenix
#

next.js

#

I created stripe.ts file in pages/api and I want to use it only in my API routes

earnest shore
#

We do advise a more global import of Stripe.JS. It might seem like overkill but there are some fraud-detection processes that are useful to have running.

white phoenix
#

so u think that it will be better to just import stripe in every API route where I want to use stripe and then just call:

    apiVersion: "2020-08-27"
  });```
earnest shore
#

Actually I was thinking about what I do with Vue.js. Stripe is injected into the root context that the app shares.

white phoenix
earnest shore
#

Hmmm I'm not familiar with integrating the NPM module. However it would appear that exporting the Stripe object from a dedicated .ts file is not supported.

white phoenix
#

ok, ty for help 😄

earnest shore
#

Honestly I'm surprised the NPM package maintainers don't have that approach documented.

#

Except the one thing that jumps out to me is that the import appears off in your snippet:

import Stripe from 'stripe';
const stripe = new Stripe('sk_test_...', {
  apiVersion: '2020-08-27',
});
#

Capitalized

#

Not sure if that helps

sharp crescent
#

(NOT with Stripe) I use exactly this in my Firebase Cloud Functions (single deploy of multiple functions):

import Stripe from "stripe";

import { configs } from "../Functions";

// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const secret_key = configs?.stripe.stripe_test_key
  ? configs.stripe.stripe_test_key
  : process.env.STRIPE_TEST_KEY;

export const stripe = new Stripe(secret_key);
stripe.setMaxNetworkRetries(2);
#

this is in an app.js file in a Stripe directory which is in a generalized Services Directory - imports are from the Services path:
For example:

import {
  chargeExpandables,
  listCharges,
  listTransfers,
  monthStamp,
  monthStampNow,
  refundCharge,
  transferToAccount
} from "../../Stripe";
#

imported/initialized from top-level:

import "./services/Stripe";
sharp crescent
#

(NOT with Stripe) - do note the import of "Stripe", and the export of "stripe"

crude quartz
#

@white phoenix just want to make sure you're not using the server-side stripe-node library here with your client app and secret key

#

This shoudl not be done

#

Are you building a client or server part of your application currently?

white phoenix
#

server

#

but i figured out that there is problem with my building enviroment

#

because i'm having same issue even if I am importing stripe from "stripe" in every API route and creating new stripe instance by:

  apiVersion: '2020-08-27',
});```
crude quartz
#

That's a pattern for initializing Stripe.js though

#

What is Stripe here?

white phoenix
#

import Stripe from "stripe"

crude quartz
#

OK, and what happens?

#

Are you in an environment that support ES modules?

white phoenix
#

i'm getting error
Can't resolve 'child_process' in '/node_modules/stripe/lib'in my console

#

yes ES modules are supported.

#

I fixed it by changing webpack config a bit:
config.resolve.fallback.child_process = false;

#

and with this there are no errors in console