接口官方文档 https://docs.open.alipay.com/203/107090/
首先在Nuget 安装 Alipay
/// <summary> /// 支付宝支付 /// </summary> /// <param name="model"></param> /// <param name="configPath"></param> /// <returns></returns> public static string Alipay() { string OrderNumber = "alipay" + DateTime.Now.ToString("yyyyMMddHHmmss"); string app_id = "你的app_id"; string merchant_private_key = "你的应用私钥"; string alipay_public_key = "你的支付宝公钥"; string timeout_express = "30m";//订单有效时间(分钟) string postUrl = "https://openapi.alipay.com/gateway.do"; string sign_type = "RSA2";//加签方式 有两种RSA和RSA2 我这里使用的RSA2(支付宝推荐的) string version = "1.0";//固定值 不用改 string format = "json";//固定值 string Amount = "0.01";//订单金额 string method = "QUICK_WAP_WAY";//调用接口 固定值 不用改 IAopClient client = new DefaultAopClient(postUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, "UTF-8", false); AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.SetNotifyUrl("支付宝后台通知的地址"); request.SetReturnUrl("支付宝前台回跳的地址"); request.BizContent = "{" + " "body":"对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。"," + " "subject":"商品描述"," + " "out_trade_no":"商家唯一订单,填写你项目里生成的唯一订单号"," + " "timeout_express":"" + timeout_express + ""," + " "total_amount":" + Amount + "," + " "product_code":""+ method + """ + " }"; AlipayTradeWapPayResponse response = client.pageExecute(request); string form = response.Body.Substring(0, response.Body.IndexOf("<script>")); return form; }
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" />
<title></title>
<script type="text/javascript">
function submit(){
document.forms['alipaysubmit'].submit();
}
</script>
</head>
<body>
<div>
<button onclick="submit()">支付宝支付</button>
<div id="formDiv" style="display:none;">
<!--form-->
</div>
</div>
</body>
</html>
/// <summary> /// 支付宝异步回调专用(修改请参考相关文档) /// </summary> /// <param name="notify_time">通知时间</param> /// <param name="app_id">开发者的app_id</param> /// <param name="charset">编码格式</param> /// <param name="version">接口版本</param> /// <param name="sign_type">签名类型</param> /// <param name="sign">签名</param> /// <param name="trade_no">支付宝交易号</param> /// <param name="out_trade_no">商户订单号</param> /// <param name="buyer_id">买家支付宝用户号</param> /// <param name="buyer_logon_id">买家支付宝账号</param> /// <param name="seller_id">卖家支付宝用户号</param> /// <param name="seller_email">卖家支付宝账号</param> /// <param name="trade_status">交易状态</param> /// <param name="total_amount">订单金额</param> /// <param name="receipt_amount">实收金额</param> /// <param name="invoice_amount">开票金额</param> /// <param name="buyer_pay_amount">付款金额</param> /// <param name="subject">订单标题</param> /// <param name="body">商品描述</param> /// <returns></returns> public ActionResult AlipayAsynchronousReception(DateTime notify_time, string app_id = "", string charset = "", string version = "", string sign_type = "", string sign = "", string trade_no = "", string out_trade_no = "", string buyer_id = "", string buyer_logon_id = "", string seller_id = "", string seller_email = "", string trade_status = "", double total_amount = 0, double receipt_amount = 0, double invoice_amount = 0, double buyer_pay_amount = 0, string subject = "", string body = "") { var msgStr = $@"AlipayAsynchronousReception: out_trade_no={out_trade_no},trade_no={trade_no},trade_status={trade_status},notify_time={notify_time},DateTimeNow={DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"; new LogEntity().SetLogMessage(msgStr); var model = new PaymentRecordModel(); model.Trade_no = trade_no; model.Out_trade_no = out_trade_no; model.Buyer_login_id = buyer_logon_id; model.Seller_email = seller_email; model.State = (int)Enum.Parse(typeof(PaymentRecordStateEnum), trade_status); var facade = new TrainingFacade(); var result = facade.OpreationPaymentRecord(model); if (result.IsSuccess) { return Json("success", JsonRequestBehavior.AllowGet); } else { return Json("fail", JsonRequestBehavior.AllowGet); } }