需要的Dll AopSdk.dll 在官方SDK中下载。
private static string APPID = "2017060507xxxxx"; //应用ID private static string ALI_Public_key = "xxxxxxxxx";//支付宝公钥 private static string APP_Private_key = "xxxxxxxx”;//应用私钥(应用私钥注意用支付宝工具 secret_key_tools_RSA_win 转换成非JAVA语言版本)
///1.在服务端签名 public class CreatePayOrderInput { public string Body { set; get; } public string Subject { set; get; } public string TotalAmount { set; get; } public string OutTradeNo { set; get; } } /// <summary> /// 获取支付宝签名接口 /// </summary> /// <param name="input"></param> /// <returns></returns> [HttpPost] public ReturnNode CreatePayOrder(CreatePayOrderInput input) { IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_Private_key, "json", "1.0", "RSA2", ALI_Public_key, "UTF-8", false); //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如:alipay.trade.app.pay AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); model.Body = input.Body;//"我是测试数据"; model.Subject = input.Subject;//"1"; model.TotalAmount = input.TotalAmount;//"0.01"; model.OutTradeNo = input.OutTradeNo;//"20170704125896"; model.ProductCode = "QUICK_MSECURITY_PAY"; model.TimeoutExpress = "30m"; request.SetBizModel(model); request.SetNotifyUrl("http://xxxxxx/shopapi/controllers/PayAliNotify.aspx");//回调地址 //这里和普通的接口调用不同,使用的是sdkExecute AlipayTradeAppPayResponse response = client.SdkExecute(request); //页面输出的response.Body就是orderString 可以直接给客户端请求,无需再做处理 return ReturnNode.ReturnSuccess(response.Body); }
/2.支付异步通知服务器方法,可以用aspx页面做地址。 using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace YFAPICommon.Controllers { public partial class PayAliNotify : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { Dictionary<string, string> param = GetRequestPost(); NotifyFunc(param); } catch (Exception ex) { } } /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组 /// request回来的信息组成的数组 public Dictionary<string, string> GetRequestPost() { int i = 0; Dictionary<string, string> sArray = new Dictionary<string, string>(); NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = Request.Form; // Get names of all forms into a string array. String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], Request.Form[requestItem[i]]); } return sArray; } } }
///3.回调签名验证 public class AliNotifyInput { public string total_amount { set; get; }// = 2.00 public string buyer_id { set; get; }// = 2088102116773037& public string body { set; get; }// = 大乐透2.1& public string trade_no { set; get; }//= 2016071921001003030200089909& public string refund_fee { set; get; }//= 0.00& public string notify_time { set; get; }//= 2016-07-19 14:10:49& public string subject { set; get; }//= 大乐透2.1& public string sign_type { set; get; }//= RSA2& public string charset { set; get; }//= utf-8& public string notify_type { set; get; }//= trade_status_sync& public string out_trade_no { set; get; }//= 0719141034-6418& public string gmt_close { set; get; }//= 2016-07-19 14:10:46& public string gmt_payment { set; get; }//= 2016-07-19 14:10:47& public string trade_status { set; get; }//= TRADE_SUCCESS& public string version { set; get; }//= 1.0& public string sign { set; get; } public string gmt_create { set; get; }//= 2016-07-19 14:10:44& public string app_id { set; get; }//= 2015102700040153& public string seller_id { set; get; }//= 2088102119685838& public string notify_id { set; get; }//= 4a91b7a78a503640467525113fb7d8bg8e } public void NotifyFunc(Dictionary<string, string> param) { //校验签名 bool flag = AlipaySignature.RSACheckV1(param, ALI_Public_key, "UTF-8", "RSA2", false); if (flag) { string json = JsonConvert.SerializeObject(param); AliNotifyInput input = JsonConvert.DeserializeObject<AliNotifyInput>(json); //校验APPID if (input.app_id == APPID) { //校验支付状态 if (input.trade_status == "TRADE_SUCCESS" || input.trade_status == "TRADE_FINISHED") { int totalAmount = (int)(double.Parse(input.total_amount) * 100); OrderController controller = new OrderController(); controller.PayOrder(input.out_trade_no, totalAmount, "Ali"); } } } }