开心网文档很全面,接入先对简单。
actions API以充值举例
kaixin001_payment
def _generate_sig(params):
hasher = hashlib.md5(''.join(['%s=%s' % (str(x), str(params[x])) for x in sorted(params.keys())]))
hasher.update(SECRET_KEY)
return hasher.hexdigest()
def _generate_kaixin_base_params(session_key, method="actions.pay"):
params = {}
params['api_key'] = API_KEY
params['method'] = method
params['call_id'] = str(time.time())
params['session_key'] = session_key
params['v'] = '1.0'
params['format'] = 'JSON'
return params
def _get_para(params, url=ACTION_URL):
sig = _generate_sig(params)
params['sig'] = sig
data = urllib.urlencode(params)
return _url_base64_encode(data)
def _url_base64_encode(strs):
dics = {"+":"*", "/":"-", "=":""}
base_str = base64.b64encode(strs)
for i in dics:
base_str = base_str.replace(i, dics[i])
return base_str
def get_kaixin_para(session_key, pname, pnumber, pcode, amount, orderid, mode="0", callback=""):
params = _generate_kaixin_base_params(session_key, "actions.pay")
params["pname"] = pname
params["pnumber"] = pnumber
params["pcode"] = pcode
params["amount"] = amount
params["orderid"] = orderid
params["mode"] = mode
params["callback"] = callback
kaixin_para = _get_para(params)
return kaixin_para
def get_payment_para(request):
pearls = request.GET.get('pearls', '0')
sKey = request.GET.get('session_key', None) or request.POST.get('session_key', None)
pearls = int(pearls)
rmb = pearls_to_rmb(pearls)
member = login_member(request, sKey)
if rmb == -1:
return HttpResponse("RMB_ERROR")
record = RechargeRecord(member_id=member.id,
pearls=pearls,
rmb=rmb,
status='UP')
record.save()
pname = "pearls"
pnumber = record.pearls
pcode = "zhenzhu"
amount = record.rmb
orderid = record.id
callback = settings.SITE_URL + "/recharge/callback/"
kx_para = get_kaixin_para(sKey, pname, pnumber, pcode, amount, orderid, mode="0", callback=callback)
return HttpResponse(kx_para)
页面部分JS代码
kaixin001_payment_js
function setHeight(){
var dHeight = document.documentElement.offsetHeight;
if("\v"=="v")
{
dHeight = document.documentElement.scrollHeight;
}
var t = document.createElement("div");
t.innerHTML = '<iframe id="kxiframeagent" src="http://rest.kaixin001.com/api/agent.html#'+dHeight+'" scrolling="yes" height="0px" width="0px"></iframe>';
document.documentElement.appendChild(t.firstChild);
}
function showKxDialog(para)
{
var t = document.createElement("div");
t.innerHTML = '<iframe src="http://www.kaixin001.com/rest/rest.php?para='+para+'" scrolling="yes" height="0px" width="0px" style="display:none"></iframe>';
document.body.appendChild(t.firstChild);
}
function showPaymentDialog(skey, sns_id)
{
$.get('/recharge/get_payment_para/',
{
session_key:"{{ skey }}",
pearls: "{{ pearls }}",
},
function(txt){
if(txt)
{
showKxDialog(txt);
}
}
);
}
普通API,以获取用户id为例
kaixin001_api
def _generate_sig(params):
hasher = hashlib.md5(''.join(['%s=%s' % (x, params[x]) for x in sorted(params.keys())]))
hasher.update(SECRET_KEY)
return hasher.hexdigest()
def _generate_kaixin_base_params(session_key, method="users.getLoggedInUser"):
params = {}
params['api_key'] = API_KEY
params['method'] = method
params['call_id'] = str(time.time())
params['session_key'] = session_key
params['v'] = '1.0'
params['format'] = 'JSON'
return params
def _post_api_request(params):
params['sig'] = _generate_sig(params)
data = urllib.urlencode(params)
req = urllib2.Request(ACTION_URL)
response = urllib2.urlopen(req, data)
result = response.read()
result = simplejson.loads(result)
return result
def get_sns_id(session_key):
try:
params = _generate_kaixin_base_params(session_key, method="users.getLoggedInUser")
result = _post_api_request(params)
return result['result']
except:
return "SNS_API_ERROR"
遇到的问题:
总的来说文档很全,接入很顺利,但是还是有两点问题。
一、开心要求汉字gb2312编码,我用的都是utf8的,开始乱码,后来也没问题,具体参考充值部分代码
二、开心弹窗有时候会被flash挡住。
将<param name="wmode" value="window" />
改为<param name="wmode" value="opaque" />
修改后载入flash代码如下:
View Code
<div id="flash_div" style="margin:0 auto; 756px;">
<object id='forIE' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="756" height="640">
<param name="movie" value="{{ media_url }}swf/{{ media_ver }}/gameMain.swf?v=2"/>
<param name="flashVars" value="skey={{ skey }}&sns=kaixin&feed_fun=sendUserFeed&snsid={{ sns_id }}"/>
<param name="wmode" value="opaque" />
<param name="quality" value="high" />
<param name="allowScriptAccess" value="always" />
<embed name="forIE" src="{{ media_url }}swf/{{ media_ver }}/gameMain.swf?v=2" width="756" height="640" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="opaque" flashVars="skey={{ skey }}&sns=kaixin&feed_fun=sendUserFeed&snsid={{ sns_id }}" allowScriptAccess="always" ></embed>
</object>
</div>