首页 手机话费充值 手机话费充值示例代码 话费充值[Python]

话费充值示例代码[Python]

作者:liuxiaojie 阅读数:4525 上传时间:2016-03-10

话费充值

#!/usr/bin/python
# encoding:utf-8

import urllib2, json, urllib, hashlib

def md5(str):
    m = hashlib.md5()
    m.update(str)
    return m.hexdigest()

def makeSign(queryarr, appsecret):
    queryarr = sorted(queryarr.items(),key=lambda e:e[0])
    strs = ""
    for str in queryarr:
        strs += "%s" %str[1]

    return md5(strs + appsecret)


#  1、手机充值


data = {}
data["appkey"] = "your_appkey_here"
data["mobile"] = "15158825888"   # 充值账号
data["amount"] = 30 # 充值金额
data["outorderno"] = "66522311111"   # 商家订单号 可选
data["appsecret"] = "sssssssssss"   # 你的appsecret
queryarr = {"mobile":data["mobile"],"amount":data["amount"],"outorderno":data["outorderno"]}
sign = makeSign(queryarr , data["appsecret"])
data["sign"] = sign

url_values = urllib.urlencode(data)
url = "https://api.jisuapi.com/mobilerecharge/recharge" + "?" + url_values
request = urllib2.Request(url)
result = urllib2.urlopen(request)
jsonarr = json.loads(result.read())

if jsonarr["status"] != u"0":
    print jsonarr["msg"]
    exit()
result = jsonarr["result"]

print result["mobile"],result["amount"],result["outorderno"]
print result["orderno"],result["totalfee"],result["rechargestatus"]