123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- using System;
- using System.Collections.Generic;
- using Aop.Api;
- using Library;
- using Microsoft.AspNetCore.Http;
- using Aop.Api.Request;
- using Aop.Api.Response;
- using Aop.Api.Util;
- using MySystem.Models;
- using System.Linq;
- namespace MySystem
- {
- public class AlipayFunction
- {
- public readonly static AlipayFunction Instance = new AlipayFunction();
- private AlipayFunction()
- { }
- public string Pid = "2088720140260314";
- private string AppId = "2021004100667901";
- private string PrivateKey = "MIIEowIBAAKCAQEAhw2C7AypN/cSBNZU1LU2th58fzvLlhlfg7jZz3xNJQwpICtSF6hXXB2qChecEZv7ZGyiuuamTn/c+ZAKJAl3EZ8fMEgTrdK9M8+3DX6FIzWywCuPktsnxLIkTj4Lt/LjPI3P3yZLESuWzHIQ+TGnwEgm/Fx5ESWribFOddLRnNpINk36y+gtaVoXpQ742Wt6Yg1l4FZrzCAOHjLfVeTyAzmXXm8TGdzep83fyCqSBp+rdpb1CGQukHYpCapyk52hM4wqsAVdndAqO5qoRi5BfuyiVs2BR6Oe+8iF4rD5FJ/Wk6QguemWX8C/4jTnuNs1YsFuCTHqYwr8k8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J1uPuKEq79zeqNEfDHFZHW+4YzT4xDlq0vm5YYhphcyr80Z0jLzGgmoRHKTfDwBHYF5TXdUsWgJEGx9jucPi0jFkK6HDAerUqG4WJc8iu+93pyYYfPIszWHEwzxEutjpUnU2HpXLMhgUXQrwSWrQpHTkuPMY8ZCQwKZ4TBMV";
- private string PublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhw2C7AypN/cSBNZU1LU2th58fzvLlhlfg7jZz3xNJQwpICtSF6hXXB2qChecEZv7ZGyiuuamTn/c+ZAKJAl3EZ8fMEgTrdK9M8+3DX6FIzWywCuPktsnxLIkTj4Lt/LjPI3P3yZLESuWzHIQ+TGnwEgm/Fx5ESWribFOddLRnNpINk36y+gtaVoXpQ742Wt6Yg1l4FZrzCAOHjLfVeTyAzmXXm8TGdzep83fyCqSBp+rdpb1CGQukHYpCapyk52hM4wqsAVdndAqO5qoRi5BfuyiVs2BR6Oe+8iF4rD5FJ/Wk6QguemWX8C/4jTnuNs1YsFuCTHqYwr8k8J6ekEdgwIDAQAB";
- public string AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxuS3Edgrruau8SO2DsK0f2KcE+A6pGJ1f8CdL395qwoX5RnEx9yRUotkwN0ZF6qMUojFKu/J/X2DpxBWBc6jIa+cnFwPUykefXcXj/AQsgiIbFzNXBH9NSz+hngXP6xDs8D1u9h+fJB+TYD+WFXKwOcGwcx23uHLHUDi1UIEZOhT2Eui+aMCYVygUWH7JynTtbFQZyZcSsD2Dxj5OJNJlK110jarFgvqY98WxqtaRtB9Y8whwRRlGfa5GP2r/Vt3xbGeAflRl5khFgRLZgeFIfxJpEj1TZV2tCtpFS0uhXJGEQbMnzFynx8UaOx/0hBCyik1MQuu3flym6nx7XxPSQIDAQAB";
-
-
- #region 代商家签约
-
- public string GetBatchNo(string Name, string Mobile, string Email)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayOpenAgentCreateRequest request = new AlipayOpenAgentCreateRequest();
- request.BizContent = "{" +
- "\"account\":\"" + Email + "\"," +
- "\"contact_info\":{" +
- "\"contact_name\":\"" + Name + "\"," +
- "\"contact_mobile\":\"" + Mobile + "\"," +
- "\"contact_email\":\"" + Email + "\"" +
- "}" +
- "}";
- string result = "";
- try
- {
- AlipayOpenAgentCreateResponse response = client.Execute(request);
- result = response.Body;
- LogHelper.Instance.WriteLog(result, "开启事务");
-
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "接口创建应用事务异常");
- result = "";
- }
- return result;
- }
-
- public string AgentConfirm(string BatchNo)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayOpenAgentConfirmRequest request = new AlipayOpenAgentConfirmRequest();
- request.BizContent = "{" +
- " \"batch_no\":\"" + BatchNo + "\"" +
- "}";
- string result = "";
- try
- {
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(request), "提交代商户签约");
- AlipayOpenAgentConfirmResponse response = client.Execute(request);
- result = response.Body;
- LogHelper.Instance.WriteLog(result + "\n\n", "提交代商户签约");
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "提交代商户签约异常");
- result = "";
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string CommonSign(string BatchNo, string MccCode, string BusinessLicenseNo = "", string BusinessLicensePicPath = "", string SpecialLicensePicPath = "", string ShopScenePicPath = "", string ShopSignBoardPicPath = "")
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayOpenAgentFacetofaceSignRequest request = new AlipayOpenAgentFacetofaceSignRequest() ;
- request.BatchNo = BatchNo;
- request.MccCode = MccCode;
- if (!string.IsNullOrEmpty(BusinessLicenseNo))
- {
- request.BusinessLicenseNo = BusinessLicenseNo;
- }
- if (!string.IsNullOrEmpty(BusinessLicensePicPath))
- {
- FileItem BusinessLicensePic = new FileItem(function.getPath(BusinessLicensePicPath));
-
- request.BusinessLicensePic = BusinessLicensePic;
- FileItem BusinessLicenseAuthPic = new FileItem(function.getPath(BusinessLicensePicPath));
-
- request.BusinessLicenseAuthPic = BusinessLicenseAuthPic;
- }
- if (!string.IsNullOrEmpty(SpecialLicensePicPath))
- {
- FileItem SpecialLicensePic = new FileItem(function.getPath(SpecialLicensePicPath));
-
- request.SpecialLicensePic = SpecialLicensePic;
- }
- if (!string.IsNullOrEmpty(ShopScenePicPath))
- {
- if(ShopScenePicPath.Contains(","))
- {
- ShopScenePicPath = ShopScenePicPath.Split(',')[0];
- }
- FileItem ShopScenePic = new FileItem(function.getPath(ShopScenePicPath));
-
- request.ShopScenePic = ShopScenePic;
- }
- if (!string.IsNullOrEmpty(ShopSignBoardPicPath))
- {
- if(ShopSignBoardPicPath.Contains(","))
- {
- ShopSignBoardPicPath = ShopSignBoardPicPath.Split(',')[0];
- }
- FileItem ShopSignBoardPic = new FileItem(function.getPath(ShopSignBoardPicPath));
-
- request.ShopSignBoardPic = ShopSignBoardPic;
- }
-
-
- request.Rate = "0.38";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- string result = "";
- try
- {
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(request), "代商家发起签约产品申请");
- AlipayOpenAgentFacetofaceSignResponse response = client.Execute(request);
- result = response.Body;
- LogHelper.Instance.WriteLog(result, "代商家发起签约产品申请");
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "代签约产品通用接口异常");
- result = "";
- }
- return result;
- }
-
- public string CommonSignConfirm(string BatchNo)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayOpenAgentCommonsignConfirmRequest request= new AlipayOpenAgentCommonsignConfirmRequest();
- request.BizContent="{" +
- "\"batch_no\":\"" + BatchNo + "\"" +
- "}";
- string result = "";
- try
- {
- AlipayOpenAgentCommonsignConfirmResponse response = client.Execute(request);
- result = response.Body;
- LogHelper.Instance.WriteLog(result, "提交信息确认");
-
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "接口创建应用事务异常");
- result = "";
- }
- return result;
- }
- #endregion
- #region (查询申请单状态)查询签约申请的结果
- public string QuerySignStatus(string BatchNo)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayOpenAgentOrderQueryRequest request= new AlipayOpenAgentOrderQueryRequest() ;
- request.BizContent="{" +
- " \"batch_no\":\"" + BatchNo + "\"" +
- "}";
- string result = "";
- try
- {
- AlipayOpenAgentOrderQueryResponse response = client.Execute(request);
- result = response.Body;
- LogHelper.Instance.WriteLog(result, "查询签约申请的结果");
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "查询签约申请的结果异常");
- result = "";
- }
- return result;
-
- }
- #endregion
-
- #region 小程序支付请调用MySystemLib下的统一收单交易创建,返回交易号返给小程序请求支付my.tradePay
- public string GetToken()
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
- request.BizContent = "{" +
- "\"grant_type\":\"authorization_code\"," +
- "\"code\":\"P16be77ee92264acc99553528b8a7103\"" +
- " }";
- string result = "";
- try
- {
- AlipayOpenAuthTokenAppResponse response = client.Execute(request);
- result = response.Body;
-
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "支付宝统一收单交易创建(获取authtoken)异常");
- result = "";
- }
- return result;
- }
- public string CreateTrade(string OrderNo, string Subject, string Amount, string BuyerId, string Notify_Url, string Token)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
- request.SetNotifyUrl(Notify_Url);
- request.BizContent = "{" +
- "\"out_trade_no\":\"" + OrderNo + "\"," +
- "\"total_amount\":" + Amount + "," +
- "\"subject\":\"" + Subject + "\"," +
- "\"buyer_id\":\"" + BuyerId + "\"," +
- "\"timeout_express\":\"10m\"" +
- "}";
- string result = "";
- try
- {
- AlipayTradeCreateResponse response = client.Execute(request, null, Token);
- result = response.Body;
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "支付宝统一收单交易创建异常");
- result = "";
- }
- return result;
- }
- public string QueryTrade(string OrderNo, string Token)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
- request.BizContent = "{" +
- "\"out_trade_no\":\"" + OrderNo + "\"" +
- "}";
- string result = "";
- try
- {
- AlipayTradeQueryResponse response = client.Execute(request, null, Token);
- result = response.Body;
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "统一收单线下交易查询异常");
- result = "";
- }
- return result;
- }
- #endregion
- #region 退款
- public string Refund(string TradeNo, string RefundNo, string Amount, string Reason, string MerToken)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
- request.BizContent = "{" +
- "\"trade_no\":\"" + TradeNo + "\"," +
- "\"refund_amount\":" + Amount + "," +
- "\"out_request_no\":\"" + RefundNo + "\"," +
- "\"refund_reason\":\"" + Reason + "\"" +
-
-
-
-
-
-
-
- "}";
- string result = "";
- try
- {
- AlipayTradeRefundResponse response = client.Execute(request, null, MerToken);
- result = response.Body;
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "退款异常");
- result = "";
- }
- return result;
- }
- #endregion
- #region 分账
- public string Settle(string OrderNo, string TradeNo, List<RoyaltyParameters> Parameters, string MerToken)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayTradeOrderSettleRequest request= new AlipayTradeOrderSettleRequest();
- string royalty_parameters = "";
- foreach (RoyaltyParameters Parameter in Parameters)
- {
- royalty_parameters += " {" +
-
-
- " \"trans_in\":\"" + Parameter.Account + "\"," +
- " \"amount\":" + Parameter.Amount + "" +
-
- " },";
- }
- request.BizContent = "{" +
- " \"out_request_no\":\"" + OrderNo + "\"," +
- " \"trade_no\":\"" + TradeNo + "\"," +
- " \"royalty_parameters\":[" +
- royalty_parameters.TrimEnd(',') +
- " ]" +
- "}";
- string result = "";
- try
- {
- AlipayTradeOrderSettleResponse response = client.Execute(request, null, MerToken);
- result = response.Body;
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "分账异常");
- result = "";
- }
- return result;
- }
- #endregion
- #region 绑定分账关系
- public string BindSettleRelation(string RequestNo, string Account, string MerToken)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayTradeRoyaltyRelationBindRequest request = new AlipayTradeRoyaltyRelationBindRequest();
- request.BizContent="{" +
- " \"receiver_list\":[" +
- " {" +
- " \"type\":\"userId\"," +
- " \"account\":\"" + Account + "\"" +
-
-
-
-
- " }" +
- " ]," +
- " \"out_request_no\":\"" + RequestNo + "\"" +
- "}";
- string result = "";
- try
- {
- AlipayTradeRoyaltyRelationBindResponse response = client.Execute(request, null, MerToken);
- result = response.Body;
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "绑定分账关系异常");
- result = "";
- }
- return result;
- }
- #endregion
- #region 查看分账
- public string QuerySettle(string OrderNo, string TradeNo, string MerToken)
- {
- IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
- AlipayTradeOrderSettleQueryRequest request = new AlipayTradeOrderSettleQueryRequest();
- request.BizContent="{" +
- " \"out_request_no\":\"" + OrderNo + "\"," +
- " \"trade_no\":\"" + TradeNo + "\"" +
- "}";
- string result = "";
- try
- {
- AlipayTradeOrderSettleQueryResponse response = client.Execute(request, null, MerToken);
- result = response.Body;
- LogHelper.Instance.WriteLog(request.BizContent + "\n" + result, "支付宝查询分账结果");
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(ex.ToString(), "分账异常");
- result = "";
- }
- return result;
- }
- #endregion
- }
- }
|