Issue
I have a question about Stripe payments... I figured out how to do some code when payment was succesful, but what and where I can check that user even didn't enter anything in checkout form, and just pressed Back cancel_url
?
my checkout code:
domain = 'http://127.0.0.1:8000/profiles/'+username+'/balance/'
stripe.api_key = settings.STRIPE_SECRET_KEY
try:
# Create new Checkout Session for the order
# Other optional params include:
# [billing_address_collection] - to display billing address details on the page
# [customer] - if you have an existing Stripe Customer ID
# [payment_intent_data] - capture the payment later
# [customer_email] - prefill the email input in the form
# For full details see https://stripe.com/docs/api/checkout/sessions/create
# ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param
checkout_session = stripe.checkout.Session.create(
client_reference_id=request.user.username if request.user.is_authenticated else None,
success_url=domain + 'success?session_id={CHECKOUT_SESSION_ID}',
cancel_url=domain + 'cancelled',
payment_method_types=['card'],
mode='payment',
line_items=[
{
'price_data': {
'currency': 'eur',
'unit_amount': grata_points_amount, #6900,
'product_data': {
'name': pack.name, #'69 GP',
# 'description': 'package with 6900 Grata Points',
# 'images': [''],
},
},
'quantity': pack_cnt, #1,
}
],
metadata={
'transactionID': transaction.id
}
)
return JsonResponse({'sessionId': checkout_session['id']})
except Exception as e:
return JsonResponse({'error': str(e)})
sucess payment (all needed fields was filled up):
def stripe_webhook(request):
stripe.api_key = settings.STRIPE_SECRET_KEY
endpoint_secret = settings.STRIPE_ENDPOINT_SECRET
payload = request.body
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)
# Handle the checkout.session.completed event
if event['type'] == 'checkout.session.completed':
# TODO: run some custom code here
session = event['data']['object']
#getting information of order from session
userObject = User.objects.get(username=session['client_reference_id'])
sessionID = session["id"]
#inform user about successfull payment
add_notification(userObject, 'bought', 'balance')
#generate redirect link when payment registered
domain = 'http://127.0.0.1:8000/profiles/' + session['client_reference_id'] + '/balance/'
return HttpResponseRedirect(domain)
print("Payment was successful.")
return HttpResponse(status=200)
I'm about button which is on screenshot
So any ideas?
Solution
When users click on the back arrow on the Checkout page, they are redirected to the cancel_url
you defined. When than happens the Checkout Session is still valid and Stripe won't send you a webhook event. But note that later (usually after 24 hours) the Checkout Session will expire and you will receive a checkout.session.expired
event.
If you want to know when a user clicks on the back arrow, the only option is to write some custom code on your cancel_url
page directly.
Answered By - soma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.