/// <summary> /// 获取表单 /// </summary> private string GetForm(AlipayParameterBuilder builder) { FormBuilder formBuilder = new FormBuilder(); formBuilder.AddParam(builder); return(formBuilder.ToString()); }
/// <summary> /// 初始化支付宝结果 /// </summary> /// <param name="response">json响应消息</param> /// <param name="builder">支付宝参数生成器</param> public AlipayResult(string response, AlipayParameterBuilder builder = null) { Raw = response; Builder = builder; _result = new Dictionary <string, string>(); LoadJson(response); }
/// <summary> /// 配置参数生成器 /// </summary> /// <param name="builder">支付宝参数生成器</param> /// <param name="param">请求参数</param> protected void ConfigBuilder(AlipayParameterBuilder builder, TRequest param) { builder.Init(); builder.Method(GetMethod()); InitBuilder(builder, param); InitContentBuilder(builder.Content, param); }
/// <summary> /// 请求结果 /// </summary> /// <param name="config">支付宝配置</param> /// <param name="builder">支付宝参数生成器</param> /// <returns></returns> protected virtual async Task <PayResult> RequestResult(AlipayConfig config, AlipayParameterBuilder builder) { var result = new AlipayResult(await Request(config, builder)); WriteLog(config, builder, result); return(CreateResult(builder, result)); }
/// <summary> /// 写日志 /// </summary> /// <param name="config">支付宝配置</param> /// <param name="builder">支付宝参数生成器</param> /// <param name="result">支付宝结果</param> protected void WriteLog(AlipayConfig config, AlipayParameterBuilder builder, AlipayResult result) { var log = GetLog(); if (log.IsTraceEnabled == false) { return; } log.Class(GetType().FullName) .Caption("支付宝支付") .Content($"支付宝方式 : {GetPayWay().Description()}") .Content($"支付网关 : {config.GetGatewayUrl()}") .Content("请求参数:") .Content(builder.GetDictionary()) .Content() .Content("返回结果:") .Content(result.GetDictionary()) .Content() .Content("原始请求:") .Content(builder.ToString()) .Content() .Content("原始响应:") .Content(result.Raw) .Trace(); }
/// <summary> /// 配置 /// </summary> /// <param name="builder">支付宝参数生成器</param> /// <param name="param">支付参数</param> protected void Config(AlipayParameterBuilder builder, PayParam param) { builder.Init(param); builder.Method(GetMethod()); builder.Content.Scene(GetScene()); InitContentBuilder(builder.Content, param); }
public void TestConfig() { _builder = new AlipayParameterBuilder(new AlipayConfig { AppId = "a" }); _output.WriteLine(_builder.ToString()); Assert.Equal("app_id=a&charset=utf-8&format=json&sign_type=RSA2×tamp=2000-10-10 10:10:10&version=1.0", _builder.ToString()); }
/// <summary> /// 创建结果 /// </summary> /// <param name="builder">支付宝参数生成器</param> /// <param name="result">支付宝结果</param> /// <returns></returns> protected virtual PayResult CreateResult(AlipayParameterBuilder builder, AlipayResult result) { return(new PayResult(result.Success, result.GetTradeNo(), result.Raw) { Parameter = builder.ToString(), Message = result.GetMessage() }); }
/// <summary> /// 发送请求 /// </summary> protected virtual async Task <string> Request(AliPayConfig config, AlipayParameterBuilder builder) { var resonse = await Web.Client() .Post(config.GetGatewayUrl()) .JsonData(builder.GetDictionary()) .ResultAsync(); return(await resonse.Content.ReadAsStringAsync()); }
/// <summary> /// 创建结果 /// </summary> protected override PayResult CreateResult(AlipayParameterBuilder builder, AlipayResult result) { return(new PayResult(result.Success, result.GetTradeNo(), result.Raw) { Parameter = builder.ToString(), Message = result.GetMessage(), Result = result.GetValue(AlipayConst.QrCode) }); }
/// <summary> /// 请求结果 /// </summary> protected override Task <AlipayResult> RequstResult(AlipayConfig config, AlipayParameterBuilder builder) { var result = builder.Result(true); WriteLog(config, builder, result); return(Task.FromResult(new AlipayResult { Result = result })); }
/// <summary> /// 写日志 /// </summary> protected void WriteLog(AlipayConfig config, AlipayParameterBuilder builder, string content) { Logger.Error(GetType().FullName + " 支付宝支付:" + $"支付方式 : {EnumUtil.GetEnumDescription(GetPayWay())}" + $"支付网关 : {config.GetGatewayUrl()}" + "请求参数:" + builder.GetDictionary() + "原始请求:" + builder.ToString() + "内容: " + content ); }
/// <summary> /// 获取调试参数 /// </summary> /// <param name="param">支付参数</param> public virtual async Task <string> Debug(PayParam param) { var config = await ConfigProvider.GetConfigAsync(); Validate(config, param); var builder = new AlipayParameterBuilder(config, param); Config(builder, param); return(builder.ToString()); }
/// <summary> /// 请求 /// </summary> /// <param name="param">请求参数</param> protected async Task <AlipayResult> Request(TRequest param) { var config = await ConfigProvider.GetConfigAsync(param); Validate(config, param); var builder = new AlipayParameterBuilder(config); ConfigBuilder(builder, param); return(await RequstResult(config, builder)); }
/// <summary> /// 支付 /// </summary> /// <param name="param">支付参数</param> /// <returns></returns> public virtual async Task <PayResult> PayAsync(PayParam param) { var config = await ConfigProvider.GetConfigAsync(); Validate(config, param); var builder = new AlipayParameterBuilder(config); Config(builder, param); return(await RequestResult(config, builder)); }
/// <summary> /// 发送请求 /// </summary> protected virtual async Task <string> Request(AlipayConfig config, AlipayParameterBuilder builder) { if (IsSend == false) { return(string.Empty); } return(await Web.Client() .Post(config.GetGatewayUrl()) .Data(builder.GetDictionary()) .ResultAsync()); }
/// <summary> /// 写日志 /// </summary> protected void WriteLog(AliPayConfig config, AlipayParameterBuilder builder, AlipayResult result) { var logContent = LogContentBuilder.CreateLogContentBuilder() .SetEventId(Guid.NewGuid()).SetMoudle(GetType().FullName).SetTitle("支付宝支付") .AddContent($"支付宝支付 : {GetType()}") .AddContent($"支付网关 : {config?.GetGatewayUrl()}") .AddContent($"请求参数:{builder?.GetDictionary()?.ToXml()}") .AddContent($"返回结果:{result?.GetDictionary()}") .AddContent($"原始响应:{result?.Raw}") .Build(); Logger.LogInfo(logContent); }
/// <summary> /// 支付 /// </summary> /// <param name="param">支付参数</param> public override async Task <PayResult> PayAsync(PayParam param) { var config = await ConfigProvider.GetConfigAsync(); Validate(config, param); var builder = new AlipayParameterBuilder(config); Config(builder, param); var form = GetForm(builder); WriteLog(config, builder, form); return(new PayResult { Result = form }); }
/// <summary> /// 写日志 /// </summary> protected void WriteLog(AlipayConfig config, AlipayParameterBuilder builder, string content) { var log = GetLog(); if (log.IsTraceEnabled == false) { return; } log.Class(GetType().FullName) .Caption("支付宝支付") .Content($"支付方式 : {GetType()}") .Content($"支付网关 : {config.GetGatewayUrl()}") .Content("请求参数:") .Content(builder.GetDictionary()) .Content() .Content("原始请求:") .Content(builder.ToString()) .Content() .Content("内容: ") .Content(content) .Trace(); }
/// <summary> /// 测试初始化 /// </summary> public AlipayParameterBuilderTest(ITestOutputHelper output) { _output = output; Time.SetTime(TestConst.Time); _builder = new AlipayParameterBuilder(new AlipayConfig()); }
protected override void InitContentBuilder(AlipayParameterBuilder builder, AlipayBarcodePayRequest param) { builder.Remove(AlipayConst.ReturnUrl); }
protected void WriteLog(AlipayConfig config, AlipayParameterBuilder builder, AlipayResult result) { }
protected void WriteLog(AlipayConfig config, AlipayParameterBuilder builder, string content) { }
/// <summary> /// 初始化支付宝支付服务 /// </summary> /// <param name="provider">支付宝配置提供器</param> protected AlipayServiceBase(IAlipayConfigProvider provider) { provider.CheckNull(nameof(provider)); _config = provider.GetConfig(); _builder = new AlipayParameterBuilder(_config); }
/// <summary> /// 测试初始化 /// </summary> public AlipayParameterBuilderTest(ITestOutputHelper output) { _output = output; Time.SetTime("2000-10-10 10:10:10"); _builder = new AlipayParameterBuilder(new AlipayConfig()); }
/// <summary> /// 请求结果 /// </summary> protected virtual async Task <AlipayResult> RequstResult(AlipayConfig config, AlipayParameterBuilder builder) { var response = await SendRequest(config, builder); var result = new AlipayResult(response, builder); WriteLog(config, builder, result); return(result); }
/// <summary> /// 初始化参数生成器 /// </summary> /// <param name="builder">支付宝参数生成器</param> /// <param name="param">请求参数</param> protected virtual void InitBuilder(AlipayParameterBuilder builder, TRequest param) { }
protected virtual void InitContentBuilder(AlipayParameterBuilder builder, TPayParam param) { }
/// <summary> /// 初始化参数生成器 /// </summary> /// <param name="builder">支付宝参数生成器</param> /// <param name="param">支付参数</param> protected override void InitBuilder(AlipayParameterBuilder builder, PayParam param) { builder.Init(param); builder.Content.Scene(GetScene()); }
protected override void InitContentBuilder(AlipayParameterBuilder builder, AlipayPagePayRequest param) { builder.ReturnUrl(param.ReturnUrl); }