配置和测试条带帐户创建条带自定义

时间:2017-11-01 00:51:28

标签: node.js stripe-payments stripe-connect

我正在尝试为Stripe自定义配置stripe.accounts.create({})。我的目标是以一种形式创建所有内容,以便用户满足条带帐户的所有信息要求,以便在表单竞争后进行交易。使用Stripe推荐的信用卡号测试当前代码时,我收到以下代码块后显示的错误。我想知道是否存在我遗漏的令牌化过程,并未在条带创建帐户文档中引用。这是我目前的帖子方法

var knex = require("../models/knex"),
    express = require('express'),
    middleware = require("../middleware/index"),
    stripe = require("stripe")("sk_test_VALUEOFMYTESTKEY"),
    router = express.Router({mergeParams:true});
    router.post("/formuser",function(req,res){
    console.log(req.user[0].user_id);
    knex("users.user").select("*").where("user_id",req.user[0].user_id)
    .then((user) => {
      var today = new Date(Date.now()).toLocaleString();
      var accountType = String(req.body.accountType).toLowerCase();
      var checkIfCard = accountType=="card";
      console.log(req.body.accountType,checkIfCard,String(req.body.cardNumber));
      var ip = req.headers['x-forwarded-for'] || 
               req.connection.remoteAddress || 
               req.socket.remoteAddress ||
               req.connection.socket.remoteAddress; 

      console.log(ip);
      if(!checkIfCard){
          stripe.accounts.create({
        email: user.email,
        country: "US",
        type: "custom",
        //Required fields for Custom via... https://stripe.com/docs/connect/required-verification-information
        metadata: {
        "external_account": {
          "object": "bank_account", 
          "exp_month": req.body.cardExpirationMonth,
          "exp_year":  req.body.cardExpirationYear,// : null,
          "number":  req.body.bankNumber,// : null,

        },                        //external account info... https://stripe.com/docs/api#account_create_bank_account
        "city": req.body.city,
        "legal_entity.adress.line1": req.body.streetAddress,
        "legal_entity.address.postal_code": req.body.zipCode,
        "legal_entity.address.state": req.body.state,
        "legal_entity.dob.day": req.body.birthDay,
        "legal_entity.dob.month": req.body.birthMonth,
        "legal_entity.dob.year": req.body.birthYear,
        "legal_entity.first_name": req.body.firstName,
        "legal_entity.last_name": req.body.lastName,
        "legal_entity.ssn_last_4": req.body.ssn_last_4,
        "tos_acceptance.date": today,
        "tos_acceptance.ip": ip,
        }

      }).then((acct) => {
        res.redirect("/");
      })
    .catch((e) => {
        console.log(e);
    });
      } else {
          stripe.accounts.create({
        email: user.email,
        country: "US",
        type: "custom",
        //Required fields for Custom via... https://stripe.com/docs/connect/required-verification-information
        metadata: {
        "external_account": {
          "object": "card", //bank account or cc or dc...
          "card": req.body.cardNumber.toString(),
          "cvc" : req.body.cvc.toString(),
          "currency" : "usd",// : null

        },                        //external account info... https://stripe.com/docs/api#account_create_bank_account
        "city": req.body.city,
        "legal_entity.adress.line1": req.body.streetAddress,
        "legal_entity.address.postal_code": req.body.zipCode,
        "legal_entity.address.state": req.body.state,
        "legal_entity.dob.day": req.body.birthDay,
        "legal_entity.dob.month": req.body.birthMonth,
        "legal_entity.dob.year": req.body.birthYear,
        "legal_entity.first_name": req.body.firstName,
        "legal_entity.last_name": req.body.lastName,
        "legal_entity.ssn_last_4": req.body.ssn_last_4,
        "tos_acceptance.date": today,
        "tos_acceptance.ip": ip,
        }

      }).then((acct) => {
        res.redirect("/");
      })
    .catch((e) => {
        console.log(e);
    });
      }});
});

当我输入Stripe建议测试的信用卡信息时,我收到以下错误

   { [Error: Invalid val: {"object"=>"card", "card"=>"4242 4242 4242 4242", "cvc"=>"111", "currency"=>"usd"} must be a string under 500 characters]
  type: 'StripeInvalidRequestError',
  stack: 'Error: Invalid val: {"object"=>"card", "card"=>"4242 4242 4242 4242", "cvc"=>"111", "currency"=>"usd"} must be a string under 500 character

当我期望创建用户时。

编辑:我删除了这篇文章中的一些knex数据库代码,以缩短它的长度,因为它与当前错误无关。目前的错误特别来自Stripe的承诺。

1 个答案:

答案 0 :(得分:0)

您的代码正在尝试传递external_account中的银行帐户详细信息,但同时也会传递卡数据。这不太可能是你想要的。

除此之外,您不应该在服务器端传递此信息,因为它很敏感。相反,您应该创建一个令牌客户端。对于卡片数据,您可以使用Elements,对于银行帐户数据,您可以使用Stripe.js构建自己的表单并进行标记。完成此操作后,您会获得一个卡片令牌tok_123或一个银行帐户令牌btok_123,然后可以在external_account参数中使用此服务器端。

然后,您还应该将数据作为嵌套哈希值传递。这意味着您不会传递"legal_entity.adress.line1"而是传递legal_entity[address][line1]。您的代码应该看起来像这样:

stripe.accounts.create( 
{
  type: 'custom',
  country: 'US',
  legal_entity : {
    first_name : 'john',
    last_name : 'doe',
    type : 'individual',
    address: {
      line1: 'line1',
      city: 'city',
      state: 'state',
      postal_code: '90210',
      country: 'US'
    } 
  },
  external_account: 'tok_visa_debit',
}).then((acct) => {
  console.log('account: ', JSON.stringify(acct));
}).catch((e) => {
  console.log(e);
});