Retrieving Stripe Charge through metadata

Viewed 3710

I'm trying to get a Charge using Stripe API.

Everything is good but I don't understand why this request is not working:

Stripe::Charge.all(metadata: { project_id: an_id })

I got error:

Stripe::InvalidRequestError: Received unknown parameter: metadata

Here's what a response look like:

{"id":"ch_1BQIXjGrTKCCbjGtKVJOKzbg","object":"charge","amount":1000,"amount_refunded":0,"application":null,"application_fee":"fee_1BQIXlEsrND8ZkheXtsfgQWj","balance_transaction":"txn_1BQIXlGrTKCCbjGtpSDEu34m","captured":true,"created":1511196887,"currency":"eur","customer":"cus_BnuMP3mm0iEFL8","description":"Don","destination":"acct_1BEzwEEsrND8Zkhe","dispute":null,"failure_code":null,"failure_message":null,"fraud_details":{},"invoice":null,"livemode":true,"**metadata":**{"donation_amount":"10","donation_fees":"0","donation_id":"21110","donation_referer_id":"417","donation_reward_id":"2120",**"project_id":"501"**,"project_name":"Arcadia - Le Concert Classique des youtubers","user_city":"Segré","user_country":"FR","user_email":"simon-chretien@hotmail.fr","user_first_name":"Simon","user_id":"23038","user_last_name":"Chrétien"},"on_behalf_of":"acct_1BEzwEEsrND8Zkhe","order":null,"outcome":{"network_status":"approved_by_network","reason":null,"risk_level":"normal","seller_message":"Payment complete.","type":"authorized"},"paid":true,"receipt_email":"simon-chretien@hotmail.fr","receipt_number":"1097-0153","refunded":false,"refunds":{"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/charges/ch_1BQIXjGrTKCCbjGtKVJOKzbg/refunds"},"review":null,"shipping":null,"source":{"id":"card_1BQIXcGrTKCCbjGtKktLMM42","object":"card","address_city":null,"address_country":null,"address_line1":null,"address_line1_check":null,"address_line2":null,"address_state":null,"address_zip":null,"address_zip_check":null,"brand":"Visa","country":"FR","customer":"cus_BnuMP3mm0iEFL8","cvc_check":"pass","dynamic_last4":null,"exp_month":10,"exp_year":2020,"fingerprint":"aMPkqWKgq4dQUnWy","funding":"credit","last4":"8073","metadata":{},"name":"simon-chretien@hotmail.fr","tokenization_method":null},"source_transfer":null,"statement_descriptor":null,"status":"succeeded","transfer":"tr_1BQIXlGrTKCCbjGtjFk4oC0D","transfer_group":"group_ch_1BQIXjGrTKCCbjGtKVJOKzbg"},

As you can see there's a metadata => project_id in response

What am I doing wrong ?

1 Answers

As one of the comments has said you can use retrieve if you are looking to return one charge. You would have to pass the charge id to the method:

Stripe::Charge.retrieve('ch_1BQIXjGrTKCCbjGtKVJOKzbg')

The documentation that you are looking for would be under List all charges. all is actually an alias for list. Based on the documentation to accomplish what you are looking at doing, you could return all the Stripe Charges and then search for the project_id within metadata.

Depending on how many charges you have this seems like it would be overkill returning all of your charges. Instead consider only return the charges based on the customer

Only return charges for the customer specified by this customer ID.

Stripe::Charge.list(customer: "cus_BnuMP3mm0iEFL8")
# or
Stripe::Charge.all(customer: "cus_BnuMP3mm0iEFL8")
Related