123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Microsoft.AspNetCore.Authorization;
- using System.Web;
- using MySystem.MainModels;
- using LitJson;
- using Library;
- using System.IO;
- using Aop.Api.Util;
- namespace MySystem.Areas.Api.Controllers.v1
- {
- [Area("Api")]
- [Route("Api/[controller]/[action]")]
- public class AlipayController : BaseController
- {
- public AlipayController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
- {
- }
- #region 支付宝服务平台支付回调
- public string Notice()
- {
- StreamReader sr = new StreamReader(Request.Body);
- string requestMes = sr.ReadToEnd();
- function.WriteLog(DateTime.Now.ToString() + "\r\n" + requestMes + "\r\n\r\n", "支付宝服务平台支付回调");
- return PayCallBack(new AlipayFunction(_accessor.HttpContext).AlipayPublicKey, requestMes);
- }
- public string PayCallBack(string publickey, string requestMes, bool IsRedis = false)
- {
-
- var result = "fail";
- var sPara = GetRequestPost(requestMes);
- if (sPara.Count > 0)
- {
-
- bool verifyResult = false;
- var aliNotify = new Com.Alipay.Notify();
- string signtype = sPara["sign_type"];
- var total_fee = 0m;
- string strPrice = sPara["total_amount"];
- decimal.TryParse(strPrice, out total_fee);
- verifyResult = AlipaySignature.RSACheckV1(sPara, publickey, "UTF-8", signtype, false);
-
- if (verifyResult)
- {
-
-
- string out_trade_no = sPara["out_trade_no"];
-
- string trade_no = sPara["trade_no"];
-
- string trade_status = sPara["trade_status"];
-
- if (trade_status == "TRADE_FINISHED" || trade_status == "TRADE_SUCCESS")
- {
- RedisDbconn.Instance.AddList("PayCallBack", "{\"out_trade_no\":\"" + out_trade_no + "\",\"transaction_id\":\"" + trade_no + "\",\"total_fee\":\"" + total_fee + "\",\"pay_mode\":\"1\",\"openid\":\"\",\"attach\":\"\"}");
- result = "success";
- }
- }
- }
- return result;
- }
- public string MerAuth(string app_auth_code)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + Request.QueryString.ToString(), "支付宝代商家授权回调日志");
- string result = new AlipayFunction().GetToken(app_auth_code);
- function.WriteLog(result, "支付宝代商家授权回调日志");
-
- JsonData jsonObj = JsonMapper.ToObject(result);
- if(jsonObj["alipay_open_auth_token_app_response"]["code"].ToString() == "10000")
- {
- string app_auth_token = jsonObj["alipay_open_auth_token_app_response"]["app_auth_token"].ToString();
- MerchantAddInfo addInfo = maindb.MerchantAddInfo.FirstOrDefault(m => m.Id == 0);
- if(addInfo != null)
- {
- addInfo.AlipayAuthToken = app_auth_token;
- maindb.SaveChanges();
- }
- }
- return "授权成功";
- }
-
- private SortedDictionary<string, string> GetRequestPost(string canshu)
- {
- var sArray = new SortedDictionary<string, string>();
- string[] canshulist = canshu.Split('&');
- foreach (var substring in canshulist)
- {
- string[] datalist = substring.Split('=');
- sArray.Add(datalist[0], HttpUtility.UrlDecode(datalist[1]));
- }
- return sArray;
- }
- #endregion
-
- }
- }
|