条纹连接帐户可配置为在常规 payout schedule 中合并支出,例如在我们的案例中,用于每月支付。对于这些每月支付,我们需要向账户所有者解释我们平台上的哪些交易(在我们的案例中是预订和退款)产生了他们收到的总金额。我们将 strip 费用 ID(相应的退款 ID)存储在我们数据库中的预订(相应的退款)对象中。因此问题归结为:

给定一个 strip 帐户 ID,您如何获得对上次付款有贡献的 strip 收费和退款 ID 列表?

最佳答案

我与 Stripe 的支持团队进行了广泛的交流,有几个拼图是必要的:

付款受帐户限制

如果您使用 query stripe for a list of payouts ,您将只会收到您(平台所有者)从 Stripe 获得的支付对象。要获取特定帐户的支付对象,您可以使用平台的普通身份验证,但 send the stripe account id as a header 。因此,获得最后一笔付款的代码片段如下所示(我将使用 ruby​​ 片段作为其余答案的示例):

Stripe::Payout.list({limit: 1}, {stripe_account: 'acct_0000001234567890aBcDeFgH'})
=> #<Stripe::ListObject:0x0123456789ab> JSON: {
  "object": "list",
  "data": [
    {"id":"po_1000001234567890aBcDeFgH",
     "object":"payout",
     "amount":53102,
     "arrival_date":1504000000,
     "balance_transaction":"txn_2000001234567890aBcDeFgH",
     "created":1504000000,
     "currency":"eur",
     "description":"STRIPE TRANSFER",
     "destination":"ba_3000001234567890aBcDeFgH",
     "failure_balance_transaction":null,
     "failure_code":null,
     "failure_message":null,
     "livemode":true,"metadata":{},
     "method":"standard",
     "source_type":"card",
     "statement_descriptor":"[…]",
     "status":"paid",
     "type":"bank_account"
    }
  ],
  "has_more": true,
  "url": "/v1/payouts"
}

有了支付 ID,我们可以查询 balance transactions, scoped to a payout 的列表:
Stripe::BalanceTransaction.all({
    payout: 'po_1000001234567890aBcDeFgH',
    limit: 2,
}, {
    stripe_account: 'acct_0000001234567890aBcDeFgH'
})

与被视为平台所有者的对象相比,被视为帐户的对象被剥夺了大部分信息

即使您现在拥有付款 ID,该对象仍处于帐户范围内,您无法以平台所有者的身份检索它。但作为一个账户,支出只显示这样的伪收费和退款对象(注意第二笔交易有一个 py_7000001234567890aBcDeFgH 对象作为来源,而不是常规的 ch_ 收费对象):
Stripe::BalanceTransaction.all({
    payout: 'po_1000001234567890aBcDeFgH',
    limit: 2,
}, {
    stripe_account: 'acct_0000001234567890aBcDeFgH'
})
=> {
    :object => "list",
    :data => [
        {
            :id => "txn_4000001234567890aBcDeFgH",
            :object => "balance_transaction",
            :amount => -53102,
            :available_on => 1504000000,
            :created => 1504000000,
            :currency => "eur",
            :description => "STRIPE TRANSFER",
            :fee => 0,
            :fee_details => [],
            :net => -53102,
            :source => "po_5000001234567890aBcDeFgH",
            :status => "available",
            :type => "payout"
        },
        {
            :id => "txn_6000001234567890aBcDeFgH",
            :object => "balance_transaction",
            :amount => 513,
            :available_on => 1504000000,
            :created => 1504000000,
            :currency => "eur",
            :description => nil,
            :fee => 0,
            :fee_details => [],
            :net => 513,
            :source => "py_7000001234567890aBcDeFgH",
            :status => "available",
            :type => "payment"
        }
    ],
    :has_more => true,
    :url => "/v1/balance/history"
}

您可以让 stripe 自动扩展响应中的对象

As an additional parameter, you can give stripe paths of objects which you want stripe to expand in their response. 因此,我们可以通过传输从伪对象步行回到原始电荷对象:
Stripe::BalanceTransaction.all({
    payout: 'po_1000001234567890aBcDeFgH',
    limit: 2,
    expand:['data.source.source_transfer',]
}, {
    stripe_account: 'acct_0000001234567890aBcDeFgH'
}).data.second.source.source_transfer.source_transaction
=> "ch_8000001234567890aBcDeFgH"

如果你想处理整个列表,你需要消除 source.object 属性之间的歧义:
Stripe::BalanceTransaction.all({
    payout: 'po_1000001234567890aBcDeFgH',
    limit: 2,
    expand:['data.source.source_transfer',]
}, {
    stripe_account: 'acct_0000001234567890aBcDeFgH'
}).data.map do |bt|
  if bt.source.object == 'charge'
    ['charge', bt.source.source_transfer.source_transaction]
  else
    [bt.source.object]
  end
end
=> [["payout"], ["charge", "ch_8000001234567890aBcDeFgH"]]

退款没有返回原始 ID 的连接对象路径

不幸的是,目前无法从 BalanceTransaction 列表调用返回的伪 re_ 中获取原始 pyr_ 对象以进行退款交易。我发现的最佳替代方法是通过 data.source.charge.source_transfer.source_transaction 路径获取发放退款的费用的费用 ID,并将其与 createdpyr_ 属性结合使用以匹配我们的数据库退款对象。不过,我不确定这种方法到底有多稳定。提取该数据的代码:
Stripe::BalanceTransaction.all({
    payout: 'po_1000001234567890aBcDeFgH',
    limit: 100, # max page size, the code to iterate over all pages is TBD
    expand: [
        'data.source.source_transfer', # For charges
        'data.source.charge.source_transfer', # For refunds
    ]
}, {
    stripe_account: 'acct_0000001234567890aBcDeFgH'
}).data.map do |bt|
  res = case bt.source.object
    when 'charge'
      {
          charge_id: bt.source.source_transfer.source_transaction
      }
    when 'refund'
      {
          charge_id: bt.source.charge.source_transfer.source_transaction
      }
    else
      {}
  end
  res.merge(type: bt.source.object, amount: bt.amount, created: bt.created)
end

关于ruby - 如何获取自动付款的原始费用和退款 ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46296374/

10-12 01:01