首页 文章

使用Stripe Connect with stripe-node为共享客户收费

提问于
浏览
4

我正在努力找到正确的语法,以便为使用Stripe Connect的连接用户带来好处 . 我正在使用stripe-node api . 我已经尝试了似乎是一百种params的组合来创造电荷,没有一个对我有用 .

我希望收取的客户已经作为客户添加到我的(Connect应用程序的)Stripe帐户中,而我想要收取费用的用户已经连接到我的应用程序 . 那很好 . 完成这些步骤后,我会检索新费用的令牌,这似乎也没问题 . 我打电话:

Stripe.tokens.create(
  { customer: myCustomer },
  connectedUserAccessToken,
  function(error, token){
    if (error){
      return error;
    } else {
      return token;
    }
  }
);

哪个回报:

{ id: 'tok_15L16gLFEmuXszazTaVUA0ty',
livemode: false,
created: 1421280206,
used: false,
object: 'token',
type: 'card',
card:
  { id: 'card_15L16gLFEmuXszaz7VAu2ciH',
  object: 'card',
  last4: '8210',
  brand: 'MasterCard',
  funding: 'debit',
  exp_month: 1,
  exp_year: 2020,
  fingerprint: 'ny39g9uj2lfhYA2H',
  country: 'US',
  name: null,
  address_line1: null,
  address_line2: null,
  address_city: 'New York',
  address_state: 'NY',
  address_zip: '10003',
  address_country: 'US',
  cvc_check: null,
  address_line1_check: null,
  address_zip_check: null,
  dynamic_last4: null,
  customer: null },
  client_ip: '99.99.9.99'
}

(卡数据在Stripe测试页面中是假的) .

当我尝试使用新令牌来创建充电时会出现问题 . 我将上面的对象分配给var令牌并调用:

Stripe.charges.create(
  { card: token.id },
  function(error, result){
    if (error){
      return error;
    } else {
      return result;
    }
  }
);

返回:

[ Error: There is no token with ID tok_15L16gLFEmuXszazTaVUA0ty. ]

这个响应对我没有意义,因为正如您所看到的,令牌ID与刚刚创建的id匹配 . (我看到令牌创建事件也发布到我的Stripe日志中,所以它们正在被正确创建) .

如果不是将 { card: token.id } 传递给 Stripe.charges.create() 而是通过:

Stripe.charges.create(
  token,
  function(error, result){}
);

我收到:

[Error: [Error: Missing required param: currency]

这是有道理的,因为我没有通过它的收费货币或金额 . 但是当我试图将这些额外的参数作为选项时,例如:

Stripe.charges.create(
  { currency: user.currency, amount: transaction.amount },
  token,
  function(error, result){}
);

我收到:

[Error: Stripe: Unknown arguments ([object Object]). Did you mean to pass an options object? See https://github.com/stripe/stripe-node/wiki/Passing-Options.]

如果我反转参数并调用,我会收到相同的错误:

Stripe.charges.create(
  token,
  { currency: user.currency, amount: transaction.amount },
  function(error, result){}
);

事实上,我以任何格式传递的任何选项似乎都会被拒绝,并且上面会出现同样的错误 .

我试过了:

Stripe.charges.create(
  { customer: token.id, currency: user.currency, amount: transaction.amount },
  function(error. result){}
);

这导致:

[Error: No such customer: tok_15L16gLFEmuXszazTaVUA0ty]

我尝试通过 token.currency = user.currency 直接将货币添加到令牌本身,然后调用 Stripe.charges.create(token) . 它似乎首先喜欢它返回错误:

[Error: Missing required param: amount]

但当我跟进并设置 token.currency = user.currency; token.amount = transaction.amount; 然后它拒绝整个对象,返回:

[Error: Received unknown parameters: id, livemode, created, used, object, type, client_ip]

我想不出它可能在寻找什么 .

我读了很多次上面给我看的链接,但是它没有帮助我遵守语法 . 所以我暂时没想到了 . 谢谢阅读 .

1 回答

  • 2

    使用 publishable_key 为您的关联用户创建卡片令牌后,您需要使用 access_token 代表他们创建费用 . 有关如何collect fees的文档中描述了这一点,代码如下所示:

    // Get the credit card details submitted by the form
    var token = request.body.stripeToken;
    
    // Create the charge on Stripe's servers - this will charge the user's card
    stripe.charges.create(
      {
        amount: 1000, // amount in cents
        currency: "eur",
        card: token,
        description: "payinguser@example.com",
        application_fee: 123 // amount in cents
      },
      ACCESS_TOKEN, // user's access token from the Stripe Connect flow
      function(err, charge) {
        // check for `err`
        // do something with `charge`
      }
    );
    

相关问题