【问题标题】:Using Stripe Connect with stripe-node to charge shared customer使用 Stripe Connect 和 stripe-node 向共享客户收费
【发布时间】:2015-03-13 09:27:20
【问题描述】:

我正在努力寻找正确的语法,以便通过 Stripe Connect 为连接用户的利益向客户收费。我正在使用条带节点 api。我已经尝试了数百种参数组合来创建费用,但没有一个对我有用。

我希望收费的客户已作为客户添加到我的(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]

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

我多次阅读上面显示的链接https://github.com/stripe/stripe-node/wiki/Using-Stripe-Connect-with-node.js,但它并没有帮助我符合语法。所以我暂时没有想法。感谢阅读。

【问题讨论】:

    标签: node.js stripe-payments


    【解决方案1】:

    一旦您使用他们的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`
      }
    );
    

    【讨论】:

    • 是的,做到了!非常感谢。我没有检查有关收费的文件,因为我没有收取任何费用。请注意,您的答案中的 card: $token 应该是 card: token.id (可能想为未来的读者编辑它),但关键是我缺少的费用中的 ACCESS_TOKEN 参数。
    • 哦,太好了,完全错过了这里的 $token 感谢您指出
    • 谁能解释一下 ACCESS_TOKEN 是什么,以及如何获得它?
    • @adamwong246 您在使用独立帐户或托管帐户的连接流程中获得它。
    • 是的,但是怎么做?它是什么?到目前为止,我唯一可以获得的访问令牌是令牌化的 CC 信息……在上面,那是 token,对吧?那么ACCESS_TOKEN 是什么?
    【解决方案2】:

    我已经解决了这个问题。要使用 Stripe Connect 创建常规的 paymentIntent,Node 中的语法是:

    const connectedAccountStripeId = 'acct_something';
    const paymentIntent = await stripe.paymentIntents.create({
        payment_method_types: ['card'],
        amount: price,
        statement_descriptor: 'Statement for bank record',
        currency: 'usd',
      }, {
        stripe_account: connectedAccountStripeId,
      })
    return paymentIntent;
    

    注意:我使用了stripe_account 而不是stripeAccount,这是文档here 中的内容。在进行此更改之前,我收到了以下错误。

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

    【讨论】:

      【解决方案3】:

      我做了以下代码可能会有所帮助....

       var stripe = require("stripe")("<STRIPE SECRET KEY>");
       // var stripe = require("stripe")("sk_test_**********");
       stripe.charges.create({
          amount: <AMOUNT>,
          currency: "<CURRENCY>", // EUR
          source: "<CARD ID>", // card_1233442FFHDJSDFJM
          customer:"<CUSTOMER ID>",// cus_E06YG6h0DFDFSDF
          description: "<DESCRIPTION>" // CHARGE FOR ORDER #8487
       },function(err, charge) {
          if(err)
            console.log('ERROR : '+err);
          else
            console.log('charge : '+JSON.stringify(charge,null,2));
       });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-31
        • 2020-04-02
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 2018-06-26
        • 2017-06-17
        相关资源
        最近更新 更多