#some1ataplace-checkout-discounts
1 messages · Page 1 of 1 (latest)
Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- some1ataplace, 21 hours ago, 21 messages
You can pass discounts in to the discount parameter when creating the session https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-discounts
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Though unfortunately we don't have a non-checkout way of validating the coupon codes
So you would need to validate that the input was a valid code yourself
oh okay i will try it
some1ataplace-checkout-discounts
I am confused which I would use though. Can you explain simply when I would use coupons versus promo codes?
A coupon just describes how much of a discount the user gets, promotion codes define usage limits (max redemptions, expiration date, etc.)
And of course promotion codes offer a way for customers to input a string that represents the discount
Our coupon objects don't have that by themselves
So it seems the promo code seems like the way to go with checkout then/
Yep, it is probably the more popular one to use in this scenario
Like this?
checkout_session = stripe.checkout.Session.create(
success_url="https://yourwebsite.com/success",
cancel_url="https://yourwebsite.com/canceled",
payment_method_types=["card"],
mode="payment",
line_items=[{
'price': 'price_id',
'quantity': 1,
}],
discounts=[
{"coupon": '<COUPON_ID>'},
{"promotion_code": '<PROMOTION_CODE_ID>'}
]
)
If so, how would I then validate the codes?
stripe.Coupon.retrieve("Z4OV52SU")
stripe.PromotionCode.retrieve(
"promo_1O2hXKIeTJrsS1reRfsqT2rT",
)
Is that it, or am I missing something?
I think it is expecting the Stripe ID for the coupon (co_1234)
You'd probably want to cache a list of these codes -> ID mappings on your end and pass the ID to the API when creating the session
hmm
how about this?
import stripe
stripe.api_key = 'YOUR_SECRET_KEY'
def get_promotion_id(promo_code):
active_promotions = stripe.PromotionCode.list(active=True, limit=100)
for promotion in active_promotions.auto_paging_iter():
if promotion['code'] == promo_code:
return promotion['id']
return None
def get_coupon(coupon_code):
try:
return stripe.Coupon.retrieve(coupon_code)
except stripe.error.InvalidRequestError:
return None
def validate_codes(coupon_code=None, promo_code=None):
if coupon_code:
coupon = get_coupon(coupon_code)
if not (coupon and coupon['valid']):
return False
if promo_code:
promo_id = get_promotion_id(promo_code)
if promo_id is None: # The promotion code is not found or not active
return False
return True
That looks fine (though I'd suggest testing it thoroughly to make sure)
That being said, it will likely add a couple second delay to your validation if you always make these networked calls to Stripe to validate
Yea I know that's the only thing
Seems like if I did coupon code it'll be faster?
I would normally use the built in method of adding promo codes to checkout but I have other payment streams besides stripe
I think the retrieve and list calls would be roughly as fast as each other, so they'd both result in a slower validation
Not sure I understand the other payment streams angle. Can you tell me more about how that is complicating this validation?
i am using btcpayserver and that does not have the ability to accept a promo code like stripe does which is why i am using a form to pass the promo code into it so it will be effected with both stripe and btcpayserver
You could still cache promo codes, you would just need to also have to store info on which platform the promo code came from
True. Is there a way I could pass in the value from the form into the promo code textbox into the checkout session?
I think if you pass the promo code ID checkout will populate the actual promo code ("BUY15" as opposed to promo_1234)
ooh good maybe I will do that just with stripe then
that way stripe will do the validation, not me, right?
I think there may have been a miscommunication here. I meant that if you pass in a valid promo ID (promo_1234) in the Checkout session create call, Stripe will display the human readable code on the hosted checkout page
If you put in an incorrect ID, the API request will error out
Yeah, Stripe validates promo codes when a user is on our checkout page and puts the promo code in, but if you are validating on your site you will need custom logic for it unfortunately
Yep, that would put an empty promo code input on the Checkout page that a user can type their code in to
So I cannot use
allow_promotion_codes
=True to input the value from my form into stripe's form?
Correct
Ugh that would be a really nice feature to have
Yep, definitely see the value in that
Will you pass it on to your team? Would love to have it added
Will do!
Awesome thank you