简体   繁体   English

未定义NodeJS Google Cloud功能请求

[英]NodeJS google cloud function request not defined

I'm trying to write some NodeJS for a google cloud function to process stripe. 我正在尝试为Google云函数编写一些NodeJS来处理条带化。 I'm pretty new to this and trying to follow various tutorials. 我对此很陌生,并尝试遵循各种教程。

I currently get the following error: 我目前收到以下错误:

 Detailed stack trace: ReferenceError: request is not defined

I don't really understand since request I thought is part of express package. 我不太了解,因为我认为这是快递包裹的一部分。 This is my entire code. 这是我的全部代码。 It's designed to take a token from the form and create a charge. 它旨在从表单中获取令牌并创建费用。 Even looking at the stripe tutorial they don't separately define request. 即使查看条纹教程,他们也不会单独定义请求。

var app = require('express')();
var http = require('http').Server(app);
var stripe = require('stripe')(
  "sk_);
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

//get token from body
const token = request.body.stripeToken;

//initiate a one-off charge for a customer
exports.chargeCustomer = app.get("/", function      chargeCustomer (req,res){
stripe.charges.create({
source: token,
currency: 'usd',
amount:999
 },function(err, charge) {
  if(err) {
  return res.send(JSON.stringify(err));
  }
  res.send(JSON.stringify(charge));
  });
  });

I'm not sure what tutorial you followed, so can't point to that. 我不确定您遵循的是哪个教程,因此无法指出。 What's missing from your code is the first request to the tokens endpoint that creates the card token, which is needed for the subsequent charges call. 您的代码中缺少的是对创建卡令牌的令牌端点的第一个请求,这是后续charges调用所必需的。

var stripe = require("stripe")("sk_test_XXXXXXX"); // your test key

stripe.tokens.create({
  card: {
    number: '4242424242424242',
    exp_month: 12,
    exp_year: 2020,
    cvc: '123'
  }
}, function(err, token) {
  // asynchronously called
});

The result of this will return a JSON object, and you will need the id of that object. 这样的结果将返回一个JSON对象,您将需要该对象的id The ID is a string starts with tok_ , ie tok_0EDyAkDOjEFp8g5k9NxTrm2k ID是以tok_开头的字符串,即tok_0EDyAkDOjEFp8g5k9NxTrm2k

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM