Browse Source

fix: add finalize request to complete the transaction

pull/2/head
Mangesh-Khairnar 5 years ago
parent
commit
9cf9839b10
4 changed files with 22 additions and 60 deletions
  1. +19
    -14
      payments/payment_gateways/doctype/paytm_settings/paytm_settings.py
  2. +0
    -42
      payments/templates/includes/paytm_checkout.js
  3. +3
    -1
      payments/templates/pages/paytm_checkout.html
  4. +0
    -3
      payments/templates/pages/paytm_checkout.py

+ 19
- 14
payments/payment_gateways/doctype/paytm_settings/paytm_settings.py View File

@@ -71,8 +71,8 @@ def get_paytm_params(payment_details, order_id, paytm_config):
# initialize a dictionary # initialize a dictionary
paytm_params = dict() 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({ paytm_params.update({
"MID" : paytm_config.merchant_id, "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''' '''Verify checksum for received data in the callback and then verify the transaction'''
paytm_config = get_paytm_config() paytm_config = get_paytm_config()
received_data = frappe._dict(kwargs) received_data = frappe._dict(kwargs)
is_valid_checksum = False


paytm_params = {} paytm_params = {}
for key, value in received_data.items(): for key, value in received_data.items():
if key == 'CHECKSUMHASH': if key == 'CHECKSUMHASH':
paytm_checksum = value paytm_checksum = value
elif key == 'cmd':
continue
else: else:
paytm_params[key] = value 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': if is_valid_checksum and received_data['RESPCODE'] == '01':
verify_transaction_status(paytm_config, received_data['ORDERID']) verify_transaction_status(paytm_config, received_data['ORDERID'])
else: else:
frappe.respond_as_web_page("Payment Failed", 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') 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): def verify_transaction_status(paytm_config, order_id):
'''Verify transaction completion after checksum has been verified''' '''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): def finalize_request(order_id, transaction_response):
request = frappe.get_doc('Integration Request', order_id) 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 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 custom_redirect_to = None
try: 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') request.db_set('status', 'Completed')
except Exception: except Exception:
request.db_set('status', 'Failed') request.db_set('status', 'Failed')
@@ -153,10 +158,10 @@ def finalize_request(order_id, transaction_response):
if custom_redirect_to: if custom_redirect_to:
redirect_to = custom_redirect_to redirect_to = custom_redirect_to


redirect_url = 'payment-success'
redirect_url = '/integrations/payment-success'
else: else:
request.db_set('status', 'Failed') request.db_set('status', 'Failed')
redirect_url = 'payment-failed'
redirect_url = '/integrations/payment-failed'


if redirect_to: if redirect_to:
redirect_url += '?' + urlencode({'redirect_to': 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}) redirect_url += '&' + urlencode({'redirect_message': redirect_message})


frappe.local.response['type'] = 'redirect' frappe.local.response['type'] = 'redirect'
frappe.local.response['location'] = 'redirect_url'
frappe.local.response['location'] = redirect_url


def get_gateway_controller(doctype, docname): def get_gateway_controller(doctype, docname):
reference_doc = frappe.get_doc(doctype, docname) reference_doc = frappe.get_doc(doctype, docname)


+ 0
- 42
payments/templates/includes/paytm_checkout.js View File

@@ -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);
});
});
}
}

+ 3
- 1
payments/templates/pages/paytm_checkout.html View File

@@ -16,12 +16,14 @@


{%- block page_content -%} {%- block page_content -%}
<body> <body>
<center><h1>Please do not refresh this page...</h1></center>
<div style="margin: 30vh;">
<center><h2>Please do not refresh this page...</h2></center>
<form method="post" action="{{ url }}" name="paytm_form"> <form method="post" action="{{ url }}" name="paytm_form">
{% for name, value in payment_details.items() %} {% for name, value in payment_details.items() %}
<input type="hidden" name="{{ name }}" value="{{ value }}"> <input type="hidden" name="{{ name }}" value="{{ value }}">
{% endfor %} {% endfor %}
</form> </form>
</div>
</body> </body>
{% endblock %} {% endblock %}




+ 0
- 3
payments/templates/pages/paytm_checkout.py View File

@@ -12,9 +12,6 @@ import requests


no_cache = 1 no_cache = 1


expected_keys = ('amount', 'title', 'description', 'reference_doctype', 'reference_docname',
'payer_name', 'payer_email')

def get_context(context): def get_context(context):
context.no_cache = 1 context.no_cache = 1
paytm_config = get_paytm_config() paytm_config = get_paytm_config()


Loading…
Cancel
Save