stripe.net example for getting charges from the transfer object?

1,526 views
Skip to first unread message

Jamin Benjamin

unread,
Jun 20, 2014, 2:36:44 AM6/20/14
to api-d...@lists.stripe.com
Can someone post an example in C# on how to get the charges from a transfer object?  (or advise me if I'm doing this wrong)

The goal here is to take action on charges that relate to a particular transfer.


So here's what I'm doing:

1. receive transfer.created event
2. get the transfer ID from the event
3. call stripe API to get the transfer by transferID
4. ...

at this point, I am not seeing the transactions inside the StripeTransfer object.  I had thought the dot notation would lead down through the json like it displays on Stripes docs but it seems like I'm only getting the top level.  I believe that my shoddy json skills are to blame and I was hoping someone could offer a quick tap in the right direction.  My code thus far is pretty standard but I'll post it below anyway...

the transfer object json can be found here: https://stripe.com/docs/api#retrieve_transfer

My event handler:
var json = new StreamReader(context.Request.InputStream).ReadToEnd();

            // var mydata = JsonConvert.DeserializeObject<StripeTransfer>(json);
            var stripeEvent = StripeEventUtility.ParseEvent(json);

            StripeTransfer stripeTransfer;
            switch (stripeEvent.Type)
            {
                case "transfer.created":
                    stripeTransfer = Stripe.Mapper<StripeTransfer>.MapFromJson(stripeEvent.Data.Object.ToString());
                    var transferId = stripeTransfer.Id;
                    stripeTransfer = _stripeManager.GetTransfer(transferId);
                    
                    _stripeManager.ProcessTransfer(stripeTransfer);
                    break;



Jamin Benjamin

unread,
Jun 20, 2014, 3:04:32 PM6/20/14
to api-d...@lists.stripe.com
Ok, with the help of a neat little ruby gem called Ultrahook...made local testing of the webhooks a bit easier.  

Since the event is incoming data, I just want to nab the transferId and all the chargeIds, then use them to make my own data calls to get back data that I know is from Stripe.  Here is a snippet from my event handler:

var json = new StreamReader(context.Request.InputStream).ReadToEnd();



           
var stripeEvent = StripeEventUtility.ParseEvent(json);



           
StripeTransfer stripeTransfer;
           
switch (stripeEvent.Type)
           
{
               
case "transfer.created":
                    stripeTransfer
= Stripe.Mapper<StripeTransfer>.MapFromJson(stripeEvent.Data.Object.ToString());

                   
string transferId = stripeTransfer.Id;
                   
// do some stuff

                   
                   
for (int i = 0; i < Int32.Parse(stripeEvent.Data.Object["transactions"]["total_count"].ToString()); i++)
                   
{
                       
string chargeId = stripeEvent.Data.Object["transactions"]["data"][i]["id"];
                       
// send ID over to business tier for processing

                   
}
                   
break;


Jamin Benjamin

unread,
Jun 27, 2014, 3:09:33 PM6/27/14
to api-d...@lists.stripe.com
Ok, so the above example does not work...well, it does but using total_count errors out with out of range index.  For some reason there are only 5 charge objects in my transfer data even though the data's total_count says 17.  I suspect it may be intentional on Stripe's side and that I probably need to explicitly request all charges.

 
Finding assistance with stripe.net has been pretty much impossible but for the record it is a beautiful API.  Jayme Davis is a ninja rock star.  I'll be working on this today so once had I'll be sure and post the solution.

Thanks in advance, Self, for your responses.

Jamin Benjamin

unread,
Jun 28, 2014, 8:33:26 PM6/28/14
to api-d...@lists.stripe.com
SOLUTION: Stripe.net, getting all charges in a transfer


Ok, turns out you can get the list of charges (not limited to 5) by hitting the transfers/{id}/transactions end point.  But after talking to Stripe support, I learned that there is a proper way to do this and my way wasn't really the right way.  The end point to get all charges in a transfer is this:


Now that I know that, of course, I see in the documentation (https://stripe.com/docs/api#balance_history) the parameters are shown right there.  So now that I'm on the right track using balance/history, Stripe.net has my back (and always did) 

Here is the code, tested and works:

public IEnumerable<StripeBalanceTransaction> GetChargesByTransferId(string transferId, StripeBalanceTransactionType type)
        {
            try
            {
                var balanceService = new StripeBalanceService();
                var options = new StripeBalanceTransactionListOptions
                {
                    TransferId = transferId,
                    Type = type.ToString()
                };

                IEnumerable<StripeBalanceTransaction> balanceTransactions = balanceService.List(options);

                return balanceTransactions;
            }
            catch (StripeException sex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw;
            }

I wanna thank Jonathan and Bernie at Stripe for pointing me in the right direction.  Stripe's support has been fairly quick, friendly, respectful, and all on a Saturday afternoon during a world cup match...thanks fellas





Jamin Benjamin

unread,
Jun 28, 2014, 8:36:16 PM6/28/14
to api-d...@lists.stripe.com
to clarify, StripeBalanceTransactionType is not part of Stripe.net, that's a enum from my own application (made up of the types listed in Stripe's API docs)


--
You received this message because you are subscribed to the Google Groups "Stripe API Discussion" group.
To post to this group, send email to api-d...@lists.stripe.com.
Visit this group at http://groups.google.com/a/lists.stripe.com/group/api-discuss/.

To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Reply all
Reply to author
Forward
0 new messages