-*- coding: utf-8 -*-
import datetime
import logging
from ctypes import *
import socket
from xml.dom import minidom
logger=logging.getLogger(__name__)
class BankTrader(object):
TRANSACT_TYPE_IC='icCard'
TRANSACT_TYPE_STRIP='stripCard'
# 参数全部为字符串,不能是unicode
def __init__(self,host,port,regionId,terminalID,atmNumber):
self.logger=logger
# 装载加密、解密方法
self.fdesdll=cdll.LoadLibrary("fdes.dll")
self.fdesdll.nEncrptPkg.restype=c_int
self.fdesdll.nEncrptPkg.argtypes=[c_char_p,c_char_p,c_char_p,POINTER(c_int)]
self.fdesdll.nDeEncrpt.restype=c_int
self.fdesdll.nDeEncrpt.argtypes=[c_char_p,c_char_p,c_char_p,POINTER(c_int)]
self.host=host
self.port=port
self.regionId=regionId
self.terminalID=terminalID
self.atmNumber=atmNumber
# 密钥明文,必须为字符串,默认为='1234567887654321'
self.encrptionKey='1234567887654321'
# 银行卡BIN号
self.bank_bins=[]
# 最近查询的帐户余额
self.lastBalance=''
# separator='0x',0x010x020x03
# separator=' ',01 02 03
# separator='',010203
def str2hex(self,value,separator='0x'):
v='0x'.join(n.encode('hex') for n in value)
v='0x'+v
if separator=='':
a=v.split('0x')
v=''.join(n for n in a[1:])
return v
elif separator==' ':
a=v.split('0x')
v=' '.join(n for n in a[1:])
return v
else:
return v
# 加密,参数全部为字符串,不能是unicode
def encrpt(self,source):
acheadbody=source
# 保证加密内容的长度为8的倍数
for n in range(8-(len(acheadbody)%8)):
acheadbody=acheadbody+' '
acRadKey=self.encrptionKey
acencheadbody=create_string_buffer(1024*5)
packlen=c_int(len(acheadbody))
success=(self.fdesdll.nEncrptPkg(acheadbody,acencheadbody,acRadKey,byref(packlen))==0)
value=acencheadbody.raw[:packlen.value]
return success,value