ソースを参照

fix(paytm-integration): use checksum library to generate/verify checksum

pull/2/head
Mangesh-Khairnar 4年前
コミット
e636ce9c24
2個のファイルの変更1行の追加81行の削除
  1. +0
    -80
      payments/payment_gateways/doctype/paytm_settings/checksum.py
  2. +1
    -1
      payments/payment_gateways/doctype/paytm_settings/paytm_settings.py

+ 0
- 80
payments/payment_gateways/doctype/paytm_settings/checksum.py ファイルの表示

@@ -1,80 +0,0 @@
import base64
import string
import random
import hashlib
import sys

from Crypto.Cipher import AES


iv = '@@@@&&&&####$$$$'
BLOCK_SIZE = 16

if (sys.version_info > (3, 0)):
__pad__ = lambda s: bytes(s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE), 'utf-8')
else:
__pad__ = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)

__unpad__ = lambda s: s[0:-ord(s[-1])]

def encrypt(input, key):
input = __pad__(input)
c = AES.new(key.encode("utf8"), AES.MODE_CBC, iv.encode("utf8"))
input = c.encrypt(input)
input = base64.b64encode(input)
return input.decode("UTF-8")

def decrypt(encrypted, key):
encrypted = base64.b64decode(encrypted)
c = AES.new(key.encode("utf8"), AES.MODE_CBC, iv.encode("utf8"))
param = c.decrypt(encrypted)
if type(param) == bytes:
param = param.decode()
return __unpad__(param)

def generateSignature(params, key):
if not type(params) is dict and not type(params) is str:
raise Exception("string or dict expected, " + str(type(params)) + " given")
if type(params) is dict:
params = getStringByParams(params)
return generateSignatureByString(params, key)

def verifySignature(params, key, checksum):
if not type(params) is dict and not type(params) is str:
raise Exception("string or dict expected, " + str(type(params)) + " given")
if "CHECKSUMHASH" in params:
del params["CHECKSUMHASH"]
if type(params) is dict:
params = getStringByParams(params)
return verifySignatureByString(params, key, checksum)

def generateSignatureByString(params, key):
salt = generateRandomString(4)
return calculateChecksum(params, key, salt)

def verifySignatureByString(params, key, checksum):
paytm_hash = decrypt(checksum, key)
salt = paytm_hash[-4:]
return paytm_hash == calculateHash(params, salt)

def generateRandomString(length):
chars = string.ascii_uppercase + string.digits + string.ascii_lowercase
return ''.join(random.choice(chars) for _ in range(length))

def getStringByParams(params):
params_string = []
for key in sorted(params.keys()):
value = params[key] if params[key] is not None and params[key].lower() != "null" else ""
params_string.append(str(value))
return '|'.join(params_string)

def calculateHash(params, salt):
finalString = '%s|%s' % (params, salt)
hasher = hashlib.sha256(finalString.encode())
hashString = hasher.hexdigest() + salt
return hashString

def calculateChecksum(params, key, salt):
hashString = calculateHash(params, salt)
return encrypt(hashString, key)

+ 1
- 1
payments/payment_gateways/doctype/paytm_settings/paytm_settings.py ファイルの表示

@@ -13,7 +13,7 @@ from frappe import _
from frappe.utils import get_url, call_hook_method, cint, flt, cstr
from frappe.integrations.utils import create_request_log, create_payment_gateway
from frappe.utils import get_request_site_address
from frappe.integrations.doctype.paytm_settings.checksum import generateSignature, verifySignature
from paytmchecksum import generateSignature, verifySignature
from frappe.utils.password import get_decrypted_password

class PaytmSettings(Document):


読み込み中…
キャンセル
保存