0

I want to be able to parse through the API response in a loop.

I have this in a controller method:

@payout_batch= PayPal::SDK::REST::Payout.get('xxxxxxx')
logger.info "Got Payout Batch Status[#{@payout_batch.batch_header.payout_batch_id}]"


rescue ResourceNotFound => err
  logger.error "Payout Batch not Found"
end

I can show results like this:

<%= @payout_batch.batch_header.amount.value %>

But I want to be able to loop through everything in an .each loop, if posible.. I have tried a few ways but nothing seems to be working:

<% @payout_batch.batch_header.each  do |x| %>
    <%= (x["amount"]) %>
<% end %>

And many more ways similar to this. Tried defining the response with:

 json = JSON.parse(@payout_batch)

and looping with json but that wouldn't seem to work either eithe.

Question: How can I result the response in the views with a loop?

uno
  • 1,421
  • 12
  • 38
  • Could you add an example for `@payout_batch`? – ttuan Apr 08 '19 at 00:44
  • seems `batch_header` is not enumerable – Pan Ke Apr 08 '19 at 02:06
  • @TuanTran yes, I'll add the response in the update in a second – uno Apr 08 '19 at 03:04
  • @TuanTran actually, thsi is best since its very long: https://developer.paypal.com/docs/api/payments.payouts-batch/v1/#payouts_get (scroll down a bit) – uno Apr 08 '19 at 03:04
  • @uno Do you want to print all keys, values in `batch_header` (`payout_batch_id`, `batch_status`, ...) or just print `amount` value? – ttuan Apr 08 '19 at 03:33
  • So, i am able to print, the batch_header information from the view but i have issues printing the "items.receiver", since i get the error: `undefined method receiver for #`... ... byt yeah, basicall yi want the be able to print all of the parameters – uno Apr 08 '19 at 03:36
  • Whats getting me is that all of the info i want response's within the CMD.. but in order to get receiver, I need to use ` @payout_item_details= PayoutItem.get(@payout_batch.items[0].payout_item_id)` but shouldn't i be able to have it appear in my views from the response? I've been stuck on this all day – uno Apr 08 '19 at 04:27
  • If you can show some more info about what exact info you want to extract, it would be helpful (and please do so by editing the question, not in comments). I'm not sure if `PayPal::SDK::REST::DataTypes::PayoutBatchHeader.members` helps, it returns a list of method names that you can call on the result of `.batch_header`. – max pleaner Apr 08 '19 at 06:05
  • I'm mostly aiming for "receiver" ... I am able to get the payout_item_id but nothing past that will append to the view with errors all saying they are nil, but i can see them in my cmd – uno Apr 08 '19 at 06:07
  • HOLY... actually hours later.... `<%= @payout_batch.items[0].payout_item.receiver %>` got it for me.... – uno Apr 08 '19 at 06:14

1 Answers1

0

PayPal and Stripe Payment Gateway's response always in the form of array. (For now, Don't know about other gateway as i have worked on these two.)

You can say that they have to maintain a common structure which handles both single record or multiple record that's why it returns in the form of array in most of the cases in every payment gateway.

So you have to do like this.

 <%= @payout_batch.items[0].batch_header %>
Pragya Sriharsh
  • 529
  • 3
  • 6