C#支付宝支付接口H5版(手机网页支付)
接口官方文档 https://docs.open.alipay.com/203/107090/
首先在Nuget 安装 Alipay
////// 支付宝支付 /// /// /// /// 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("")); return form; }
////// 支付宝异步回调专用(修改请参考相关文档) /// /// 通知时间 /// 开发者的app_id /// 编码格式 /// 接口版本 /// 签名类型 /// 签名 /// 支付宝交易号 /// 商户订单号 /// 买家支付宝用户号 /// 买家支付宝账号 /// 卖家支付宝用户号 /// 卖家支付宝账号 /// 交易状态 /// 订单金额 /// 实收金额 /// 开票金额 /// 付款金额 /// 订单标题 /// 商品描述 ///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); } }