I am working on a Flask application where I need to retrieve a value set in one route inside another route. I am trying to set a value in the session inside the payment_callback function and retrieve it in the payment_result function. However, the value is None when I try to access it in payment_result.
Here is my code:
@app.route('/payment/callback', methods=['POST'])
def payment_callback():
global processed_transactions
try:
data = get_request_data()
logger.info(f"Received data: {data}")
response_status = decode_if_bytes(data.get('payment_result', {}).get('response_status'))
if response_status:
session['response_status'] = response_status
logger.info(f"Callback session data: {dict(session)}")
else:
logger.warning("Response status not found in the received data.")
return redirect(url_for('payment_result'))
except Exception as e:
logger.error(f"Error querying transaction: {e}")
flash('Error querying transaction', 'danger')
return redirect(url_for('index'))
@app.route('/payment/return', methods=['GET', 'POST'])
def payment_return():
time.sleep(3)
logger.info('------return--------')
return redirect(url_for('payment_result'))
@app.route('/payment/result')
def payment_result():
print('I want to get response status')
response_status = session.get('response_status')
print(response_status) # Output is NONE----------------- here is the problem
return render_template('Payment_Result.html')
Log output:
INFO:main:Received data: {'tran_ref': 'TST2417201883637', 'merchant_id': 78035, 'profile_id': 137790, 'cart_id': 'card_123', 'cart_description': 'Sample Payment', 'cart_currency': 'EGP', 'cart_amount': '10.00', 'tran_currency': 'EGP', 'tran_total': '10.00', 'tran_type': 'Sale', 'tran_class': 'ECom', 'customer_details': {'name': 'shady Henawy wardy', 'email': '[email protected]', 'street1': 'Cairo', 'city': 'Cairo', 'state': 'GZ', 'country': 'EG', 'zip': '11637', 'ip': '197.49.115.176'}, 'payment_result': {'response_status': 'A', 'response_code': 'G01271', 'response_message': 'Authorised', 'acquirer_ref': 'TRAN0001.66745A17.0003D57B', 'cvv_result': ' ', 'avs_result': ' ', 'transaction_time': '2024-06-20T16:34:31Z'}, 'payment_info': {'payment_method': 'Visa', 'card_type': 'Credit', 'card_scheme': 'Visa', 'payment_description': '4000 00## #### 0002', 'expiryMonth': 6, 'expiryYear': 2029}, 'ipn_trace': 'IPNS0001.66745A17.00005B19'}
INFO:main:Callback session data: {'response_status': 'A'}
INFO:werkzeug:127.0.0.1 - - [20/Jun/2024 19:34:38] "POST /payment/callback HTTP/1.1" 302 -
INFO:main:------return--------
INFO:werkzeug:127.0.0.1 - - [20/Jun/2024 19:34:41] "GET /payment/return HTTP/1.1" 302 -
INFO:werkzeug:127.0.0.1 - - [20/Jun/2024 19:34:41] "GET /payment/result HTTP/1.1" 200 -
I want to get response status
None
Expected Behavior: The session[‘response_status’] set in payment_callback should be accessible in payment_result.
Actual Behavior: The value of session[‘response_status’] is None in payment_result.
Additional Information: I have verified that the session is being set correctly in payment_callback, as shown in the logs.
What am I doing wrong, and how can I fix this issue?