Skip to content

Instantly share code, notes, and snippets.

@alfredrumss
Last active February 26, 2019 23:30
Show Gist options
  • Save alfredrumss/aa45263d641468a8211002c81d199371 to your computer and use it in GitHub Desktop.
Save alfredrumss/aa45263d641468a8211002c81d199371 to your computer and use it in GitHub Desktop.
import urllib, collections, hmac, hashlib, uuid, codecs
import urllib.parse
import urllib.request
#Payment url
PAYMENT_URL = 'https://migs.mastercard.com.au/vpcpay'
SECURE_SECRET = '5A7F9982DA5955FE2CAF9AB946C237EAXXX'
#Payment Data
PAYMENT_DATA = { #Generate vpc_MerchTxnRef Unique
'vpc_ReturnURL': 'https://visitbahrain.bh/payment',
'vpc_OrderInfo': 'ORDER_PAYMENT_VISIT',
'vpc_Locale': 'en_BH',
'vpc_Currency': 'BHD',
'vpc_Command': 'pay',
'vpc_Merchant': 'E11111900',
'vpc_Version': '1',
'vpc_AccessCode': '97F14200',
'vpc_Amount': 10,
'vpc_MerchTxnRef': 'ORDER123'
}
#Revisar
# - cuando tiene espacios en blanco vpc_OrderInfo no genera el mismo del demo
class MakePayment(APIView):
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, format=None):
try:
#print "AMOUNT =>",request.data['amount'], type(request.data['amount'])
PAYMENT_DATA['vpc_Amount'] = int(request.data['amount'] * 1000)
PAYMENT_DATA['vpc_MerchTxnRef'] = 'ORDER'+uuid.uuid4().hex[:20].upper()
payment_key = aux_return_url = None
cache.set(PAYMENT_DATA['vpc_MerchTxnRef'], PAYMENT_DATA['vpc_MerchTxnRef'], 600)
#Shipment request
if 'shipment' in request.data.keys() and request.data['shipment']:
payment_key = uuid.uuid4().hex
aux_return_url = PAYMENT_DATA['vpc_ReturnURL']
PAYMENT_DATA['vpc_ReturnURL'] = PAYMENT_DATA['vpc_ReturnURL'] + "?shipment="+payment_key
payment_data_ordered = collections.OrderedDict(sorted(PAYMENT_DATA.items()))
PAYMENT_DATA['vpc_ReturnURL'] = aux_return_url if aux_return_url else PAYMENT_DATA['vpc_ReturnURL']
#secure_secret_decoded = SECURE_SECRET.decode('hex')
secure_secret_decoded = codecs.decode(SECURE_SECRET,'hex')
url_encoded = urllib.parse.urlencode(payment_data_ordered)
url_decoded = urllib.request.url2pathname(url_encoded)
secure_hash = hmac.new(secure_secret_decoded, url_decoded.encode('utf-8'), hashlib.sha256).hexdigest().upper()
service_payment = PAYMENT_URL+"?"+url_encoded+"&vpc_SecureHash="+secure_hash+"&vpc_SecureHashType=SHA256"
except Exception as e:
print (e)
return Response(status= status.HTTP_400_BAD_REQUEST)
return Response({'service': service_payment, 'payment_key': payment_key },status= status.HTTP_200_OK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment