Test the Billing API

Link to chapter - https://serverless-stack.com/chapters/test-the-billing-api.html

Hi, i get the following when i run serverless invoke on the billing function:

Serverless Warning --------------------------------------
 
  A valid file to satisfy the declaration 'file(resources/api-gateway-errors.yml)' could not be found.
 
Serverless: Bundling with Webpack...
Time: 787ms
Built at: 2/14/2019 10:29:11 PM
         Asset      Size   Chunks             Chunk Names
    billing.js  8.72 KiB  billing  [emitted]  billing
billing.js.map  6.12 KiB  billing  [emitted]  billing
Entrypoint billing = billing.js billing.js.map
[./billing.js] 2.17 KiB {billing} [built]
[./libs/billing-lib.js] 285 bytes {billing} [built]
[./libs/response-lib.js] 762 bytes {billing} [built]
[babel-runtime/core-js/json/stringify] external "babel-runtime/core-js/json/stringify" 42 bytes {billing} [built]
[babel-runtime/helpers/asyncToGenerator] external "babel-runtime/helpers/asyncToGenerator" 42 bytes {billing} [built]
[babel-runtime/regenerator] external "babel-runtime/regenerator" 42 bytes {billing} [built]
[source-map-support/register] external "source-map-support/register" 42 bytes {billing} [built]
[stripe] external "stripe" 42 bytes {billing} [built]

I dont know why it isnt returning success code 200 or any failure code after function execution. Do you have any idea what i could be doing wrong?

Thank you

Not sure what is going on here. To debug this add a SLS_DEBUG=* server less invoke… before your command.

I had the same issue. When I cloned the repository for the project from git, it did not include the resource folder with the project. Therefore, that file did not exist. You can go to the git repo and copy the file contents, and create the file manually.

Also, the serverless.yml file did not include this section when the project was cloned:

resources:

  • ${file(resources/api-gateway-errors.yml)}
1 Like

Yeah we missed that in one of our previous updates. It should be there now.

Watch out when you copy and paste when adding to billing-event, billing-lib and billing …trailing spaces will cause Errors… you may need to look at the formatting…

Are you copying this from the PDF? Cos the online version seems fine to me.

I ran into the trailing spaces error as well. It is in line 6-7 of the billing-lib.js code block. If you remove the extra space at the end of line 6 and all of blank line 7 it runs without error. I ran into it earlier with the response-lib.js file as well and had to remove blank lines 4 & 8 with the trailing space at the end of each prior line. It’s weird because I can see that there is the same empty line with a trailing space on lines 4-5 of dynamodb-lib.js but it’s not throwing an error. For what it’s worth, I used the copy button built into the code blocks on the website (plus you can see it just by looking at the code, unless Chrome is doing something funky with the formatting when rendering the page.) Other info: Windows 10 pro, VSC for editing/file creation. Or is this an issue with whole CR/LF discrepancy between Windows and MacOS?

HI, I’m getting a Serverless error “The file you provided does not exist.”

It seems to be having trouble finding the .env file where STRIPE_SECRET_KEY is stored per the tutorial. The .env file is there. I can see it when I run ls -a and it exists in the root directory of this project. I am using a relatively new Mac with Catalina installed. I’m wondering if the DOTENV plugin is having trouble accessing hidden files with Catalina. I have given terminal full disk access but it still says the file doesn’t exist.

I’m not sure how to proceed. I ran SLS_DEBUG=* serverless invoke local --function billing --path mocks/billing-event.json and got the following output:

