From 9cf9839b1090e50b8654f39e3f7b8e367bd907e1 Mon Sep 17 00:00:00 2001 From: Mangesh-Khairnar Date: Wed, 22 Apr 2020 11:40:08 +0530 Subject: [PATCH] fix: add finalize request to complete the transaction --- .../doctype/paytm_settings/paytm_settings.py | 33 ++++++++------- payments/templates/includes/paytm_checkout.js | 42 ------------------- payments/templates/pages/paytm_checkout.html | 4 +- payments/templates/pages/paytm_checkout.py | 3 -- 4 files changed, 22 insertions(+), 60 deletions(-) delete mode 100644 payments/templates/includes/paytm_checkout.js diff --git a/payments/payment_gateways/doctype/paytm_settings/paytm_settings.py b/payments/payment_gateways/doctype/paytm_settings/paytm_settings.py index 39a9f42..394cd9c 100644 --- a/payments/payment_gateways/doctype/paytm_settings/paytm_settings.py +++ b/payments/payment_gateways/doctype/paytm_settings/paytm_settings.py @@ -71,8 +71,8 @@ def get_paytm_params(payment_details, order_id, paytm_config): # initialize a dictionary paytm_params = dict() - # redirect_uri = get_request_site_address(True) + "/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.get_transaction_status" - redirect_uri = "http://cf9b2bb1.ngrok.io/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.get_transaction_status" + redirect_uri = get_request_site_address(True) + "/api/method/frappe.integrations.doctype.paytm_settings.paytm_settings.verify_transaction" + paytm_params.update({ "MID" : paytm_config.merchant_id, @@ -99,24 +99,28 @@ def verify_transaction(**kwargs): '''Verify checksum for received data in the callback and then verify the transaction''' paytm_config = get_paytm_config() received_data = frappe._dict(kwargs) + is_valid_checksum = False paytm_params = {} for key, value in received_data.items(): if key == 'CHECKSUMHASH': paytm_checksum = value + elif key == 'cmd': + continue else: paytm_params[key] = value - # Verify checksum - is_valid_checksum = verify_checksum(paytm_params, paytm_config.merchant_key, paytm_checksum) + if paytm_params and paytm_config and paytm_checksum: + # Verify checksum + is_valid_checksum = verify_checksum(paytm_params, paytm_config.merchant_key, paytm_checksum) if is_valid_checksum and received_data['RESPCODE'] == '01': verify_transaction_status(paytm_config, received_data['ORDERID']) else: frappe.respond_as_web_page("Payment Failed", - "Transaction failed to complete. Don't worry, in case of failure amount will get refunded to your account.", + "Transaction failed to complete. In case of any deductions, deducted amount will get refunded to your account.", http_status_code=401, indicator_color='red') - frappe.log_error("Order unsuccessful, received data:"+received_data, 'Paytm Payment Failed') + frappe.log_error("Order unsuccessful. Failed Response:"+cstr(received_data), 'Paytm Payment Failed') def verify_transaction_status(paytm_config, order_id): '''Verify transaction completion after checksum has been verified''' @@ -136,15 +140,16 @@ def verify_transaction_status(paytm_config, order_id): def finalize_request(order_id, transaction_response): request = frappe.get_doc('Integration Request', order_id) - redirect_to = request.data.get('redirect_to') or None - redirect_message = request.data.get('redirect_message') or None + transaction_data = frappe._dict(json.loads(request.data)) + redirect_to = transaction_data.get('redirect_to') or None + redirect_message = transaction_data.get('redirect_message') or None if transaction_response['STATUS'] == "TXN_SUCCESS": - if request.data.reference_doctype and request.data.reference_docname: + if transaction_data.reference_doctype and transaction_data.reference_docname: custom_redirect_to = None try: - custom_redirect_to = frappe.get_doc(request.data.reference_doctype, - request.data.reference_docname).run_method("on_payment_authorized", 'Completed') + custom_redirect_to = frappe.get_doc(transaction_data.reference_doctype, + transaction_data.reference_docname).run_method("on_payment_authorized", 'Completed') request.db_set('status', 'Completed') except Exception: request.db_set('status', 'Failed') @@ -153,10 +158,10 @@ def finalize_request(order_id, transaction_response): if custom_redirect_to: redirect_to = custom_redirect_to - redirect_url = 'payment-success' + redirect_url = '/integrations/payment-success' else: request.db_set('status', 'Failed') - redirect_url = 'payment-failed' + redirect_url = '/integrations/payment-failed' if redirect_to: redirect_url += '?' + urlencode({'redirect_to': redirect_to}) @@ -164,7 +169,7 @@ def finalize_request(order_id, transaction_response): redirect_url += '&' + urlencode({'redirect_message': redirect_message}) frappe.local.response['type'] = 'redirect' - frappe.local.response['location'] = 'redirect_url' + frappe.local.response['location'] = redirect_url def get_gateway_controller(doctype, docname): reference_doc = frappe.get_doc(doctype, docname) diff --git a/payments/templates/includes/paytm_checkout.js b/payments/templates/includes/paytm_checkout.js deleted file mode 100644 index 4cf070b..0000000 --- a/payments/templates/includes/paytm_checkout.js +++ /dev/null @@ -1,42 +0,0 @@ -function onScriptLoad() { - console.log('inside on load') - var config = { - root: '', - flow: 'DEFAULT', - data: { - orderId: '{{ order_id}}', - token: '{{ token }}', - tokenType: 'TXN_TOKEN', - amount: '{{ amount }}' - }, - handler: { - notifyMerchant: function(eventName, data) { - // notify about the state of the payment page ( invalid token , session expire , cancel transaction) - console.log('notifyMerchant handler function called'); - console.log('eventName => ', eventName); - console.log('data => ', data); - }, - transactionStatus: function transactionStatus(paymentStatus) { - // provide information to merchant about the payment status. - console.log('transaction status handler function called'); - console.log('paymentStatus => ', paymentStatus); - } - } - }; - - $('.paytm-loading').addClass('hidden'); - if (window.Paytm && window.Paytm.CheckoutJS) { - window.Paytm.CheckoutJS.onLoad(function excecuteAfterCompleteLoad() { - // initialze configuration using init method - window.Paytm.CheckoutJS.init(config) - .then(function onSuccess() { - // after successfully updating configuration, invoke Blink Checkout - window.Paytm.CheckoutJS.invoke(); - }) - .catch(function onError(error) { - console.log('inside the error window') - console.log('error => ', error); - }); - }); - } -} diff --git a/payments/templates/pages/paytm_checkout.html b/payments/templates/pages/paytm_checkout.html index 772e16e..77a2045 100644 --- a/payments/templates/pages/paytm_checkout.html +++ b/payments/templates/pages/paytm_checkout.html @@ -16,12 +16,14 @@ {%- block page_content -%} -

Please do not refresh this page...

+
+

Please do not refresh this page...

{% for name, value in payment_details.items() %} {% endfor %}
+
{% endblock %} diff --git a/payments/templates/pages/paytm_checkout.py b/payments/templates/pages/paytm_checkout.py index 1fa947a..5a55d02 100644 --- a/payments/templates/pages/paytm_checkout.py +++ b/payments/templates/pages/paytm_checkout.py @@ -12,9 +12,6 @@ import requests no_cache = 1 -expected_keys = ('amount', 'title', 'description', 'reference_doctype', 'reference_docname', - 'payer_name', 'payer_email') - def get_context(context): context.no_cache = 1 paytm_config = get_paytm_config()