How to retrieve a Stripe Promo code by code

Viewed 1258

It looks like in Stripe you can retrieve a promo code by id:

Stripe::PromotionCode.retrieve(
  'promo_1Hd0sBG03p6y1vChab7Jh6Zs',
)

However, I can not see a way to retrieve this by the actual customer facing code ex FIFTYOFF. Is there any way to do this?

It doesn't seem to make sense this is not possible, since this is all the data the user would have. I would need to either keep a local database duplicate of each Promo code and correlate with the customer facing code, or lookup all of my codes and iterate over them to find the actual promo code id.

1 Answers

The easiest solution is to use the List Promotion Codes API and pass the code parameter. This will return a list of Promotion Codes with that code and since it has to be unique, the list will only contain one element which is what you are after:

promotion_codes = Stripe::PromotionCode.list({
  code: 'FIFTYOFF',
})
promotion_code_id = promotion_codes.data[0].id
Related