Serverless Error ---------------------------------------

  ServerlessError: The file you provided does not exist.
      at AwsInvokeLocal.validateFile (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:49:13)
      at /usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:74:16
      at /usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:66:14
  From previous event:
      at AwsInvokeLocal.extendedValidate (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:61:28)
  From previous event:
      at Object.before:invoke:local:loadEnvVars [as hook] (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:32:12)
      at /usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:490:55
  From previous event:
      at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:490:22)
      at /usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:525:24
  From previous event:
      at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:525:8)
      at /usr/local/lib/node_modules/serverless/lib/Serverless.js:133:33
      at processImmediate (internal/timers.js:445:21)
      at process.topLevelDomainCallback (domain.js:135:15)
  From previous event:
      at Serverless.run (/usr/local/lib/node_modules/serverless/lib/Serverless.js:120:74)
      at /usr/local/lib/node_modules/serverless/bin/serverless.js:75:30
      at /usr/local/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:111:16
      at /Users/aaronpennington/Development/notes-app-api/node_modules/graceful-fs/graceful-fs.js:57:14
      at /usr/local/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:45:10
      at FSReqCallback.oncomplete (fs.js:153:23)
  From previous event:
      at /usr/local/lib/node_modules/serverless/bin/serverless.js:75:8
      at processImmediate (internal/timers.js:445:21)
      at process.topLevelDomainCallback (domain.js:135:15)
  From previous event:
      at Object.<anonymous> (/usr/local/lib/node_modules/serverless/bin/serverless.js:64:4)
      at Module._compile (internal/modules/cjs/loader.js:1139:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1159:10)
      at Module.load (internal/modules/cjs/loader.js:988:32)
      at Function.Module._load (internal/modules/cjs/loader.js:896:14)
      at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
      at internal/main/run_main_module.js:17:47
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              13.5.0
     Framework Version:         1.60.5
     Plugin Version:            3.2.7
     SDK Version:               2.2.1
     Components Core Version:   1.1.2
     Components CLI Version:    1.4.0

Can you point me to the exact snippet that is giving you issues?

Hmm that is interesting. Did you figure out this issue?

Here is a screenshot of what I am seeing in chrome for the code block of billing-lib in the “Add a billing API” chapter. I selected the end of the 6th line and into the next line so that you can see there is a space after the semi-colon as well as another one on the next line. image

Once the selected portion was removed it wasn’t an issue.

Here is the screenshot from response-lib code block in the “Add a Create Note API” chapter, which also threw the error–again the spaces are selected so that they are highlighted in grey at the end of lines 3-4 & 7-8.

image

strangely, I noticed that there were also trailing spaces in the dynamodb-lib.js file but it didn’t throw an error for me.

I did! I named my mock billing-events.json instead of billing-event.json.

1 Like

That’s super weird. Because we don’t have that in the source.

Btw, if you use the Copy button, it works fine right?

No, it still throws an error even if I use the Copy button (that’s how I’ve been copying all of the code throughout the tutorial.) I also tried it in firefox to see if it was chrome and while the trailing spaces don’t show when you select them on the screen like they do in chrome, they still are there when I copy and paste it into Visual Studio Code. Regardless of whether I manually select or use the copy button.

Are you on Windows? I tried it across browsers on macOS and it seems fine.

I am getting a Invalid token id: tok_visa error when running the command serverless invoke local --function billing --path mocks/billing-event.json. Is there a particular version of Stripe that i need to use?

{
    "statusCode": 500,
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    },
    "body": "{\"message\":\"Invalid token id: tok_visa\"}"
}

never mind i got it fixed. i was using the live stripe secret key in .env file.

1 Like

Tip for Indians here.

If you are from India trying out the Stripe API or from any other country that require a customer name and address of someone from outside India. Try the following in billing.js file:

await stripe.charges.create({
    source,
    amount,
    description,
    currency: "usd",
    shipping: {
      name: "Jenny Rosen",
      address: {
        line1: "510 Townsend St",
        postal_code: "98140",
        city: "San Francisco",
        state: "CA",
        country: "US",
      },
    },
  });

You can give any shipping details. But it should be something related to US/UK etc etc.

For more information check out: https://stripe.com/docs/india-exports

@jayair Please update the docs with this case. I spent considerable amount of time trying to debug the problem.

Hope it helps everyone.