whatbox
whatbox

Reputation: 33

Is it possible to limit Stripe Charges by metadata?

I have a paginated charge list like this:

stripelist = Stripe::Charge.list({limit: 100})

stripelist.each do |payment|
    if payment["metadata"]["ss_registration"]
        striperecords << payment if pendingrecs.include? payment.metadata.portal_payment_id
    end
end

while stripelist.has_more do 
    stripelist = Stripe::Charge.list({limit: 100, starting_after: stripelist.data.last.id})
    stripelist.each do |payment|
        if payment["metadata"]["ss_registration"]
            striperecords << payment if pendingrecs.include? payment.metadata.portal_payment_id
        end
    end
end

The problem is that I have to pull all charges and then filter the results in Ruby, which costs processing time. Is there a way to filter by metadata?... to do something like this at the start:

stripelist = Stripe::Charge.list({limit: 100}, metadata: '{ss_registration}')

Upvotes: 1

Views: 264

Answers (2)

whatbox
whatbox

Reputation: 33

Strive Development will consider adding metadata as charge criteria. Here's their response to my request:

Thank you for reaching out to us, and give us your feedback to improve our API charges, we know it is important for the businesses.

We're always working to make Stripe better for our users, so it's valuable to hear your input. We understand you want our development team to code for limiting meta data in addition to the transfer group and our development team will consider it.

If you have any questions, feel free to contact us!

Upvotes: 0

ttmarek
ttmarek

Reputation: 3250

Unfortunately you can't list Charges by their metadata. Right now the only parameters that you can list Charges by are listed here: https://stripe.com/docs/api/charges/list?lang=ruby

But, your code could be simplified a bit by using Stripe's auto-pagination functionality; described here: https://stripe.com/docs/api/pagination/auto?lang=ruby.

With auto-pagination, your code could boil down to something like:

charges = Stripe::Charge.list({ limit: 100 })

charges.auto_paging_each do |charge|
  if charge["metadata"]["ss_registration"]
    striperecords << charge if pendingrecs.include? charge.metadata.portal_payment_id
  end
end

Upvotes: 1

Related Questions