Skip to content

Instantly share code, notes, and snippets.

@isDipesh
Created April 26, 2022 11:01
Show Gist options
  • Save isDipesh/ec308e2b62527b97ebcae5d3d616642f to your computer and use it in GitHub Desktop.
Save isDipesh/ec308e2b62527b97ebcae5d3d616642f to your computer and use it in GitHub Desktop.
Fonepay transaction verification
import requests
import hashlib
import hmac
import base64
import json
from django.conf import settings
def base64(st):
return base64.b64encode(st.encode('utf-8')).decode('utf-8')
def verify(params):
auth_str = '{}:{}'.format(settings.FONEPAY_USERNAME, settings.FONEPAY_PASSWORD)
auth_str = 'Basic {}'.format(base64(auth_str))
concat = '{},{},{},{},{},{{"prn": "{}","merchantCode": "{}","amount": "{}"}}'. \
format(settings.FONEPAY_USERNAME,
settings.FONEPAY_PASSWORD, 'POST',
'application/json',
'/merchant/merchantDetailsForThirdParty/txnVerification',
params['PRN'],
settings.FONEPAY_MERCHANT_CODE,
int(float(self.amount)),
).encode('utf8')
sign = hmac.new(settings.FONEPAY_SECRET.encode('utf8'), concat, hashlib.sha512).hexdigest()
headers = {
'Content-Type': 'application/json',
'Authorization': auth_str,
'auth': sign,
}
data = {"prn": params['PRN'], "merchantCode": settings.FONEPAY_MERCHANT_CODE, "amount": self.amount}
response = requests.post(settings.FONEPAY_VERIFICATION_URL, data=json.dumps(data), headers=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment