预览
代码
手机端效果
demoWeiXin.cs
demoWeiXin.Designer.cs
demoWeiXinPay.cs
demoWeiXinPay.Designer.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using Smobiler.Core;
using Smobiler.Core.Controls;
using Smobiler.Plugins;
namespace Smobiler.Tutorials.Plugins
{
partial class demoWeiXin : Smobiler.Core.Controls.MobileForm
{
public demoWeiXin()
: base()
{
//This call is required by the SmobilerForm.
InitializeComponent();
}
private string appid = ""; //微信开放平台审核通过的应用APPID(请登录open.weixin.qq.com查看,注意与公众号的APPID不同)
private string secret = ""; //微信开放平台审核通过后的应用密钥AppSecret
private string universalLink = ""; //由于苹果iOS 13系统版本安全升级,ios需要配置,具体可以查看https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/iOS.html
private const string accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token"; //登陆获取access token
private const string userinfoUrl = "https://api.weixin.qq.com/sns/userinfo"; //根据openid获取用户信息
private void title1_ImagePress(object sender, EventArgs e)
{
this.Close();
}
private void demoWeiXin_Load(object sender, EventArgs e)
{
ListMenuViewGroup g1 = new ListMenuViewGroup();
g1.AddItem("logon", "注册", "Reg", "");
g1.AddItem("logon", "登录", "Login", "");
g1.AddItem("logon", "支付", "Pay", "");
g1.AddItem("logon", "分享消息给好友", "ShareNews", "");
g1.AddItem("logon", "分享文本给好友", "ShareText", "");
g1.AddItem("logon", "分享图片给好友", "ShareImage", "");
g1.AddItem("logon", "分享视频给好友", "ShareVideo", "");
g1.AddItem("logon", "分享音频给好友", "ShareAudio", "");
g1.AddItem("logon", "分享消息到朋友圈", "TimeLineNews", "");
g1.AddItem("logon", "分享文本到朋友圈", "TimeLineText", "");
g1.AddItem("logon", "分享图片到朋友圈", "TimeLineImage", "");
g1.AddItem("logon", "分享视频到朋友圈", "TimeLineVideo", "");
g1.AddItem("logon", "分享音频到朋友圈", "TimeLineAudio", "");
g1.AddItem("logon", "检测是否安装微信", "isWXAppInstalled", "");
g1.AddItem("logon", "检测是否支持微信api", "isWXAppSupportApi", "");
g1.AddItem("logon", "获取微信itunes下载地址(ios独有)", "getWXAppInstallUrl", "");
g1.AddItem("logon", "获取微信api版本", "getApiVersion", "");
g1.AddItem("logon", "打开微信", "openWXApp", "");
listMenuView1.Groups.Add(g1);
}
private void listMenuView1_ItemPress(object sender, ListMenuViewItemPressEventArgs e)
{
WeiXinShareEntity entity;
switch (e.Item.SubContent)
{
case "Reg":
this.weiXin1.registerApp(appid,(obj, args) => { }, universalLink);//每个设备只需注册一次,方法参数中的appid为微信平台注册应用的appid
break;
case "Login":
WeiXinLogin();
break;
case "Pay":
Show(new demoWeixinPay());
break;
case "ShareNews":
entity = new WeiXinShareNewsEntity("百度一下你就知道News", "百度一下你就知道", "https://www.baidu.com/");
this.weiXin1.shareToSession(entity, HandleWeiXinRPCResult);
break;
case "ShareText":
entity = new WeiXinShareTextEntity("这是一个分享标题Text", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToSession(entity, HandleWeiXinRPCResult);
break;
case "ShareImage":
entity = new WeiXinShareImageEntity("这是一个分享标题Image", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToSession(entity, HandleWeiXinRPCResult);
break;
case "ShareVideo":
entity = new WeiXinShareVideoEntity("这是一个分享标题Video", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToSession(entity, HandleWeiXinRPCResult);
break;
case "ShareAudio":
entity = new WeiXinShareAudioEntity("这是一个分享标题Audio", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToSession(entity, HandleWeiXinRPCResult);
break;
case "TimeLineNews":
entity = new WeiXinShareNewsEntity("百度一下你就知道News", "百度一下你就知道", "https://www.baidu.com/");
this.weiXin1.shareToTimeline(entity, HandleWeiXinRPCResult);
break;
case "TimeLineText":
entity = new WeiXinShareTextEntity("这是一个分享标题Text", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToTimeline(entity, HandleWeiXinRPCResult);
break;
case "TimeLineImage":
entity = new WeiXinShareImageEntity("这是一个分享标题Image", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToTimeline(entity, HandleWeiXinRPCResult);
break;
case "TimeLineVideo":
entity = new WeiXinShareVideoEntity("这是一个分享标题Video", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToTimeline(entity, HandleWeiXinRPCResult);
break;
case "TimeLineAudio":
entity = new WeiXinShareAudioEntity("这是一个分享标题Audio", "这是一个分享文本", "http://www.smobiler.com/images/index/logo.png");
this.weiXin1.shareToTimeline(entity, HandleWeiXinRPCResult);
break;
case "isWXAppInstalled":
this.weiXin1.isWXAppInstalled(HandleWeiXinRPCResult);
break;
case "isWXAppSupportApi":
this.weiXin1.isWXAppSupportApi(HandleWeiXinRPCResult);
break;
case "getWXAppInstallUrl":
this.weiXin1.getWXAppInstallUrl((s, ss) => { RedirectUrl(ss.args.ToString()); });
break;
case "getApiVersion":
this.weiXin1.getApiVersion(HandleWeiXinRPCResult);
break;
case "openWXApp":
this.weiXin1.openWXApp(HandleWeiXinRPCResult);
break;
}
}
private void HandleWeiXinRPCResult(object sender, Smobiler.Core.Controls.ComponentResultArgs e)
{
this.Toast(string.Format("isError{0},error{1},errorCode{2}", e.isError, e.error, e.errorCode));
}
private void WeiXinLogin()
{
//参考 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317851&token=&lang=zh_CN
//第一个参数为scope,获取用户个人信息则填写snsapi_userinfo
//第二个参数为state,设置为一个随机数或应用信息即可,用于执行检验
this.weiXin1.loginWithWeixin("snsapi_userinfo", "demo_smowx_login", (obj, args) =>
{
//这里需要判断是否返回正确,错误信息会保存在error及errorCode中
if (args.isError == false)
{
//如果没有错误,会返回一个code,code在args参数中
string code = args.args.ToString();
//根据code去获取access_token
WeXinAccessTokenResponseEntity accessTokenEntity = GetAccessTokenByCode(code);
if (accessTokenEntity.errcode == 0)
{
//根据获取到的access token获取用户信息
var userEntity = GetUserInfoByOpenid(accessTokenEntity.access_token, accessTokenEntity.openid);
//这里要判断一下结果是否有错误
if (userEntity.errcode == 0)
{
Toast(string.Format("用户信息获取成功:{0}/{1}", userEntity.openid, userEntity.nickname));
}
else
{
Toast(string.Format("get userinfo error: {0}/{1}", userEntity.errcode, userEntity.errmsg));
}
}
else
{
Toast(string.Format("get access token error: {0}/{1}", accessTokenEntity.errcode, accessTokenEntity.errmsg));
}
}
else
{
Toast(string.Format("login error: {0}/{1}", args.errorCode, args.error));
}
});
}
private WeXinAccessTokenResponseEntity GetAccessTokenByCode(string code)
{
//获取的格式为https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
//appid为申请的应用号,secret是应用密钥,code是在调用登陆后获取的参数,grant_type固定为authorization_code
string accessTokenParameterUrl = string.Format("{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code", accessTokenUrl, appid, secret, code);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(accessTokenParameterUrl);
myHttpWebRequest.Method = "GET";
//获取结果
HttpWebResponse result = (HttpWebResponse)myHttpWebRequest.GetResponse();
string resultStr = "";
if (result.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(result.GetResponseStream()))
{
resultStr = reader.ReadToEnd();
//返回的是json格式,这里直接做序列化即可
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<WeXinAccessTokenResponseEntity>(resultStr);
}
}
else
{
return new WeXinAccessTokenResponseEntity() { errcode = -1, errmsg = "HTTP错误状态" + result.StatusCode };
}
}
private WeXinUserInfoResponseEntity GetUserInfoByOpenid(string accessToken, string openid)
{
//获取的格式为https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
//access_token是前面获取的调用凭证,openid是用户标识
string parameterUrl = string.Format("{0}?access_token={1}&openid={2}", userinfoUrl, accessToken, openid);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(parameterUrl);
myHttpWebRequest.Method = "GET";
//获取结果
HttpWebResponse result = (HttpWebResponse)myHttpWebRequest.GetResponse();
string resultStr = "";
if (result.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(result.GetResponseStream()))
{
resultStr = reader.ReadToEnd();
//返回的是json格式,这里直接做序列化即可
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<WeXinUserInfoResponseEntity>(resultStr);
}
}
else
{
return new WeXinUserInfoResponseEntity() { errcode = -1, errmsg = "HTTP错误状态" + result.StatusCode };
}
}
}
public abstract class WexinGetResponseEntity
{
//如果出现错误,会有错误编号
public int errcode { get; set; }
//如果出现错误,错误编号对应的错误信息
public string errmsg { get; set; }
}
//具体参数信息详见 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=zh_CN
public class WeXinAccessTokenResponseEntity : WexinGetResponseEntity
{
//-------如果没有出现错误返回如下内容-------
//接口调用凭证
public string access_token { get; set; }
//access_token接口调用凭证超时时间,单位(秒)
public int expires_in { get; set; }
//用户刷新access_token
public string refresh_token { get; set; }
//授权用户唯一标识
public string openid { get; set; }
//用户授权的作用域,使用逗号(,)分隔
public string scope { get; set; }
//当且仅当该移动应用已获得该用户的userinfo授权时,才会出现该字段
public string unionid { get; set; }
}
public class WeXinUserInfoResponseEntity : WexinGetResponseEntity
{
//-------如果没有出现错误返回如下内容-------
//普通用户的标识,对当前开发者帐号唯一
public string openid { get; set; }
//普通用户昵称
public string nickname { get; set; }
//普通用户性别,1为男性,2为女性
public int sex { get; set; }
//普通用户个人资料填写的省份
public string province { get; set; }
//普通用户个人资料填写的城市
public string city { get; set; }
//国家,如中国为CN
public string country { get; set; }
//用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
public string headimgurl { get; set; }
//用户特权信息,json数组,如微信沃卡用户为(chinaunicom)
public string[] privilege { get; set; }
//用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。
//开发者最好保存unionID信息,以便以后在不同应用之间进行用户信息互通。
public string unionid { get; set; }
}
}
using System;
using Smobiler.Core;
namespace Smobiler.Tutorials.Plugins
{
partial class demoWeiXin : Smobiler.Core.Controls.MobileForm
{
#region "SmobilerForm generated code "
//SmobilerForm overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
//NOTE: The following procedure is required by the SmobilerForm
//It can be modified using the SmobilerForm.
//Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.title1 = new Smobiler.Core.Controls.Title();
this.listMenuView1 = new Smobiler.Core.Controls.ListMenuView();
this.weiXin1 = new Smobiler.Plugins.WeiXin();
//
// title1
//
this.title1.ImageType = Smobiler.Core.Controls.ImageEx.ImageStyle.FontIcon;
this.title1.Name = "title1";
this.title1.ResourceID = "angle-left";
this.title1.Size = new System.Drawing.Size(300, 30);
this.title1.Text = "WeiXin";
this.title1.ImagePress += new System.EventHandler(this.title1_ImagePress);
//
// listMenuView1
//
this.listMenuView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listMenuView1.Location = new System.Drawing.Point(0, 30);
this.listMenuView1.Name = "listMenuView1";
this.listMenuView1.Size = new System.Drawing.Size(300, 300);
this.listMenuView1.ItemPress += new Smobiler.Core.Controls.ListMenuViewItemPressEventHandler(this.listMenuView1_ItemPress);
//
// weiXin1
//
this.weiXin1.Name = "weiXin1";
//
// demoWeiXin
//
this.Components.AddRange(new Smobiler.Core.Controls.MobileComponent[] {
this.weiXin1});
this.Controls.AddRange(new Smobiler.Core.Controls.MobileControl[] {
this.title1,
this.listMenuView1});
this.Load += new System.EventHandler(this.demoWeiXin_Load);
this.Name = "demoWeiXin";
}
#endregion
private Core.Controls.Title title1;
private Core.Controls.ListMenuView listMenuView1;
private Smobiler.Plugins.WeiXin weiXin1;
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using Smobiler.Core;
using Smobiler.Core.Controls;
namespace Smobiler.Tutorials.Plugins
{
partial class demoWeixinPay : Smobiler.Core.Controls.MobileForm
{
private string key = ""; //商户平台设置的密钥key
private string appid = ""; //微信开放平台审核通过的应用APPID(请登录open.weixin.qq.com查看,注意与公众号的APPID不同)
private string mch_id = ""; //微信支付分配的商户号
private string out_trade_no = ""; //外部订单号
private const string unifieOrderUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //微信创建订单地址
private const string orderqueryUrl = "https://api.mch.weixin.qq.com/pay/orderquery"; //微信支付订单的查询地址
public demoWeixinPay() : base()
{
//This call is required by the SmobilerForm.
InitializeComponent();
}
private void title1_ImagePress(object sender, EventArgs e)
{
this.Close();
}
private void demoWeixinPay_Load(object sender, EventArgs e)
{
//注册APPID
this.weiXin1.registerApp(appid);
CreateOutTradeNo();
}
private void btnWXPay_Press(object sender, EventArgs e)
{
try
{
Pay(UnifiedOrder());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CreateOutTradeNo()
{
out_trade_no = "SMOAPP" + DateTime.Now.ToString("yyyyMMddHHmmss");
this.labOutOrder.Text = out_trade_no;
}
/// <summary>
/// https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
/// 调起支付时,需要先调用微信接口生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付
/// </summary>
private WeXinPayAPIResponseEntity UnifiedOrder()
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(unifieOrderUrl);
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
SortedDictionary<string, string> payDict = new SortedDictionary<string, string>();
payDict.Add("appid", appid);//应用ID
payDict.Add("mch_id", mch_id);//商户号
payDict.Add("device_info", this.Client.DeviceID);//设备号
payDict.Add("nonce_str", System.Guid.NewGuid().ToString("N"));//随机字符串
payDict.Add("body", "请支持0.01元");//商品描述
payDict.Add("out_trade_no", out_trade_no);//外部订单编号
payDict.Add("total_fee", "1");//金额,单位是分,100代表为1元
payDict.Add("spbill_create_ip", this.Client.IPEndPoint.Address.ToString());//获取客户端的IP地址
payDict.Add("notify_url", "http://www.weixin.qq.com/wxpay/pay.php");//由于notify_url为必须的字段,如果有通知地址就填写一个,如果没有的话,可随意填写一个
payDict.Add("trade_type", "APP");//这里固定为APP
string payXmlStr = CreateRequestXML(payDict);
byte[] encodedBytes = Encoding.UTF8.GetBytes(payXmlStr);
myHttpWebRequest.ContentLength = encodedBytes.Length;
Stream requestStream = myHttpWebRequest.GetRequestStream();
requestStream.Write(encodedBytes, 0, encodedBytes.Length);
requestStream.Close();
//获取结果
HttpWebResponse result = (HttpWebResponse)myHttpWebRequest.GetResponse();
string resultStr = "";
if (result.StatusCode == HttpStatusCode.OK)
{
using (Stream mystream = result.GetResponseStream())
{
using (StreamReader reader = new StreamReader(mystream))
{
resultStr = reader.ReadToEnd();
}
}
}
return WeXinPayAPIResponseEntity.Create(resultStr);
}
public string CreateRequestXML(SortedDictionary<string, string> requestDict)
{
XmlDocument doc = new XmlDocument();
XmlElement xmlNode = doc.CreateElement("xml");
var props = this.GetType().GetProperties();
//signDictList主要用于生成签名,详见 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3
var signDictList = new List<string>();
foreach (var pop in requestDict)
{
if (string.IsNullOrEmpty(pop.Value) == false)
{
XmlElement element = doc.CreateElement(pop.Key);
element.InnerText = pop.Value;
xmlNode.AppendChild(element);
signDictList.Add(pop.Key + "=" + element.InnerText);
}
}
//使用MD5生成签字算法
signDictList.Add("key=" + key);
string signTempStr = string.Join("&", signDictList);
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
string signStr = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(signTempStr))).Replace("-", "");
XmlElement sign = doc.CreateElement("sign");
sign.InnerText = signStr;
xmlNode.AppendChild(sign);
doc.AppendChild(xmlNode);
return doc.OuterXml;
}
//调用支付接口
private void Pay(WeXinPayAPIResponseEntity resultEntity)
{
if (resultEntity.return_code == "SUCCESS")
{
if (resultEntity.result_code == "SUCCESS")
{
//参数说明详见 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12
Smobiler.Plugins.WeiXinPayEntity appPayEntity = new Smobiler.Plugins.WeiXinPayEntity()
{
nonceStr = resultEntity.nonce_str,
package = "Sign=WXPay",
partnerId = resultEntity.mch_id,
prepayId = resultEntity.prepay_id,
timeStamp = ((int)((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000)).ToString()
};
//按签名规范重新生成签名,注意package的值格式为Sign=WXPay
//https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3
string signTempStr = string.Format("appid={0}&noncestr={1}&package={2}&partnerid={3}&prepayid={4}×tamp={5}&key={6}", resultEntity.appid, appPayEntity.nonceStr, appPayEntity.package, appPayEntity.partnerId, appPayEntity.prepayId, appPayEntity.timeStamp, key);
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
appPayEntity.sign = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(signTempStr))).Replace("-", "");
//调用支付功能
this.weiXin1.pay(appPayEntity, (obj, args) =>
{
if (args.isError == true)
{
MessageBox.Show(string.Format("APP Pay Error: {0}", args.error));
}
else
{
//如果没有返回错误,代表支付成功,那么这里直接去查询订单状态
//一定要以具体查询的结果为准,不要以APP的返回状态为准
//https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_2
Toast("支付成功!");
}
});
}
else
{
throw new Exception("WeXinPayAPIResponseError " + resultEntity.err_code + "/" + resultEntity.err_code_des);
}
}
else
{
throw new Exception("WeXinPayAPIResponseError " + resultEntity.return_msg);
}
}
private void btnWXLogin_Press(object sender, EventArgs e)
{
}
private void btnOutOrder_Press(object sender, EventArgs e)
{
CreateOutTradeNo();
}
}
/// <summary>
/// 微信支付实体类
/// 详见 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1 请求参数
/// </summary>
/// <summary>
/// 微信支付结果类
/// 详见 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1 返回结果
/// </summary>
public class WeXinPayAPIResponseEntity
{
/// <summary>
/// SUCCESS/FAIL
/// 此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断
/// </summary>
public string return_code { get; set; }
/// <summary>
/// 返回信息,如非空,为错误原因
/// 签名失败
/// 参数格式校验错误
/// </summary>
public string return_msg { get; set; }
//--------------以下字段在return_code为SUCCESS的时候有返回
/// <summary>
/// 调用接口提交的应用ID
/// </summary>
public string appid { get; set; }
/// <summary>
/// 调用接口提交的商户号
/// </summary>
public string mch_id { get; set; }
/// <summary>
/// 调用接口提交的终端设备号
/// </summary>
public string device_info { get; set; }
/// <summary>
/// 微信返回的随机字符串
/// </summary>
public string nonce_str { get; set; }
/// <summary>
/// 微信返回的签名
/// </summary>
public string sign { get; set; }
/// <summary>
/// SUCCESS/FAIL
/// </summary>
public string result_code { get; set; }
/// <summary>
/// 详细参见第6节错误列表
/// </summary>
public string err_code { get; set; }
/// <summary>
/// 错误返回的信息描述
/// </summary>
public string err_code_des { get; set; }
//--------------以下字段在return_code 和result_code都为SUCCESS的时候有返回
/// <summary>
/// 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP,详细说明见参数规定
/// https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_2
/// </summary>
public string trade_type { get; set; }
/// <summary>
/// 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时
/// </summary>
public string prepay_id { get; set; }
/// <summary>
/// 根据返回的XML创建WeXinPayAPIResponseEntity
/// </summary>
/// <param name="orderInfoXmlStr">订单XML</param>
/// <returns></returns>
public static WeXinPayAPIResponseEntity Create(string orderInfoXmlStr)
{
WeXinPayAPIResponseEntity entity = null;
if (string.IsNullOrEmpty(orderInfoXmlStr) == false)
{
//加载返回的XML
XmlDocument doc = new XmlDocument();
doc.LoadXml(orderInfoXmlStr);
XmlElement root = doc.DocumentElement;
//获取WeXinPayAPIResponseEntity所有的属性
var props = typeof(WeXinPayAPIResponseEntity).GetProperties();
var propDict = new Dictionary<string, System.Reflection.PropertyInfo>();
foreach (var pop in props)
{
propDict.Add(pop.Name, pop);
}
entity = new WeXinPayAPIResponseEntity();
//遍历设置WeXinPayAPIResponseEntity的值
foreach (XmlElement cNode in root.ChildNodes)
{
string nodeName = cNode.Name;
if (propDict.ContainsKey(nodeName) == true)
{
propDict[nodeName].SetValue(entity, cNode.InnerText, null);
}
}
}
return entity;
}
}
}
using System;
using Smobiler.Core;
namespace Smobiler.Tutorials.Plugins
{
partial class demoWeixinPay : Smobiler.Core.Controls.MobileForm
{
#region "SmobilerForm generated code "
//SmobilerForm overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
//NOTE: The following procedure is required by the SmobilerForm
//It can be modified using the SmobilerForm.
//Do not modify it using the code editor.
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.title1 = new Smobiler.Core.Controls.Title();
this.panel1 = new Smobiler.Core.Controls.Panel();
this.btnWXPay = new Smobiler.Core.Controls.Button();
this.btnWXLogin = new Smobiler.Core.Controls.Button();
this.btnOutOrder = new Smobiler.Core.Controls.Button();
this.labOutOrder = new Smobiler.Core.Controls.Label();
this.weiXin1 = new Smobiler.Plugins.WeiXin();
//
// title1
//
this.title1.ImageType = Smobiler.Core.Controls.ImageEx.ImageStyle.FontIcon;
this.title1.Name = "title1";
this.title1.ResourceID = "angle-left";
this.title1.Size = new System.Drawing.Size(300, 30);
this.title1.Text = "WeiXin";
this.title1.ImagePress += new System.EventHandler(this.title1_ImagePress);
//
// panel1
//
this.panel1.Controls.AddRange(new Smobiler.Core.Controls.MobileControl[] {
this.btnWXPay,
this.btnWXLogin,
this.btnOutOrder,
this.labOutOrder});
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 30);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(300, 477);
//
// btnWXPay
//
this.btnWXPay.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(192)))), ((int)(((byte)(80)))));
this.btnWXPay.Location = new System.Drawing.Point(8, 48);
this.btnWXPay.Name = "btnWXPay";
this.btnWXPay.Size = new System.Drawing.Size(100, 42);
this.btnWXPay.Text = "支付1分钱";
this.btnWXPay.Press += new System.EventHandler(this.btnWXPay_Press);
//
// btnWXLogin
//
this.btnWXLogin.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(67)))), ((int)(((byte)(80)))));
this.btnWXLogin.Location = new System.Drawing.Point(183, 48);
this.btnWXLogin.Name = "btnWXLogin";
this.btnWXLogin.Size = new System.Drawing.Size(106, 42);
this.btnWXLogin.Text = "微信登陆";
this.btnWXLogin.Press += new System.EventHandler(this.btnWXLogin_Press);
//
// btnOutOrder
//
this.btnOutOrder.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(185)))), ((int)(((byte)(229)))), ((int)(((byte)(80)))));
this.btnOutOrder.Location = new System.Drawing.Point(200, 1);
this.btnOutOrder.Name = "btnOutOrder";
this.btnOutOrder.Size = new System.Drawing.Size(100, 35);
this.btnOutOrder.Text = "生成外部订单号";
this.btnOutOrder.Press += new System.EventHandler(this.btnOutOrder_Press);
//
// labOutOrder
//
this.labOutOrder.Location = new System.Drawing.Point(0, 1);
this.labOutOrder.Name = "labOutOrder";
this.labOutOrder.Size = new System.Drawing.Size(200, 35);
//
// weiXin1
//
this.weiXin1.Name = "weiXin1";
//
// demoWeixinPay
//
this.Components.AddRange(new Smobiler.Core.Controls.MobileComponent[] {
this.weiXin1});
this.Controls.AddRange(new Smobiler.Core.Controls.MobileControl[] {
this.title1,
this.panel1});
this.Load += new System.EventHandler(this.demoWeixinPay_Load);
this.Name = "demoWeixinPay";
}
#endregion
private Core.Controls.Title title1;
private Core.Controls.Panel panel1;
private Core.Controls.Button btnWXPay;
private Core.Controls.Button btnWXLogin;
private Core.Controls.Button btnOutOrder;
private Core.Controls.Label labOutOrder;
private Smobiler.Plugins.WeiXin weiXin1;
}
}