1、准备参数
新建一个公共参数配置类NewAlipayconfig (可将参数存放到config配置文件中读取)
public class NewAlipayconfig { public NewAlipayconfig() { // // TODO: 在此处添加构造函数逻辑 // } // 应用ID,您的APPID public static string app_id = "1231213"; // 支付宝网关 public static string gatewayUrl = "https://openapi.alipay.com/gateway.do"; // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 public static string alipay_public_key = "MIIBIjANBgkqhk+ng8kcURzCNMuyOGIUsssQM66m6hNBU6BXqCPcKZDEZVdlXW3it4jqDveq1utRAoYNPj9wwSFiXxmmjA4/7LhlOMxwJ28R+7aB4tDgiYjixcuQIDAQAB"; // 商户私钥,您的原始格式RSA私钥 public static string private_key = "00j7jSssF8+2QdNcIbIhAoGBAPYCd+PLanGuNHMDzumBQ=="; // 签名方式 public static string sign_type = "RSA2"; // 编码格式 public static string charset = "UTF-8"; //同步回调地址 public static string AliPayReturn_url = "同步回调地址:即支付成功之后,跳转的页面"; //异步回调通知地址 public static string AliPayNotify_url = "异步回调通知地址:即支付成功,会主动调用该地址"; }
注意:1、以上参数app_id、gatewayUrl可直接在支付宝商户平台获取
2、通过支付宝RSA签名验签工具生成一对商户公钥和秘钥,商户秘钥保存(即private_key,签名时使用),然后通过商户公钥到支付宝商户平台设置,来获取支付宝公钥
3、建议将商户系统业务状态的修改,通过异步通知地址来更新,以异步通知的地址为准
2、支付宝PC端支付
DefaultAopClient client = new DefaultAopClient(NewAlipayconfig.gatewayUrl, NewAlipayconfig.app_id, NewAlipayconfig.private_key, "json", "1.0", "RSA2", NewAlipayconfig.alipay_public_key, "utf-8", false); // 外部订单号,商户网站订单系统中唯一的订单号 string out_trade_no = 100001; // 订单名称 string subject = "购买商品"; // 付款金额 string total_amout = 0.01;// 商品描述 string body = "支付宝商品支付"; // 组装业务参数model AlipayTradePagePayModel model = new AlipayTradePagePayModel(); model.Body = body; model.Subject = subject; model.TotalAmount = total_amout; model.OutTradeNo = out_trade_no; model.ProductCode = "FAST_INSTANT_TRADE_PAY"; AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 设置同步回调地址 request.SetReturnUrl(NewAlipayconfig.AliPayReturn_url); // 设置异步通知接收地址 request.SetNotifyUrl(NewAlipayconfig.AliPayNotify_url); // 将业务model载入到request request.SetBizModel(model); AlipayTradePagePayResponse response = null; try { response = client.pageExecute(request, null, "post"); Response.Write(response.Body); } catch (Exception exp) { throw exp; }
通过以上代码即可实现PC端支付宝支付功能,建议首次接入支付宝支付功能时,先将支付宝沙箱环境测通(支付宝支付测试环境)
2、移动端支付宝支付
DefaultAopClient clientWap = new DefaultAopClient(NewAlipayconfig.gatewayUrl, NewAlipayconfig.app_id, NewAlipayconfig.private_key, "json", "1.0", NewAlipayconfig.sign_type, NewAlipayconfig.alipay_public_key, NewAlipayconfig.charset, false); // 外部订单号,商户网站订单系统中唯一的订单号 string out_trade_no_wap = 100002; // 订单名称 string subject_wap = "购买商品"; // 付款金额 string total_amout_wap = 0.01;// 商品描述 string body_wap = "支付宝商品支付"; // 支付中途退出返回商户网站地址 string quit_url = "";//如果用户中途退出 则跳转到该页面 // 组装业务参数model AlipayTradeWapPayModel modelWap = new AlipayTradeWapPayModel(); modelWap.Body = body_wap; modelWap.Subject = subject_wap; modelWap.TotalAmount = total_amout_wap; modelWap.OutTradeNo = out_trade_no_wap; modelWap.ProductCode = "QUICK_WAP_WAY"; modelWap.QuitUrl = quit_url; AlipayTradeWapPayRequest requestWap = new AlipayTradeWapPayRequest(); // 设置支付完成同步回调地址 requestWap.SetReturnUrl(NewAlipayconfig.AliPayReturn_url); // 设置支付完成异步通知接收地址 requestWap.SetNotifyUrl(NewAlipayconfig.AliPayNotify_url); // 将业务model载入到request requestWap.SetBizModel(modelWap); AlipayTradeWapPayResponse responseWap = null; try { responseWap = clientWap.pageExecute(requestWap, null, "post"); Response.Write(responseWap.Body); } catch (Exception exp) { throw exp; }
下面提供支付宝.net版sdk和签名生成工具:链接:https://pan.baidu.com/s/1BCW69nfS2uPhqmlThZOvhw 提取码:z404
原文地址:https://www.cnblogs.com/wenghan/p/11951967.html
时间: 2024-10-10 16:50:46