- c# 后台 APP 微信支付接口和微信公众号支付差不多
- #region 微信 APP 支付接口
- /// <summary>
- /// 微信 APP 支付接口
- /// </summary>
- /// <param name="out_trade_no"> 支付订单号 </param
- /// <param name="total_fee"> 支付金额 </param>
- /// <param name="type">1. 安卓, 2.IOS</param>
- /// <returns></returns>
- [HttpGet]
- public CommonResponse<Wx_pay> APP_Pay(string out_trade_no,Double total_fee,int type)
- {
- PayMent pm = new PayMent();
- string ip = HttpContext.Current.Request.UserHostAddress;
- int money =int.Parse((total_fee * 100).ToString());
- Wx_pay wp = pm.APP_PayMent("服务费",ip,money,out_trade_no,type);
- if (wp.sign == "") {
- return new CommonResponse<Wx_pay> { Data = null, Message = "系统异常, 联系管理员!", Code = EnumResponseCode.Fail.GetHashCode() };
- }
- return new CommonResponse<Wx_pay> { Data = wp, Message = String.Empty, Code = EnumResponseCode.Successful.GetHashCode() };
- }
- #endregion
- ///
- /// 是对返回数据类型
- ///
- public class CommonResponse<T>
- {
- public T Data { get; set; }
- /// <summary>
- /// 代码 1: 失败, 2: 成功
- /// </summary>
- public int Code { get; set; }
- /// <summary>
- /// 消息
- /// </summary>
- public string Message { get; set; }
- /// <summary>
- /// 其他附带信息
- /// </summary>
- public string Tag { get; set; }
- }
- /// <summary>
- /// 微信 APP 支付实体
- /// </summary>
- public class Wx_pay
- {
- /// <summary>
- /// 应用 ID
- /// </summary>
- public string appid { set; get; } = "";
- /// <summary>
- /// 商户号
- /// </summary>
- public string partnerid { set; get; } = "";
- /// <summary>
- /// 预支付交易会话 ID
- /// </summary>
- public string prepayid { set; get; } = "";
- /// <summary>
- /// 扩展字段
- /// </summary>
- public string package { set; get; } = "Sign=WXPay";
- /// <summary>
- /// 随机字符串
- /// </summary>
- public string noncestr { set; get; } = "";
- /// <summary>
- /// 时间戳
- /// </summary>
- public string timestamp { set; get; } = "";
- /// <summary>
- /// 签名
- /// </summary>
- public string sign { set; get; } = "";
- }
- #region 微信 APP 支付
- /// <summary>
- /// 微信 APP 支付
- /// </summary>
- /// <param name="boby"> 商品描述 </param>
- /// <param name="mch_id"> 商户号 </param>
- /// <param name="spbill_create_ip"> 终端 IP</param>
- /// <param name="total_fee"> 金额 </param>
- /// <param name="out_trade_no"> 商户订单号 </param>
- /// <returns></returns>
- public Wx_pay APP_PayMent( string boby, string spbill_create_ip, int total_fee, string out_trade_no,int type)
- {
- UnifiedOrder order = new UnifiedOrder();
- if (type == 1)
- {
- order.appid = APP_Aconfig.appid;
- order.mch_id = APP_Aconfig.partnerid;
- }
- else {
- order.appid = APP_Iconfig.appid;
- order.mch_id = APP_Iconfig.partnerid;
- }
- order.attach = "APP 名称 - 支付内容说明";
- order.body = boby;
- order.device_info = "web";
- order.nonce_str = TenpayUtil.getNoncestr();
- order.notify_url = APP_Aconfig.url;
- order.out_trade_no = out_trade_no;
- order.trade_type = "APP";
- order.spbill_create_ip = spbill_create_ip;
- order.total_fee = total_fee;
- TenpayUtil tenpay = new TenpayUtil();
- string paySignKey = string.Empty;
- if (type == 1)
- {
- paySignKey = APP_Aconfig.paysignkey;
- }
- else {
- paySignKey = APP_Iconfig.paysignkey;
- }
- string prepay_id = tenpay.getPrepay_id(order, paySignKey);
- string timeStamp = TenpayUtil.getTimestamp();
- string nonceStr = TenpayUtil.getNoncestr();
- SortedDictionary<string, object> sParams = new SortedDictionary<string, object>();
- sParams.Add("appId", APP_Aconfig.appid);
- sParams.Add("timeStamp", timeStamp);
- sParams.Add("nonceStr", nonceStr);
- sParams.Add("package", "prepay_id=" + prepay_id);
- sParams.Add("signType", "MD5");
- string paySign = tenpay.getsign(sParams, paySignKey);
- Wx_pay wp = new Wx_pay();
- if (type == 1)
- {
- wp.appid = APP_Aconfig.appid;
- wp.partnerid = APP_Aconfig.partnerid;
- }
- else {
- wp.appid = APP_Iconfig.appid;
- wp.partnerid = APP_Iconfig.partnerid;
- }
- wp.noncestr = nonceStr;
- wp.prepayid = prepay_id;
- wp.sign = paySign;
- wp.timestamp = timeStamp;
- return wp;
- }
- #endregion
- /// <summary>
- /// APP 安卓支付配置
- /// </summary>
- public class APP_Aconfig
- {
- public const string appid = "";//APPID
- public const string partnerid = "";// 商户号
- public const string paysignkey = "";// 证书密匙
- public const string url = "";// 回调 ur
- }
- /// <summary>
- /// APPios 支付配置
- /// </summary>
- public class APP_Iconfig
- {
- public const string appid = "";//APPID
- public const string partnerid = "";// 商户号
- public const string paysignkey = "";// 证书密匙
- public const string url = "";// 回调 url
- }
对于 TenpayUtil tenpay = new TenpayUtil(); 这个类其他文章上有代码
请求方式: http get
接口参数说明
名称 类型 必填 说明
out_trade_no string 是 支付订单 ID
total_fee Double 是 支付金额
type int 是 1. 安卓, 2.IOS
返回示例:
- /**** 失败示例 **/
- {
- "Code":1, /* 状态码 1: 失败 2: 成功 */
- "Message":"错误信息",/* 消息 (一般失败时, 返回错误信息)*/
- "Tag":""/* 附加信息 */
- }
- /**** 成功示例 **/
- {
- "Code":2, /* 状态码 1: 失败 2: 成功 */
- Data:[]
- "Message":"",/* 消息 (一般失败时, 返回错误信息)*/"Tag":" 附加信息 "/* */
- }
接口返回 Data 集合参数说明
名称 类型 说明
appid string 应用 ID
partnerid String 商户号
prepayid String 预支付交易会话 ID
package String 扩展字段
noncestr String 随机字符串
timestamp String 时间戳
sign String 签名
本人还是个新手 (刚入行不久), 请多多关照, 后续还有微信其他内容
这是测试接口返回结果:
下篇微信支付 (PC 端扫码支付)
来源: http://www.bubuko.com/infodetail-2522066.html