#adaman_best-practices
1 messages ¡ Page 1 of 1 (latest)
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
âąď¸ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
đ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1217475119686090824
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
I don't think there's a way to set up automated testing with Checkout.
Is that what you're trying to do?
Yeah it is, I am currently doing a rewrite and was originally trying to use Selenium to enter the simulated payment info which was obviously not ideal. On further reading the docs I found the PaymentMethod example I linked
Ah, we recommend against automated testing like Selenium with Checkout. It often runs afoul of our anti fraud and anti bot protections which can make those tests brittle. There are usually easier, more resiliant ways to test what you are actually trying to test.
Is there a specific payment scenario with one of those test cards that you are trying to simulate?
Yeah that was definitely not an ideal solution lol
Realistically the 2 scenario's I'd want to test are payment success and payment fail
Gotcha, and what does your integration listen for after the payment that you are looking to test?
For webhooks we typically reccommend mocking out the event. Basically, you save a real event that we sent you and then resend that event when it would be sent after the payment attempt.
@method_decorator(csrf_exempt, name="dispatch")
class StripeWebhookView(View):
"""
Stripe webhook view to handle checkout session completed event.
"""
def post(self, request, format=None):
payload = request.body
endpoint_secret = settings.STRIPE_WEBHOOK_SECRET
sig_header = request.META["HTTP_STRIPE_SIGNATURE"]
event = None
try:
event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
except ValueError as e:
# Invalid payload
return HttpResponse(status=400)
except stripe.error.SignatureVerificationError as e:
# Invalid signature
return HttpResponse(status=400)
if event["type"] == "checkout.session.completed":
session = event['data']['object']
payment_id = session['metadata']['payment_id']
process_completed_payment(payment_id)
# Can handle other events here.
return HttpResponse(status=200)
I have this webhook setup listening for a checkout.session.completed
I also have the stripe success url set to a view that will also trigger the process_completed_payment function.
I'm defining the payment ID in metadata when generating the Checkout Session if that helps.
Yeah, so basically we would recommend saving the body of the checkout.session.completed event when you receive it and then play it back when you want to test out your logic here. Similarly for testing the redirect, you can make another request to your server with the same return URL and which can in theory trigger your server to do the same Checkout Session lookup/fulfillment\ logic
Ok I'll take a look at that. Thanks.
As an alternative, could I use the PaymentIntent example from the docs as a standin for the CheckoutSession and then process that as if it was a checkout session?
That could definitely be sufficient for your purposes. So maybe you could confirm a PaymentIntent and then have your tests pretend it was assosicated with some Checkout Session
Yeah that's what I was thinking. Probably a lot simpler than trying to emulate a checkout.session.completed event