支付宝官方文档:互联网平台直付通产品介绍 | 网页&移动应用
支付宝客服(聊天窗口相比下比较大):https://opensupport.alipay.com/support/home.htm
1.先下载官方SDK(概述 | 网页&移动应用)
2.提前准备
.NET 开发语言必须使用 pkcs1 格式私钥
3.写一个静态类,用来配置基础数据
internal class AliClients
{
public static class Sandbox
{
public static AlipayConfig GetConfig()
{
AlipayConfig config = new AlipayConfig();
config.ServerUrl = AppConfigurtaionHelper.Configuration["AliInfoSettings:Gate_Way_Url"];
config.AppId = AppConfigurtaionHelper.Configuration["AliInfoSettings:SP_APPID"];
config.PrivateKey = AppConfigurtaionHelper.Configuration["AliInfoSettings:PrivateKey_PKCS1"];
//config.AlipayPublicKey = AppConfigurtaionHelper.Configuration["AliInfoSettings:PubilcKey"];
config.AlipayPublicCertPath = AppConfigurtaionHelper.Configuration["AliInfoSettings:Alipay_Cert_Path"];//支付宝公钥证书路径
config.AppCertPath = AppConfigurtaionHelper.Configuration["AliInfoSettings:App_Cert_Path"];//商户应用证书路径
config.RootCertPath = AppConfigurtaionHelper.Configuration["AliInfoSettings:Root_Cert_Path"];//支付宝根证书路径
//config.EncryptKey = "RrAdgyCsnkoE1a9A8cW72w==";
return config;
}
}
}
4. App支付
IAopClient client = new DefaultAopClient(AliClients.Sandbox.GetConfig());
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
request.SetNotifyUrl(systemSettings.Url+ aliInfoSettings.PaymentnotifyUrl);//异步返回地址
Dictionary bizContent = new Dictionary();
bizContent.Add("out_trade_no", tranInfo.TranscationId.ToString()); //商户网站唯一订单号。
bizContent.Add("total_amount", tranInfo.Amount.ToString());
bizContent.Add("subject", model.Subject);//订单标题。
bizContent.Add("body", model.Description);//订单附加信息
bizContent.Add("product_code", "QUICK_MSECURITY_PAY");
///二级商户信息
Dictionary sub = new Dictionary();
sub.Add("merchant_id", merInfo.ShId);//支付宝商户编号
bizContent.Add("sub_merchant", sub);
//结算信信息
Dictionary> sList = new Dictionary>();
List srttle = new List();
settle_detail_infos seModel = new settle_detail_infos();
seModel.amount = model.PayMoney;
seModel.trans_in_type = "defaultSettle";//表示结算到商户进件时设置的默认结算账号
srttle.Add(seModel);
sList.Add("settle_detail_infos", srttle);
bizContent.Add("settle_info", sList);
//补差信息
if (model.Subsidy_Money > 0)
{
Dictionary extend = new Dictionary();
extend.Add("orig_total_amount", model.Subsidy_Money + model.PayMoney);//外部订单金额
bizContent.Add("extend_params", extend);
}
request.BizContent = JsonConvert.SerializeObject(bizContent);
NLogHelper.Info($"参数:{JsonConvert.SerializeObject(request)}" );
var res = client.SdkExecute(request);
NLogHelper.Info($"结果:{JsonConvert.SerializeObject(request)}");
if (!res.IsError)
{
rm.Data = res.Body;
rm.State = 200;
}
else
{
rm.Msg = res.Msg;
return rm;
}
5.支付成功之后接收返回数据
// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
// request回来的信息组成的数组
public Dictionary GetRequestPost()
{
int i = 0;
Dictionary sArray = new Dictionary();
StringBuilder str = new StringBuilder();
var keys = Request.Form.Keys;
if (keys != null)
{
foreach (string key in keys)
{
sArray.Add(key, Request.Form[key]);
str.Append($"{key}={Request.Form[key]}&");
}
NLogHelper.Info( "支付宝支付异步返回结果:"+ str.ToString());
}
return sArray;
}
bool flag = AlipaySignature.RSACertCheckV1(dic, aliInfoSettings.Alipay_Cert_Path, aliInfoSettings.Charset, aliInfoSettings.Sign_Type);
if (flag)
{
//交易状态
NLogHelper.Info($"验签成功 ");
if (dic["trade_status"] == "TRADE_SUCCESS") {
NLogHelper.Info($"支付成功!");
var out_trade_no = dic["out_trade_no"];// 原支付请求的商家订单号
var trade_no = dic["trade_no"];//支付宝交易凭证号
//找到当前订单
//判断该笔订单是否在商户网站中已经做过处理
var tranInfo = EPL_OutTranscationBz.GetModel(Convert.ToInt64( out_trade_no));
if (tranInfo != null && tranInfo.PayStatus == (int)EPL_OutTranscation_PayState.未支付) {
tranInfo.Amount = Convert.ToDecimal( dic["total_amount"]);
tranInfo.OutPayOrderId = dic["trade_no"];//支付宝交易凭证号
var merInfo = EP_MerchantBz.GetModel(tranInfo.MerId);
tranInfo.PayStatus = (int)EPL_OutTranscation_PayState.待结算;
EPL_OutTranscationBz.Update(tranInfo);
var wayMoney = Math.Round(tranInfo.Amount * Convert.ToDecimal(tranInfo.PayWayRate), 2);//四舍五入
var cusMoney = tranInfo.Amount - wayMoney; //实际到商户账户的钱
//判断是否需要分账,如果不需要直接走结算,到商户的支付宝账户上
//需要分账,需要到支付宝账户上冻结,之后分账补差之后解冻
EPL_Alternative alternative = new EPL_Alternative();
alternative.TransactionId = tranInfo.TranscationId;
alternative.AlternativeMerchantId = tranInfo.MerId;
alternative.OriginalMoney = tranInfo.Amount;
alternative.WayMoney = wayMoney;
alternative.HFMoney = tranInfo.YMoney;
alternative.AlternativeMoney = cusMoney;
alternative.PostTime = DateTime.Now;
alternative.PayState = (int)EPL_Alternative_PayState.代付代收初始值;
alternative.Payfunc = tranInfo.Payfunc;
alternative.MerUserId = merInfo.UserId;
alternative.BizBatchNo = 0;
alternative.AlternativeAcceptUserId = 5100031032; //九不欺平台订单支付分账收款账户
if (tranInfo.YMoney <= 0)
{
alternative.AgencyType = (int)EPL_Alternative_AgencyType.代付;
alternative.Remark = "代付给商户";
}
else {
alternative.AgencyType = (int)EPL_Alternative_AgencyType.代收;
alternative.Remark = "支付订单代收成功";
}
alternative.AlternativePayId = EPL_AlternativeBz.Add(alternative);
if (alternative.AlternativePayId > 0)
{
IAopClient client = new DefaultAopClient(AliClients.Sandbox.GetConfig());
AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest();
SettlementModel sModel = new SettlementModel();
sModel.out_request_no = alternative.AlternativePayId.ToString();//确认结算请求流水号
sModel.trade_no = tranInfo.OutPayOrderId;
List dList = new List();
detail_infos detail = new detail_infos();
detail.amount = Convert.ToDouble(cusMoney);
detail.trans_in_type = "defaultSettle";
dList.Add(detail);
sModel.settle_info = new settle_info()
{
settle_detail_infos = dList
};
sModel.extend_params = new extend_params()
{
royalty_freeze = tranInfo.YMoney <= 0? false:true //是否进行资金冻结,用于后续分账,true表示冻结
};
request.BizContent = JsonConvert.SerializeObject(sModel);
NLogHelper.Info($"参数:{JsonConvert.SerializeObject(sModel)}");
var res = client.CertificateExecute(request);
NLogHelper.Info($"结果:{JsonConvert.SerializeObject(res)}");
if (res.IsError)
{
NLogHelper.Error($"失败 {res.SubMsg}");
rm.Msg = res.Msg;
return rm;
}
else
{
}
}
rm.State = 200;
rm.Data = tranInfo;
}
}
}
else
{
rm.Msg = "验证失败";
return rm;
}