Expanding a Source object in a Stripe API call to StripeBalenceService doesn't return any customer info

1.5k views Asked by At

I'm making a c# call to the Stripe.net API to fetch a balance history for a connected account. I'm trying to expand on the balance transaction object to see where the charge is coming from (ex. the customer who made the charge) as all the charges to connected accounts on my platform are from charge objects with a destination property to the connected account.

Here is my code and a screenshot of what the expanded source looks like, but think I should see a charge id or a customer or something refering me to the initial customer somewhere, but I don't...

var balanceService = new StripeBalanceService();
balanceService.ExpandSource = true;
var list = new List <string> () {
  "data.source.source_transfer"
};
StripeList <StripeBalanceTransaction> balanceTransactions 
  = balanceService.List(
  new StripeBalanceTransactionListOptions() {
      Limit = 20,
      Type = "payment",
      Expand = list
  },
  new StripeRequestOptions() {
    StripeConnectAccountId = accountId
  }
);

foreach(var transaction in balanceTransactions) {
  var test = transaction;
}

enter image description here

I feel like I should see a charge id (ex. ch_xxx) or a Customer value (which is null) all I see of any relevance is a payment id (ex. py_xxx)

1

There are 1 answers

8
karllekko On

It is possible to get the charge object(ch_xxx), it is just a little involved!

As you are using destination charges, the charge(ch_xxx) takes place on the platform account, and then a transfer(tr_xxx) is made to the connected account. That transfer creates a payment(py_xxx) on the connected account, which results in a balance transaction(txn_xxx).

As your code expands the source of those balance transactions, you get the payment(py_xxx). The payment is equivalent to a charge, so it has a source_transfer field. You can expand this field also! This will give you the transfer object(tr_xxx). Finally, the transfer has a source_transaction field, and this can be exapanded to give the original charge(ch_xxx)!

Putting that all together, you will want to expand on "data.source.source_transfer.source_transaction".

If you use a Stripe library in a dynamic language you can see this in action ... unfortunately, stripe-dotnet has an open issue right now which means that you can not do this directly. Instead, you will need to make the API calls manually by calling the various Retrieve functions on the IDs, instead of doing a single expansion. It would look something like this:

var paymentId = transaction.Source.Id;
var chargeService = new StripeChargeService();
var payment = chargeService.Get(
    paymentId,
    new StripeRequestOptions()
    {
        StripeConnectAccountId = accountId
    }
);
var transferService = new StripeTransferService();
transferService.ExpandSourceTransaction = true;
var transfer = transferService.Get(payment.SourceTransferId);
var charge = transfer.SourceTransaction;
Console.WriteLine(charge.Id);