#xaositect - Node

1 messages ยท Page 1 of 1 (latest)

crystal copper
#

Hello! That... is quite strange. ๐Ÿค”

long spade
#

Indeed. debugging to this point took me an hour or so, because it never occurred to me the problem would like in a function that's been working so painlessly for so long.

crystal copper
#

So stripe is the result of requiring the Stripe Node library and initializing it with your secret key, correct?

#
const stripe = require('stripe')('sk_test_...');
console.log(stripe);
#

Like that?

long spade
#
const stripe = require( 'stripe' )( process.env.STRIPE_SECRET_KEY, { apiVersion: process.env.STRIPE_API_VERSION } );
#

removing the second apiVersion arg produces the same error, sadly.

crystal copper
#

In node_modules/stripe does the package.json file there look like this at the top (but with the version you're using)?

{
  "name": "stripe",
  "version": "8.170.0",
  "description": "Stripe API wrapper",
  "keywords": [
    "stripe",
    "payment processing",
    "credit cards",
    "api"
  ],
  "homepage": "https://github.com/stripe/stripe-node",
long spade
#

hmmm, no:

  "_args": [
    [
      "stripe@7.14.0",
      "C:\\Users\\Xaositect\\Desktop\\Work\\rpc-backend-aws"
    ]
  ],
  "_from": "stripe@7.14.0",
  "_id": "stripe@7.14.0",
  "_inBundle": false,
  "_integrity": "sha512-2YSSuJ18jxue0xhW2gZIoz4asC8rITvsNxzh/KuYVKK3dELK6/4M8yoGfSjFcic0D1FtJBB0j6l8Ty26a2L08w==",
  "_location": "/stripe",
  "_phantomChildren": {
    "side-channel": "1.0.4"
  },
  "_requested": {
    "type": "version",
    "registry": true,
    "raw": "stripe@7.14.0",
    "name": "stripe",
    "escapedName": "stripe",
    "rawSpec": "7.14.0",
    "saveSpec": null,
    "fetchSpec": "7.14.0"
  },
#

mighty odd.

crystal copper
#

What the...

#

How did you install the Stripe library?

long spade
#

its a dependency in my package file as "stripe": "7.14.0",

installed with npm install

npm -v 6.13.4
node -v v12.16.3
windows system

crystal copper
#

Hm. If you go into node_modules/stripe/lib/resources what files are in there?

long spade
#

using... git bash for my terminal

#

well, I do see a Customers.js file in that folder, amongst others

crystal copper
#

That looks correct.

#

Let me try installing version 7.14.0 and see what I get...

long spade
#

Its particularly odd because we've been using this version for a year or more.

Our organization recently started using separate stripe environments for each dev. Is there perhaps something in my environment that is blocking the usage of portions of the library?

crystal copper
#

Oh, wow, I do get all that strange stuff in package.json when I install that version:

{
  "_args": [
    [
      "stripe@7.14.0",
      "/Users/-/stripe/Playground/Node"
    ]
  ],
  "_from": "stripe@7.14.0",
  "_id": "stripe@7.14.0",
  "_inBundle": false,
  "_integrity": "sha512-2YSSuJ18jxue0xhW2gZIoz4asC8rITvsNxzh/KuYVKK3dELK6/4M8yoGfSjFcic0D1FtJBB0j6l8Ty26a2L08w==",
  "_location": "/stripe",
  "_phantomChildren": {},
  "_requested": {
    "type": "version",
    "registry": true,
    "raw": "stripe@7.14.0",
    "name": "stripe",
    "escapedName": "stripe",
    "rawSpec": "7.14.0",
    "saveSpec": null,
    "fetchSpec": "7.14.0"
  },
  "_requiredBy": [
    "/"
  ],
#

My code still works normally though.

#

console.log(stripe.customers.retrieve); spits out [Function (anonymous)] as expected as well.

#

And stripe.customers.retrieve specifically works as expected.

#

It must be something in your environment or with npm.

long spade
#

yup, when I try to log retrieve, I get:

    TypeError: Cannot read property 'retrieve' of undefined```
#

Frustrating

crystal copper
#

In node_modules/stripe/lib/stripe.js does line 3 require everything in the ./resources folder?

#
const resources = require('./resources');
long spade
#

yup:


const resources = require('./resources');
crystal copper
#

And in the resources folder does Customers.js look like the following?

'use strict';

const StripeResource = require('../StripeResource');
const stripeMethod = StripeResource.method;

module.exports = StripeResource.extend({
  path: 'customers',

  includeBasic: ['create', 'del', 'list', 'retrieve', 'update'],

  deleteDiscount: stripeMethod({
    method: 'DELETE',
    path: '/{customer}/discount',
  }),

  updateSource: stripeMethod({
    method: 'POST',
    path: '/{customer}/sources/{id}',
  }),

  deleteSource: stripeMethod({
    method: 'DELETE',
    path: '/{customer}/sources/{id}',
  }),

  verifySource: stripeMethod({
    method: 'POST',
    path: '/{customer}/sources/{sourceId}/verify',
  }),

  createBalanceTransaction: stripeMethod({
    method: 'POST',
    path: '/{customer}/balance_transactions',
  }),

  listBalanceTransactions: stripeMethod({
    method: 'GET',
    path: '/{customer}/balance_transactions',
    methodType: 'list',
  }),

  retrieveBalanceTransaction: stripeMethod({
    method: 'GET',
    path: '/{customer}/balance_transactions/{transaction}',
  }),

  updateBalanceTransaction: stripeMethod({
    method: 'POST',
    path: '/{customer}/balance_transactions/{transaction}',
  }),

  createSource: stripeMethod({
    method: 'POST',
    path: '/{customer}/sources',
  }),

  listSources: stripeMethod({
    method: 'GET',
    path: '/{customer}/sources',
    methodType: 'list',
  }),

  retrieveSource: stripeMethod({
    method: 'GET',
    path: '/{customer}/sources/{id}',
  }),

  createTaxId: stripeMethod({
    method: 'POST',
    path: '/{customer}/tax_ids',
  }),

  deleteTaxId: stripeMethod({
    method: 'DELETE',
    path: '/{customer}/tax_ids/{id}',
  }),

  listTaxIds: stripeMethod({
    method: 'GET',
    path: '/{customer}/tax_ids',
    methodType: 'list',
  }),

  retrieveTaxId: stripeMethod({
    method: 'GET',
    path: '/{customer}/tax_ids/{id}',
  }),
});
long spade
#

Yes, there is no diff.

crystal copper
#

How strange.

#

Back in the lib folder does line 19 in resources.js look like this?

Customers: require('./resources/Customers'),
long spade
#

๐Ÿคทโ€โ™‚๏ธ alas, it does...

#

however...

#

when I hover over the import of stripe at the top of the file where it is imported, vscode gives me this helpful hint...

module "C:/Users/Xaositect/Desktop/Work/rpc-backend-aws/node_modules/stripe/lib/stripe"
Could not find a declaration file for module 'stripe'. 'C:/Users/Xaositect/Desktop/Work/rpc-backend-aws/node_modules/stripe/lib/stripe.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/stripe` if it exists or add a new declaration (.d.ts) file containing `declare module 'stripe';
crystal copper
#

๐Ÿคจ

#

Give that command a try and see if it helps.

long spade
#

doesn't solve the problem, alas.

crystal copper
#

Try this: make a test.js file in the same place as your node_modules directory (alongside it, not in that folder) with this content, fill in your test key, and run just that file and see if it works as expected:

const stripe = require("stripe")("sk_test_...");

stripe.balance.retrieve(function(err, balance) {
    console.log(balance);
});
#

If that does work add this to it, fill in one of your Customer IDs, and run it again:

stripe.customers.retrieve('cus_...', function(err, customer) {
    console.log(customer);
});
long spade
#

yes, that first one seems to return a balance:

{
  object: 'balance',
  available: [ { amount: 0, currency: 'usd', source_types: [Object] } ],
  connect_reserved: [ { amount: 0, currency: 'usd' } ],
  livemode: false,
  pending: [ { amount: 0, currency: 'usd', source_types: [Object] } ]
}
#

let me find a customer real fast

crystal copper
#

Okay, add the Customers code and see if it works. If it does the library itself is fine and it's something else in your project/code is causing the issue. Maybe it's a TypeScript thing?

long spade
#

yeah, the customers code works just dandy...

#

Uff. I hope someone hasn't been merging things indiscriminately over xmas break.

crystal copper
#

Maybe someone removed the definitions you were using or something? I'm not very familiar with TypeScript, so if that's the issue I'm a bit at sea. ๐Ÿ˜…

long spade
#

Sadly, I've read the typescript docs, but I'm not much further advanced there yet. We have typescript on the roadmap ๐Ÿ˜“ but not yet. And it doesn't look like it has been added.

Well I'll debug and see if I can get to the bottom of it.

Thanks for your help.

crystal copper
#

Sorry I can't help further. The only other thing I can recommend is slowing adding your other dependencies/requires to the test.js file and see if you can find the one that causes the Stripe code to stop working.

long spade
#

I'll report back if I discover the missing piece.

crystal copper
#

I hope you find it! I need to step away, but @slender yew should be lurking about if you need anything else.

long spade
#

AAAAAIGH! Someone implemented a mock, elsewhere:

const stripe = jest.fn().mockImplementation( () => {
    return {
        setApiVersion: () => {},
        invoices: {
            list: async () => {
                throw new StripeInvalidRequestError( {
                    message: 'No upcoming invoices for customer',
                } );
            },
        },
        charges: {
            retrieve: async ( chargeId, params ) => {
                return {
                    amount: parseFloat( ( chargeId / 100 ).toFixed( 2 ) ), // just for ease of mocking
                    transfer_data: {
                        amount: parseFloat( ( chargeId / 100 ).toFixed( 2 ) ), // just for ease of mocking
                    },
                };
            },
        },
    };
} );

problem solved.

slender yew
#

oh ...

#

oh yeah that would be it

#

wow nice find!

warped lantern
#

"AAAAAIGH!" couldn't help but hear this in Maleficent's voice