如何获得自动付款的原始费用和退款金额

时间:2017-09-19 09:12:09

标签: ruby stripe-payments stripe-connect

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

鉴于条带帐户ID,您如何获得导致上次付款的条带费用和退款ID列表?

2 个答案:

答案 0 :(得分:2)

我与Stripe的支持团队进行了广泛的交流,并且需要几个拼图来实现目标:

付款的范围是帐户

如果您query stripe for a list of payouts,您将只收到平台所有者从条带获得的付款对象。要获取特定帐户的支付对象,您可以使用平台的常规身份验证,但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"
}

您可以让条带自动展开响应中的对象

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,并将其与created pyr_属性结合使用{1}}匹配我们的数据库退款对象。不过,我不确定这种方法到底有多稳定。提取该数据的代码:

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

答案 1 :(得分:1)

现在可以通过“转移冲销”对象获取退款ID:

Stripe::BalanceTransaction.list({
  payout: 'po_1000001234567890aBcDeFgH',
  expand: [
    'data.source.source_transfer', # For charges
    'data.source.transfer_reversal', # For refunds
  ]
}, {
  stripe_account: 'acct_0000001234567890aBcDeFgH'
}).auto_paging_each do |balance_transaction|
  case balance_transaction.type
    when 'payment'
      charge_id = balance_transaction.source.source_transfer.source_transaction
    when 'payment_refund'
      refund_id = balance_transaction.source.charge.source_transfer.source_transaction
    end
  end
end