NET-C#开发HBuillder的APP ,QQ群 :661996236
最近由于连续太多人找我咨询NET_C#开发支付功能,请求协助和demo,所以本人在此简单分享一下,其实在本社区内,已有一部分代码了。
链接:C#配置支付宝信息
APP前端开发调用代码示例,由于是项目部分代码,请自行完善APP前端支付代码,返回测试数据,只是演示,数据自行测试输出,由于
备注 : 获取支付通道
APP端调用代码说明
//定义支付通道
var channel = null;
var channelali = null;
var channelwx = null;
//获取支付通道的方法
function getChannels() {
plus.payment.getChannels(function (channels) {
channelali = channels[0];
channelwx = channels[1];
// for (var i = 0; i < channels.length; i++) {
//
// //if(mr=="支付宝")
// //{
// if (channels[i].id == "alipay")
// { //wxpay //alipay
// channelali = channels[i];
// }
// //}
// //if(mr=="微信")
// //{
// if (channels[i].id == "wxpay")
// { //wxpay //alipay
// channelwx = channels[i];
// }
// // }
// }
// plus.ui.toast("使用支付方式:" + channel.id);
}, function (e) {
plus.ui.toast("获取支付通道失败!");
console.log("获取支付通道失败!");
})
}
//支付事件
mui('.gouwuche').on('tap', '#waitpay', function (event) {
channel = null;
var payv = g("pay").value;
console.log(payv);
if (payv == '2') {
urlpay = "OrderPay/GetWxPayInfo";
console.log(urlpay);
} else {
urlpay = "OrderPay/PayMent";
console.log(urlpay);
}
//获取支付通道
// getChannels( );
// console.log(channel.id);
//apiurl + "AliPay/testsign" OrderPay/PayMent Weixin/GetPayInfo
postwait(apiurl + urlpay, returnJson({
orderid: order_no,
type: '3'
}), function (data) {
//console.log(data);
var err = JSON.parse(data).err;
if (err == 0) {
var appdata = JSON.parse(data).data;
//console.log(m);
getChannels();
var pay = g("pay").value;
console.log(appdata.msg);
if (pay == 1) //channel.id== 'alipay')
{
appdata = appdata.msg; //JSON.parse(appdata).msg; //支付宝
channel = channelali;
console.log(channelali.id);
} else {
channel = channelwx;
console.log(channelwx.id);
// console.log("==微信信息=="+ JSON.stringify(appdata));
// appdata = JSON.stringify(appdata); //微信
}
// console.log("==支付信息=="+ appdata);
//appdata='{"appid":"wx123456789,"noncestr":"QtyOdJUspxLGOG5c","package":"Sign=WXPay","partnerid":"12345678","prepayid":"wx20179876543234","timestamp":1496823681,"sign":"DD2D77CE3A770C497A03B3BCEABEF12C"}';
plus.payment.request(channel, appdata, function (result) {
// 弹出系统提示对话框
//mui.toast("支付成功!");
plus.nativeUI.alert( "支付成功!",
function()
{
//修改订单状态
postwait(apiurl + "OrderPay/UpdateOrderStatus", returnJson({
orderid: order_no,
type: 3
}), function (data) {
console.log("修改:"+data);
//openwindows("../../index.html","index.html");
//alert('22');
//alert("end");
var obj = plus.webview.getWebviewById("shopsDetails");
if(obj!=null){ obj.reload(); }
plus.webview.getLaunchWebview().evalJS("orderlist();");
plus.webview.getLaunchWebview().show();
}
);
}, "测试支付", "确认" );
}, function (error) {
if (error.code == 62001) {
mui.toast("您放弃支付.");
// mui.back();
} else {
//alert(error.message);
mui.toast("请前往订单管理尝试支付.");
// plus.nativeUI.alert("支付失败", null, "请前往订单管理尝试支付,支付失败:"+error.code );
}
var obj = plus.webview.getWebviewById("shopsDetails");
if(obj!=null){ obj.reload(); }
plus.webview.getLaunchWebview().evalJS("orderlist();");
plus.webview.getLaunchWebview().show();
});
//
} else {
var msg = JSON.parse(data).msg; //JSON.parse(JSON.parse(data).data).error; //JSON.parse(data).msg;
mui.toast(msg);
}
}
);
//跳转
var obj = plus.webview.getWebviewById("middle_main.html");
if(obj!=null)
obj.reload();
//plus.webview.currentWebview().close();
openwindows("/pages/home/middle_main.html", "middle_main.html", { address_id: 1 });
});
NET-C#支付宝代码验证以及生成支付字符串
支付宝支付
支付宝参考:
链接:C#配置支付宝信息
本文使用C# WebAPI方式
/// <summary>
/// 获取支付信息
/// </summary>
/// <param name="_amount"></param>
/// <returns></returns>
[HttpPost, Route("Alipay/GetPayInfo")]
public string GetPayInfo(string orderid)//_amount:付款金额
{
string strJson = string.Empty;
try
{
//string _amount = "0.01";
string orderInfo = GetOrderInfoWithOutEncode(orderid);
// 对订单做RSA 签名
string sign = RSAFromPkcs8.sign(orderInfo, Config.privtekey, "utf-8");
//仅需对sign做URL编码
sign = HttpUtility.UrlEncode(sign, Encoding.UTF8);
string payInfo = GetOrderInfoWithEncode() + "&sign=" + sign;
strJson = payInfo.Replace("+", "%20");//日期那里会有一个空格(2017-01-05 11:11:11)转化为+,所以这里要替换一下
DTcms.Common.FileOperate.WriteLogInfo("", "支付宝串:" + strJson);
}
catch (Exception ex)
{
DTcms.Common.FileOperate.WriteLogInfo("", "GetPayInfo Exception:" + ex.Message);
//FileLog.WriteLog(ex.ToString());
}
return strJson;
}
/// <summary>
/// 不包含Encode的字符串拼接
/// </summary>
/// <param name="price"></param>
/// <returns></returns>
public string GetOrderInfoWithOutEncode(string orderid)
{
// DTcms.Common.FileOperate.WriteLogInfo("", "支付宝串:" + strJson);
PayInfo.Add("app_id", Config.app_id);
PayInfo.Add("biz_content", GetBizContent(orderid));
PayInfo.Add("charset", "utf-8");
PayInfo.Add("format", "json");
PayInfo.Add("method", "alipay.trade.app.pay");
PayInfo.Add("notify_url", "http://www.baidu.com/OrderPay/PayUpdateStatus");
PayInfo.Add("sign_type", "RSA");
PayInfo.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
PayInfo.Add("version", "1.0");
string strUrl = BuildQueryWithOutEncode(PayInfo);
return strUrl;
}
/// <summary>
/// 获取支付内容详情
/// </summary>
/// <param name="total_amount"></param>
/// <returns></returns>
public string GetBizContent(string orderid)
{
string strSql = string.Format(@"select dt_shop_orders.id,dt_shop_orders.order_no,dt_shop_orders.order_amount,dt_shop_order_goods.goods_title from dt_shop_orders
left join dt_shop_order_goods on dt_shop_orders.id = dt_shop_order_goods.order_id where dt_shop_orders.id = {0}", orderid);
DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
Dictionary<string, string> biz_content_info = new Dictionary<string, string>();
biz_content_info.Add("timeout_express", "30m");//该笔订单允许的最晚付款时间,逾期将关闭交易。
biz_content_info.Add("seller_id", "");//收款支付宝用户ID。 如果该值为空,则默认为商户签约账号对应的支付宝用户ID
biz_content_info.Add("product_code", "QUICK_MSECURITY_PAY");//销售产品码,商家和支付宝签约的产品码,为固定值QUICK_MSECURITY_PAY
biz_content_info.Add("total_amount", dt.Rows[0]["order_amount"].ToString());//订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
biz_content_info.Add("subject", dt.Rows[0]["goods_title"].ToString() + "……");//商品的标题/交易标题/订单标题/订单关键字等。
biz_content_info.Add("body", dt.Rows[0]["order_no"].ToString());//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
biz_content_info.Add("out_trade_no", orderid);//商户网站唯一订单号
string strBizContent = JsonConvert.SerializeObject(biz_content_info, timejson);
return strBizContent;
}
/// <summary>
/// 组装普通文本请求参数(不带Encode)。
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
public static string BuildQueryWithOutEncode(IDictionary<string, string> parameters)
{
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("=");
string encodedValue = value;
postData.Append(encodedValue);
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// 配置(请自行填上下面两个参数) 注释的部分代码不需要,只需要配置没有注释的4个基础参数
/// </summary>
public class Config
{
/// <summary>
/// 应用APPID
/// </summary>
public const string app_id = "自行填写";
/// <summary>
/// 私钥,通过工具生成 //这个就是生成器里面的那个私钥,第一个大框框那里的.
/// </summary>
/// rsa_private_key.pem 用于官方SDK获取支付宝支付信息
public const string privtekeyNocs8 = "自行填写";
//rsa_private_key_pkcs8.pem 用于获取支付宝支付信息
public const string privtekey = "自行填写";
//string APPID = "2017052207309344";
///// 私钥,通过工具生成 //这个就是生成器里面的那个私钥,第一个大框框那里的.
//rsa_private_key.pem 密钥
//string APP_PRIVATE_KEY = "自行填写";
//rsa_private_key_pkcs8.pem pkcs8密钥
//string APP_PRIVATE_KEY = "自行填写";
//配置支付宝的公钥 用于异步通知
public const string ALIPAY_PUBLIC_KEY = "自行填写";
//string CHARSET = "utf-8";
}
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Common.RSAFromPkcs
{
/// <summary>
/// 类名:RSAFromPkcs8
/// 功能:RSA解密、签名、验签
/// 详细:该类对Java生成的密钥进行解密和签名以及验签专用类,不需要修改
/// 版本:2.0
/// 修改日期:2011-05-10
/// 说明:
/// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
/// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
/// </summary>
public sealed class RSAFromPkcs8
{
/// <summary>
/// 签名
/// </summary>
/// <param name="content">需要签名的内容</param>
/// <param name="privateKey">私钥</param>
/// <param name="input_charset">编码格式</param>
/// <returns></returns>
public static string sign(string content, string privateKey, string input_charset)
{
Encoding code = Encoding.GetEncoding(input_charset);
byte[] Data = code.GetBytes(content);
RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);
SHA1 sh = new SHA1CryptoServiceProvider();
byte[] signData = rsa.SignData(Data, sh);
return Convert.ToBase64String(signData);
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="content">需要验证的内容</param>
/// <param name="signedString">签名结果</param>
/// <param name="publicKey">公钥</param>
/// <param name="input_charset">编码格式</param>
/// <returns></returns>
public static bool verify(string content, string signedString, string publicKey, string input_charset)
{
bool result = false;
Encoding code = Encoding.GetEncoding(input_charset);
byte[] Data = code.GetBytes(content);
byte[] data = Convert.FromBase64String(signedString);
RSAParameters paraPub = ConvertFromPublicKey(publicKey);
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
rsaPub.ImportParameters(paraPub);
SHA1 sh = new SHA1CryptoServiceProvider();
result = rsaPub.VerifyData(Data, sh, data);
return result;
}
/// <summary>
/// 用RSA解密
/// </summary>
/// <param name="resData">待解密字符串</param>
/// <param name="privateKey">私钥</param>
/// <param name="input_charset">编码格式</param>
/// <returns>解密结果</returns>
public static string decryptData(string resData, string privateKey, string input_charset)
{
byte[] DataToDecrypt = Convert.FromBase64String(resData);
List<byte> result = new List<byte>();
for (int j = 0; j < DataToDecrypt.Length / 128; j++)
{
byte[] buf = new byte[128];
for (int i = 0; i < 128; i++)
{
buf[i] = DataToDecrypt[i + 128 * j];
}
result.AddRange(decrypt(buf, privateKey, input_charset));
}
byte[] source = result.ToArray();
char[] asciiChars = new char[Encoding.GetEncoding(input_charset).GetCharCount(source, 0, source.Length)];
Encoding.GetEncoding(input_charset).GetChars(source, 0, source.Length, asciiChars, 0);
return new string(asciiChars);
}
private static byte[] decrypt(byte[] data, string privateKey, string input_charset)
{
RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);
SHA1 sh = new SHA1CryptoServiceProvider();
return rsa.Decrypt(data, false);
}
/// <summary>
/// 解析java生成的pem文件私钥
/// </summary>
/// <param name="pemstr"></param>
/// <returns></returns>
private static RSACryptoServiceProvider DecodePemPrivateKey(String pemstr)
{
byte[] pkcs8privatekey;
pkcs8privatekey = Convert.FromBase64String(pemstr);
if (pkcs8privatekey != null)
{
RSACryptoServiceProvider rsa = DecodePrivateKeyInfo(pkcs8privatekey);
return rsa;
}
else
return null;
}
private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8)
{
byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
byte[] seq = new byte[15];
MemoryStream mem = new MemoryStream(pkcs8);
int lenstream = (int)mem.Length;
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
bt = binr.ReadByte();
if (bt != 0x02)
return null;
twobytes = binr.ReadUInt16();
if (twobytes != 0x0001)
return null;
seq = binr.ReadBytes(15); //read the Sequence OID
if (!CompareBytearrays(seq, SeqOID)) //make sure Sequence for OID is correct
return null;
bt = binr.ReadByte();
if (bt != 0x04) //expect an Octet string
return null;
bt = binr.ReadByte(); //read next byte, or next 2 bytes is 0x81 or 0x82; otherwise bt is the byte count
if (bt == 0x81)
binr.ReadByte();
else
if (bt == 0x82)
binr.ReadUInt16();
//------ at this stage, the remaining sequence should be the RSA private key
byte[] rsaprivkey = binr.ReadBytes((int)(lenstream - mem.Position));
RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey);
return rsacsp;
}
catch (Exception)
{
return null;
}
finally { binr.Close(); }
}
private static bool CompareBytearrays(byte[] a, byte[] b)
{
if (a.Length != b.Length)
return false;
int i = 0;
foreach (byte c in a)
{
if (c != b[i])
return false;
i++;
}
return true;
}
private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
// --------- Set up stream to decode the asn.1 encoded RSA private key ------
MemoryStream mem = new MemoryStream(privkey);
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
int elems = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
return null;
bt = binr.ReadByte();
if (bt != 0x00)
return null;
//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus = MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSA.ImportParameters(RSAparams);
return RSA;
}
catch (Exception)
{
return null;
}
finally { binr.Close(); }
}
private static int GetIntegerSize(BinaryReader binr)
{
byte bt = 0;
byte lowbyte = 0x00;
byte highbyte = 0x00;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02) //expect integer
return 0;
bt = binr.ReadByte();
if (bt == 0x81)
count = binr.ReadByte(); // data size in next byte
else
if (bt == 0x82)
{
highbyte = binr.ReadByte(); // data size in next 2 bytes
lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt; // we already have the data size
}
while (binr.ReadByte() == 0x00)
{ //remove high order zeros in data
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte
return count;
}
#region 解析.net 生成的Pem
private static RSAParameters ConvertFromPublicKey(string pemFileConent)
{
byte[] keyData = Convert.FromBase64String(pemFileConent);
if (keyData.Length < 162)
{
throw new ArgumentException("pem file content is incorrect.");
}
byte[] pemModulus = new byte[128];
byte[] pemPublicExponent = new byte[3];
Array.Copy(keyData, 29, pemModulus, 0, 128);
Array.Copy(keyData, 159, pemPublicExponent, 0, 3);
RSAParameters para = new RSAParameters();
para.Modulus = pemModulus;
para.Exponent = pemPublicExponent;
return para;
}
private static RSAParameters ConvertFromPrivateKey(string pemFileConent)
{
byte[] keyData = Convert.FromBase64String(pemFileConent);
if (keyData.Length < 609)
{
throw new ArgumentException("pem file content is incorrect.");
}
int index = 11;
byte[] pemModulus = new byte[128];
Array.Copy(keyData, index, pemModulus, 0, 128);
index += 128;
index += 2;//141
byte[] pemPublicExponent = new byte[3];
Array.Copy(keyData, index, pemPublicExponent, 0, 3);
index += 3;
index += 4;//148
byte[] pemPrivateExponent = new byte[128];
Array.Copy(keyData, index, pemPrivateExponent, 0, 128);
index += 128;
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//279
byte[] pemPrime1 = new byte[64];
Array.Copy(keyData, index, pemPrime1, 0, 64);
index += 64;
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346
byte[] pemPrime2 = new byte[64];
Array.Copy(keyData, index, pemPrime2, 0, 64);
index += 64;
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413
byte[] pemExponent1 = new byte[64];
Array.Copy(keyData, index, pemExponent1, 0, 64);
index += 64;
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480
byte[] pemExponent2 = new byte[64];
Array.Copy(keyData, index, pemExponent2, 0, 64);
index += 64;
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546
byte[] pemCoefficient = new byte[64];
Array.Copy(keyData, index, pemCoefficient, 0, 64);
RSAParameters para = new RSAParameters();
para.Modulus = pemModulus;
para.Exponent = pemPublicExponent;
para.D = pemPrivateExponent;
para.P = pemPrime1;
para.Q = pemPrime2;
para.DP = pemExponent1;
para.DQ = pemExponent2;
para.InverseQ = pemCoefficient;
return para;
}
#endregion
}
}
微信支付
#region 微信支付
[HttpPost, Route("OrderPay/GetWxPayInfo")]
public string GetWxPayInfo(dynamic obj)
{
string strJson = string.Empty;
try
{
string orderid = Convert.ToString(obj.orderid);
Model.dt_shop_orders orderEntity = new BLL.dt_shop_orders().GetModel(int.Parse(orderid));
if (orderEntity != null && orderEntity.id > 0)
{
decimal amount = orderEntity.order_amount;
string horderId = orderEntity.id.ToString();
string trade_no = orderEntity.order_no.ToString();
string hopenid = "";
string parameters = WxPayDataGet(amount, horderId, trade_no, hopenid, horderId);
return new Common.DotNetJson.JSONAPI().ToJson(parameters, "获取微信支付信息成功!", 0);
//JsonConvert.SerializeObject("{\"msg\":\"" + parameters + "\",\"type\":\"wxpay\"}"), "获取微信支付信息成功!", 0);
}
else
{
return new Common.DotNetJson.JSONAPI().ToJson(JsonConvert.SerializeObject("{\"error\":\"当前订单已失效!\"}"), "获取支付信息失败!", 1);
}
}
catch (Exception ex)
{
FileOperate.WriteLogInfo("", "GetPayInfo Exception:" + ex.Message);
return new Common.DotNetJson.JSONAPI().ToJson("{\"error\":\"" + ex.Message + "\"}", "获取微信支付信息失败!", 1);
}
}
/// <summary>
/// 微信支付:生成请求数据
/// </summary>
/// <param name="openid">微信用户id</openid>
/// <param name="ttFee">商品总价格</param>
/// <param name="busiBody"></param>
/// <returns></returns>
protected string WxPayDataGet(decimal ttFee, string busiBody, string out_trade_no, string openid, string otid)
{
//BLL.wx_payment_wxpay wxPayBll = new BLL.wx_payment_wxpay();
//Model.wx_payment_wxpay paymentInfo = wxPayBll.GetModelByWid(wid);
//先设置基本信息
//string partnerId = ""; // paymentInfo.partnerId;// " ";//
//string appId = ""; // paymentInfo.appId;// " ";//
//string partnerKey = ""; // paymentInfo.partnerKey;// " ";//
////paysignkey(非appkey)
//string appKey = ""; // paymentInfo.paySignKey;
//--
JsApiPay jsApiPay = new JsApiPay();
jsApiPay.openid = openid;
jsApiPay.total_fee = int.Parse(((int)(ttFee * 100)).ToString());
string attch = 3 + "|" + otid + "|我爱我县";
WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(openid, otid, attch, "woaiwoxian");
WxPayData jsApiParam = new WxPayData();
string time = WxPayApi.GenerateTimeStamp();
string nonceStr = WxPayApi.GenerateNonceStr();
string pack = "prepay_id=" + unifiedOrderResult.GetValue("prepay_id");
//**************************************************封装调起微信客户端支付界面字符串********************
//设置待加密支付参数并加密
string appid = unifiedOrderResult.GetValue("appid").ToString();
string mch_Id = unifiedOrderResult.GetValue("mch_id").ToString();
string prepay_id = unifiedOrderResult.GetValue("prepay_id").ToString();
jsApiParam.SetValue("appId", appid); //
jsApiParam.SetValue("partnerid", mch_Id);
jsApiParam.SetValue("prepayid", prepay_id);
jsApiParam.SetValue("package", "Sign=WXPay");
jsApiParam.SetValue("nonceStr", nonceStr);
jsApiParam.SetValue("timeStamp", time);
string sign = jsApiParam.MakeSign();
// jsApiParam.SetValue("sign", sign);
//设置支付包参数
WxPayData paydata = new WxPayData();
paydata.SetValue("retcode", 0);//5+固定调起参数
paydata.SetValue("retmsg", "ok");//5+固定调起参数
paydata.SetValue("appid", appid);//AppId,微信开放平台新建应用时产生
paydata.SetValue("partnerid", mch_Id);//商户编号,微信开放平台申请微信支付时产生
paydata.SetValue("prepayid", prepay_id);//由上面获取预支付流程获取
paydata.SetValue("package", "Sign=WXpay");//APP支付固定设置参数
paydata.SetValue("noncestr", nonceStr);//随机字符串,
paydata.SetValue("timestamp", time);//时间戳
Hashtable paySignReqHandler = new Hashtable();
paySignReqHandler.Add("appid", appid);
paySignReqHandler.Add("partnerid", mch_Id);
paySignReqHandler.Add("prepayid", prepay_id);
paySignReqHandler.Add("noncestr", nonceStr);
paySignReqHandler.Add("package", "Sign=WXpay");
paySignReqHandler.Add("timestamp", time.ToString());
var paySign = CreateMd5Sign(paySignReqHandler);
paydata.SetValue("sign", paySign);//时间戳
//var serializer = new JavaScriptSerializer();
//var result = serializer.Serialize(obj);
//paySign = HttpUtility.UrlEncode(paySign, Encoding.UTF8);
//string payInfo = GetOrderInfoWithEncode() + "&sign=" + sign;
//strJson = payInfo.Replace("+", "%20");//日期那里会有一个空格(2017-01-05 11:11:11)转化为+,所以这里要替换一下
// FileOperate.WriteLogInfo("", "支付宝串:" + strJson);
string result = paydata.ToJson();
// FileOperate.WriteLogInfo("", "result:" + result);
return result;
}
private string CreateMd5Sign(Hashtable parameters)
{
var sb = new StringBuilder();
var akeys = new ArrayList(parameters.Keys);
akeys.Sort();//排序,这是微信要求的
foreach (string k in akeys)
{
var v = (string)parameters[k];
sb.Append(k + "=" + v + "&");
}
sb.Append("key=" + WxPayAPI.WxPayConfig.KEY);
string sign = GetMD5(sb.ToString());
return sign;
}
private string GetMD5(string src)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.UTF8.GetBytes(src);
byte[] md5data = md5.ComputeHash(data);
md5.Clear();
var retStr = BitConverter.ToString(md5data);
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}
#endregion
//=========================================================================
//微信支付demo里面的部分代码 JsApiPay ,其他的代码基本在微信sdk demo里面基本完成,细节部分自己待处理就好了
/**
* 调用统一下单,获得下单结果
* @return 统一下单结果
* @失败时抛异常WxPayException
*/
public WxPayData GetUnifiedOrderResult(string hasopenid, string body, string attach, string tag)
{
string strSql = string.Format(@"select dt_shop_orders.id,dt_shop_orders.order_no,dt_shop_orders.order_amount,dt_shop_order_goods.goods_title from dt_shop_orders
left join dt_shop_order_goods on dt_shop_orders.id = dt_shop_order_goods.order_id where dt_shop_orders.id = {0}", body);
DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
//统一下单
WxPayData data = new WxPayData();
data.SetValue("body", dt.Rows[0]["goods_title"].ToString() + "...");
data.SetValue("attach", attach);
data.SetValue("out_trade_no", body);
data.SetValue("total_fee", total_fee);
data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
data.SetValue("goods_tag", tag);
data.SetValue("trade_type", "APP");
data.SetValue("openid", hasopenid);
//异步通知url未设置,则使用配置文件中的url
data.SetValue("notify_url", "http://异步通知url/OrderPay/WXUpdateStatus");//异步通知url OrderPay/WXUpdateStatus
data.SetValue("appid", WxPayConfig.APPID);//公众账号ID
data.SetValue("mch_id", WxPayConfig.MCHID);//商户号
data.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip
data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串
//签名
data.SetValue("sign", data.MakeSign());
WxPayData result = WxPayApi.UnifiedOrder(data);
if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
{
Log.Error(this.GetType().ToString(), "UnifiedOrder response error!");
throw new WxPayException("UnifiedOrder response error!");
}
unifiedOrderResult = result;
return result;
}
支付宝
Android&iOS
错误码 错误描述
8000 正在处理中,支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态
4000 订单支付失败
5000 重复请求
6001 用户中途取消
6002 网络连接出错
6004 支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态
微信支付
Android&iOS
错误码 错误描述
-1 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
-2 无需处理。发生场景:用户不支付了,点击取消,返回APP。
-3 发送失败
-4 授权失败
-5 微信不支持
-
最后说明: 支付宝支付签名是 rsa_private_key_pkcs8 这个方式,千万不搞错了,本文使用是RSA,不是RSA2,然后支付宝 公钥 私钥不要搞错了 ,支付宝支付支持在线调试,微信不支持,需要打包安装才能测试支付功能,
微信支付还有一个点,密钥需要一致,不能错误!
由于时间匆忙,贴了一些代码,需要各位同学自行完善和修改,如有错误,欢迎指出我来及时修改完善,其他问题, 欢迎加群来咨询交流学习!谢谢大家
6 个评论
要回复文章请先登录或注册
太2真人
太2真人
阿鱼
l***@qq.com
w***@qq.com
1***@qq.com