一、接口申请开通
本代码是基于天聚人合的话费充值API实现的话费充值功能,使用前需要:
- 通过https://www.tianjurenhe.com/docs/api/?id=2申请开通话费接口服务。
– 你可以在个人中心 ➡️ 数据中心 ➡️ 我的API 模块看到此接口的调用凭证请求key - 与人合签订相关服务合同后,才能正式使用。前期您也可以申请开通测试环境,进行对接测试。
- 详细的接口说明,可参考聚合官方文档。
二、接口使用
2.1、引入项目需要的包
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Xfrog.Net;
using System.Diagnostics;
using System.Web;
2.2、配置appkey与主函数
在下方主要业务的注释处加入2.3-2.7的接口调用代码,在调用到的方法注释处加入2.8的方法代码
namespace ConsoleAPI
{
class Program
{
static void Main(string[] args)
{
string appkey = "*******************"; //配置您申请的appkey
//此处写调用接口的主要业务(2.3-2.7)
}
//此处写业务中调用到的方法(2.8)
}
}
2.3、提交话费充值订单
string url7 = "http://op.tianjurenhe.com/ofpay/mobile/onlineorder";
var parameters7 = new Dictionary<string, string>();
parameters7.Add("phoneno" , ""); //手机号码
parameters7.Add("cardnum" , ""); //充值金额,目前可选:1、2、5、10、20、30、50、100、200、300、500
parameters7.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters7.Add("key", appkey);//你申请的key
parameters7.Add("sign" , ""); //校验值,md5(OpenID+key+phoneno+cardnum+orderid),OpenID在个人中心查询
string result7 = sendPost(url7, parameters7, "get");
JsonObject newObj7 = new JsonObject(result7);
String errorCode7 = newObj7["error_code"].Value;
if (errorCode7 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj7);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj7["error_code"].Value+":"+newObj7["reason"].Value);
}
2.4、订单状态查询
string url2 = "http://op.tianjurenhe.com/ofpay/mobile/ordersta";
var parameters2 = new Dictionary<string, string>();
parameters2.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters2.Add("key", appkey);//你申请的key
string result2 = sendPost(url2, parameters2, "get");
JsonObject newObj2 = new JsonObject(result2);
String errorCode2 = newObj2["error_code"].Value;
if (errorCode2 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj2);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj2["error_code"].Value+":"+newObj2["reason"].Value);
}
2.5、根据手机及面额查询是否支持充值
string url5 = " http://op.tianjurenhe.com/ofpay/mobile/telcheck";
var parameters5 = new Dictionary<string, string>();
parameters5.Add("phoneno" , ""); //手机号码
parameters5.Add("cardnum" , ""); //充值金额,目前可选:1、2、5、10、20、30、50、100、200、300、500
parameters5.Add("key", appkey);//你申请的key
string result5 = sendPost(url5, parameters5, "get");
JsonObject newObj5 = new JsonObject(result5);
String errorCode5 = newObj5["error_code"].Value;
if (errorCode5 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj5);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj5["error_code"].Value+":"+newObj5["reason"].Value);
}
2.6、根据手机和面额获取商品信息
string url6 = "http://op.tianjurenhe.com/ofpay/mobile/telquery";
var parameters6 = new Dictionary<string, string>();
parameters6.Add("phoneno" , ""); //手机号码
parameters6.Add("cardnum" , ""); //充值金额,目前可选:1、2、5、10、20、30、50、100、200、300、500
parameters6.Add("key", appkey);//你申请的key
string result6 = sendPost(url6, parameters6, "get");
JsonObject newObj6 = new JsonObject(result6);
String errorCode6 = newObj6["error_code"].Value;
if (errorCode6 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj6);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj6["error_code"].Value+":"+newObj6["reason"].Value);
}
2.7、订单状态通知
string url2 = "http://op.tianjurenhe.com/ofpay/mobile/ordersta";
var parameters2 = new Dictionary<string, string>();
parameters2.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters2.Add("key", appkey);//你申请的key
string result2 = sendPost(url2, parameters2, "get");
JsonObject newObj2 = new JsonObject(result2);
String errorCode2 = newObj2["error_code"].Value;
if (errorCode2 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj2);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj2["error_code"].Value+":"+newObj2["reason"].Value);
}
2.8、以上代码所调用的方法
/// <summary>
/// Http (GET/POST)
/// </summary>
/// <param name="url">请求URL</param>
/// <param name="parameters">请求参数</param>
/// <param name="method">请求方法</param>
/// <returns>响应内容</returns>
static string sendPost(string url, IDictionary<string, string> parameters, string method)
{
if (method.ToLower() == "post")
{
HttpWebRequest req = null;
HttpWebResponse rsp = null;
System.IO.Stream reqStream = null;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = method;
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
req.Timeout = 5000;
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
byte[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, "utf8"));
reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
if (reqStream != null) reqStream.Close();
if (rsp != null) rsp.Close();
}
}
else
{
//创建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + BuildQuery(parameters, "utf8"));
//GET请求
request.Method = "GET";
request.ReadWriteTimeout = 5000;
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
//返回内容
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
/// <summary>
/// 组装普通文本请求参数。
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
static string BuildQuery(IDictionary<string, string> parameters, string encode)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
{
if (hasParam)
{
postData.Append("&");
}
postData.Append(name);
postData.Append("=");
if (encode == "gb2312")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
}
else if (encode == "utf8")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
}
else
{
postData.Append(value);
}
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// 把响应流转换为文本。
/// </summary>
/// <param name="rsp">响应流对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>响应文本</returns>
static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
System.IO.Stream stream = null;
StreamReader reader = null;
try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
reader = new StreamReader(stream, encoding);
return reader.ReadToEnd();
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
}
}