一、接口申请开通
本代码是基于天聚人合的话费充值API实现的话费充值功能,使用前需要:
- 通过https://www.tianjurenhe.com/docs/api/?id=1申请开通话费接口服务。
- 你可以在个人中心 ➡️ 数据中心 ➡️ 我的API 模块看到此接口的调用凭证请求key
- 与人合签订相关服务合同后,才能正式使用。前期您也可以申请开通测试环境,进行对接测试。
- 详细的接口说明,可参考聚合官方文档。
二、接口使用
2.1、引入项目需要的包
import json, urllib
from urllib import urlencode
)
2.2、配置appkey与主函数
def main():
#配置您申请的APPKey
appkey = "*********************"
#1.订单状态查询
request1(appkey,"GET")
#2.加油卡充值
request2(appkey,"GET")
}
2.3、订单状态查询
def request1(appkey, m="GET"):
url = "http://op.tianjurenhe.com/ofpay/sinopec/ordersta"
params = {
"orderid" : "", #商家订单号,8-32位字母数字组合
"key" : appkey, #应用APPKEY(应用详细页查询)
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
2.4、加油卡充值
def request2(appkey, m="GET"):
url = "http://op.tianjurenhe.com/ofpay/sinopec/onlineorder"
params = {
"proid" : "", #产品id:10000(中石化50元加油卡)[暂不支持]、10001(中石化100元加油卡)、10003(中石化500元加油卡)、10004(中石化1000元加油卡)、10007(中石化任意金额充值)[暂不支持]、10008(中石油任意金额充值)
"cardnum" : "", #充值数量(产品id为10007、10008时为具体充值金额(整数),其余产品id请传固定值1);注:中石油任意冲(产品id:10008)暂时只支持100\200\500\1000
"orderid" : "", #商家订单号,8-32位字母数字组合
"game_userid" : "", #加油卡卡号,中石化:以100011开头的卡号、中石油:以9开头的卡号
"gasCardTel" : "", #持卡人手机号码,可以填写一个固定格式的手机号码,如:18900000000
"gasCardName" : "", #持卡人姓名
"chargeType" : "", #加油卡类型 (1:中石化、2:中石油;默认为1)
"key" : appkey, #应用APPKEY(应用详细页查询)
"sign" : "", #校验值,md5(OpenID+key+proid+cardnum+game_userid+orderid),OpenID在个人中心查询
}
params = urlencode(params)
if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params))
else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content)
if res:
error_code = res["error_code"]
if error_code == 0:
#成功请求
print res["result"]
else:
print "%s:%s" % (res["error_code"],res["reason"])
else:
print "request api error"
2.5、配置程序入口
if __name__ == '__main__':
main()