Import Methods in api folder problem

when i put
import Stripe from 'stripe';
in methods.js in the api folder
i have this error

Uncaught TypeError: require(...).createServer is not a function

but if i put il in the startup server index it works fine and it logs forme the stripe constructor

i tried to do like this this the methods file

 if (Meteor.isServer) {
      const Stripe = require('stripe');
      const stripe = Stripe("sk_test_pOF2Jt5********UDXjlF8v");
      console.log("Stripe", Stripe);
      console.log("stripe", stripe);
}

it works good but please i want the best practice

the same error and resolution for import Fiber from ‘fibers’; in the methods file

Thanks

Just a guess here, but if you are trying to use Stripe inside a method, then that method is going to assume that the code is executable on server and client. Inside your method, you can block out your server-side only code with Meteor.isServer or !this.isSimulation if you are doing something like validated-methods.

yes i did but the problem is in the file header
import Stripe from ‘stripe’;

I’m not sure about your specific problem, but most likely a server side import is happening at the client as vigorwebsolutions stated. It might be helpful if I share some resources to help you with your overall Stripe integration. I’ve also implemented Stripe integration and this approach has been working for me,

1- In a server folder under your import directory add a file called stripe.js

2- Within that file initiate Stripe and create low level methods, wrap them in a global server side object. Here is an example:

STRIPE = {
  customers: {
    create({ email, tokenId }) {
      // validate the method arguments
      check(email, String)

      const createCustomerSync = Meteor.wrapAsync(stripe.customers.create, stripe.customers);
      if (tokenId !== undefined) {
        check(tokenId, String);
        return createCustomerSync({ email,
          source: tokenId,
        });
      }
      // Use Meteor wrapAsync to convert async method to sync and bind the context,
      return createCustomerSync({ email });
    },
}

To initiate stripe, make sure you make use of the Meteor settings file

const stripe = connectToStripe(Meteor.settings.private.stripe);

You can have a file for development to hold your testing keys and another one for production.

3- Create a validated Meteor method and use that method to call the Server side Stripe object.

export const createCustomer = new ValidatedMethod({
  name: 'createCustomer',
  validate: new SimpleSchema({
    email: {
      type: String,
    },
    token: {
      type: Object,
      blackbox: true,
      optional: true,
    },
  }).validator(),
  run({ email, token }) {
     // Some validation here
    // then call server method 
if (Meteor.isServer) {
       // do some validation, check if there're duplicates etc..
      let stripeCust = {};
      const tokenId = (token !== undefined ? token.id : undefined);
      try { stripeCust = STRIPE.customers.create({ email, tokenId }); } catch (e) {
        throw RemoteErr('customers.createCustomer.stripe',
          'stripe retrieveByEmail exception', e);
      }
    //  update the DB and do more stuff
   
 }
    return customerDocId;
  },
});

Not sure if that’s the best practice but I hope it helps. I’d also suggest taking a look at the Meteor Chef tutorials, they’re amazing:

1 Like

the problem in my case i have a throw new Meteor.Error

if an if(Meteor.isServer)
i can’t throw the error from the server to the client the validated method all the time return undefined in the error callback

and in the terminal it thgrow the error and restart the meteor

W20170413-17:27:28.900(1)? (STDERR) /home/xeconcepts/.meteor/packages/meteor-tool/.1.4.2-1-beta.1.si3hb0++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
W20170413-17:27:28.900(1)? (STDERR) 						throw(ex);
W20170413-17:27:28.900(1)? (STDERR) 						^
W20170413-17:27:29.175(1)? (STDERR) Error: carte existe [204]
W20170413-17:27:29.175(1)? (STDERR)     at imports/api/users/methods.js:211:47

and the stripe methods or

const stripe = Stripe("sk_test_pO*******MUDXjlF8v");```
wont work only with ` if(Meteor.isServer)`