Skip to content

Commit

Permalink
feat(tenpayv3): 新增微工卡投保相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed May 15, 2024
1 parent 1e3a7d4 commit 4030fc5
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,51 @@ public static class WechatTenpayClientExecutePayrollCardExtensions
return await client.SendFlurlRequestAsJsonAsync<Models.CreatePayrollCardTransferBatchResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
#endregion

#region Wesure
/// <summary>
/// <para>异步调用 [POST] /payroll-card/wesure/insurance 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/wiki/doc/apiv3_partner/Offline/apis/chapter4_1_32.shtml ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.CreatePayrollCardWesureInsuranceResponse> ExecuteCreatePayrollCardWesureInsuranceAsync(this WechatTenpayClient client, Models.CreatePayrollCardWesureInsuranceRequest request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "payroll-card", "wesure", "insurance");

return await client.SendFlurlRequestAsJsonAsync<Models.CreatePayrollCardWesureInsuranceResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// <para>异步调用 [POST] /payroll-card/wesure/token-validations 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/wiki/doc/apiv3_partner/Offline/apis/chapter4_1_31.shtml ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.GetPayrollCardWesureTokenValidationResponse> ExecuteGetPayrollCardWesureTokenValidationAsync(this WechatTenpayClient client, Models.GetPayrollCardWesureTokenValidationRequest request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "payroll-card", "wesure", "token-validations");

return await client.SendFlurlRequestAsJsonAsync<Models.GetPayrollCardWesureTokenValidationResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;

namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
{
/// <summary>
/// <para>表示 [POST] /payroll-card/wesure/insurance 接口的请求。</para>
/// </summary>
public class CreatePayrollCardWesureInsuranceRequest : WechatTenpayRequest
{
/// <summary>
/// 获取或设置微信商户号。
/// </summary>
[Newtonsoft.Json.JsonProperty("sp_mchid")]
[System.Text.Json.Serialization.JsonPropertyName("sp_mchid")]
public string MerchantId { get; set; } = string.Empty;

/// <summary>
/// 获取或设置子商户号。
/// </summary>
[Newtonsoft.Json.JsonProperty("sub_mchid")]
[System.Text.Json.Serialization.JsonPropertyName("sub_mchid")]
public string? SubMerchantId { get; set; }

/// <summary>
/// 获取或设置微保保单号。
/// </summary>
[Newtonsoft.Json.JsonProperty("wesure_order_no")]
[System.Text.Json.Serialization.JsonPropertyName("wesure_order_no")]
public string WesureOrderNumber { get; set; } = string.Empty;

/// <summary>
/// 获取或设置保险公司保单号。
/// </summary>
[Newtonsoft.Json.JsonProperty("insurance_company_order_no")]
[System.Text.Json.Serialization.JsonPropertyName("insurance_company_order_no")]
public string InsuranceCompanyOrderNumber { get; set; } = string.Empty;

/// <summary>
/// 获取或设置保险公司名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("insurance_company_name")]
[System.Text.Json.Serialization.JsonPropertyName("insurance_company_name")]
public string InsuranceCompanyName { get; set; } = string.Empty;

/// <summary>
/// 获取或设置授权 Token。
/// </summary>
[Newtonsoft.Json.JsonProperty("token")]
[System.Text.Json.Serialization.JsonPropertyName("token")]
public string? Token { get; set; }

/// <summary>
/// 获取或设置用户唯一标识。
/// </summary>
[Newtonsoft.Json.JsonProperty("openid")]
[System.Text.Json.Serialization.JsonPropertyName("openid")]
public string OpenId { get; set; } = string.Empty;

/// <summary>
/// 获取或设置投保状态。
/// </summary>
[Newtonsoft.Json.JsonProperty("insure_state")]
[System.Text.Json.Serialization.JsonPropertyName("insure_state")]
public string InsureState { get; set; } = string.Empty;

/// <summary>
/// 获取或设置保障生效时间。
/// </summary>
[Newtonsoft.Json.JsonProperty("insure_effect_time")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.Common.Rfc3339DateTimeOffsetConverter))]
[System.Text.Json.Serialization.JsonPropertyName("insure_effect_time")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.Rfc3339DateTimeOffsetConverter))]
public DateTimeOffset InsureEffectTime { get; set; }

/// <summary>
/// 获取或设置保障失效时间。
/// </summary>
[Newtonsoft.Json.JsonProperty("insure_invalid_time")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.Common.Rfc3339DateTimeOffsetConverter))]
[System.Text.Json.Serialization.JsonPropertyName("insure_invalid_time")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.Rfc3339DateTimeOffsetConverter))]
public DateTimeOffset InsureInvalidTime { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
{
/// <summary>
/// <para>表示 [POST] /payroll-card/wesure/insurance 接口的响应。</para>
/// </summary>
public class CreatePayrollCardWesureInsuranceResponse : WechatTenpayResponse
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;

namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
{
/// <summary>
/// <para>表示 [POST] /payroll-card/wesure/token-validations 接口的请求。</para>
/// </summary>
[WechatTenpaySensitive]
public class GetPayrollCardWesureTokenValidationRequest : WechatTenpayRequest
{
/// <summary>
/// 获取或设置微信商户号。
/// </summary>
[Newtonsoft.Json.JsonProperty("sp_mchid")]
[System.Text.Json.Serialization.JsonPropertyName("sp_mchid")]
public string MerchantId { get; set; } = string.Empty;

/// <summary>
/// 获取或设置子商户号。
/// </summary>
[Newtonsoft.Json.JsonProperty("sub_mchid")]
[System.Text.Json.Serialization.JsonPropertyName("sub_mchid")]
public string? SubMerchantId { get; set; }

/// <summary>
/// 获取或设置授权 Token。
/// </summary>
[Newtonsoft.Json.JsonProperty("token")]
[System.Text.Json.Serialization.JsonPropertyName("token")]
public string? Token { get; set; }

/// <summary>
/// 获取或设置用户姓名(需使用平台公钥/证书加密)。
/// </summary>
[Newtonsoft.Json.JsonProperty("user_name")]
[System.Text.Json.Serialization.JsonPropertyName("user_name")]
[WechatTenpaySensitiveProperty(scheme: Constants.SignSchemes.WECHATPAY2_RSA_2048_WITH_SHA256, algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
[WechatTenpaySensitiveProperty(scheme: Constants.SignSchemes.WECHATPAY2_SM2_WITH_SM3, algorithm: Constants.EncryptionAlgorithms.SM2_C1C3C2_ASN1)]
public string? UserName { get; set; }

/// <summary>
/// 获取或设置身份证号码(需使用平台公钥/证书加密)。
/// </summary>
[Newtonsoft.Json.JsonProperty("id_card_number")]
[System.Text.Json.Serialization.JsonPropertyName("id_card_number")]
[WechatTenpaySensitiveProperty(scheme: Constants.SignSchemes.WECHATPAY2_RSA_2048_WITH_SHA256, algorithm: Constants.EncryptionAlgorithms.RSA_2048_ECB_PKCS8_OAEP_WITH_SHA1_AND_MGF1)]
[WechatTenpaySensitiveProperty(scheme: Constants.SignSchemes.WECHATPAY2_SM2_WITH_SM3, algorithm: Constants.EncryptionAlgorithms.SM2_C1C3C2_ASN1)]
public string? IdCardNumber { get; set; }

/// <summary>
/// 获取或设置核身类型。
/// </summary>
[Newtonsoft.Json.JsonProperty("authenticate_type")]
[System.Text.Json.Serialization.JsonPropertyName("authenticate_type")]
public string AuthenticateType { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
{
/// <summary>
/// <para>表示 [POST] /payroll-card/wesure/token-validations 接口的响应。</para>
/// </summary>
public class GetPayrollCardWesureTokenValidationResponse : WechatTenpayResponse
{
/// <summary>
/// 获取或设置校验结果。
/// </summary>
[Newtonsoft.Json.JsonProperty("result")]
[System.Text.Json.Serialization.JsonPropertyName("result")]
public string Result { get; set; } = default!;

/// <summary>
/// 获取或设置用户唯一标识。
/// </summary>
[Newtonsoft.Json.JsonProperty("openid")]
[System.Text.Json.Serialization.JsonPropertyName("openid")]
public string? OpenId { get; set; }

/// <summary>
/// 获取或设置核身成功时间。
/// </summary>
[Newtonsoft.Json.JsonProperty("authenticate_success_time")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.Common.Rfc3339DateTimeOffsetConverter))]
[System.Text.Json.Serialization.JsonPropertyName("authenticate_success_time")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.Rfc3339DateTimeOffsetConverter))]
public DateTimeOffset? SuccessTime { get; set; }

/// <summary>
/// 获取或设置失败原因。
/// </summary>
[Newtonsoft.Json.JsonProperty("fail_reason")]
[System.Text.Json.Serialization.JsonPropertyName("fail_reason")]
public string? FailReason { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"insurance_company_name": "平安",
"insurance_company_order_no": "5fds5345dfd",
"insure_effect_time": "2018-06-08T10:34:56+08:00",
"insure_invalid_time": "2018-06-08T10:34:56+08:00",
"insure_state": "INSURE_SUCCESS",
"openid": "11111111",
"sp_mchid": "1900000109",
"sub_mchid": "1900000109",
"wesure_order_no": "5fds5345dfd"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"authenticate_type": "NORMAL",
"id_card_number": "7FzH5XksJG3a8HLLsaaUV6K54y1OnPMY5",
"sp_mchid": "1900000109",
"sub_mchid": "1900000109",
"token": "abcdefghijklmn",
"user_name": "LP7bT4hQXUsOZCEvK2YrSiqFsnP0oRMfeoLN0vBg"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authenticate_success_time": "2015-05-20T13:29:35+08:00",
"fail_reason": "SP_MCH_NO_PERMISSION",
"openid": "11111111",
"result": "SUCCESS"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,62 @@ static void AssertMockRequestModel(Models.CreatePayrollCardTokenRequest request,
}
}

[Fact(DisplayName = "测试用例:加密请求中的敏感数据([POST] /payroll-card/wesure/token-validations)")]
public async Task TestEncryptRequestSensitiveProperty_GetPayrollCardWesureTokenValidationRequest()
{
static Models.GetPayrollCardWesureTokenValidationRequest GenerateMockRequestModel()
{
return new Models.GetPayrollCardWesureTokenValidationRequest()
{
UserName = MOCK_PLAIN_STR,
IdCardNumber = MOCK_PLAIN_STR
};
}

static void AssertMockRequestModel(Models.GetPayrollCardWesureTokenValidationRequest request, Func<string, string> decryptor)
{
Assert.NotEqual(MOCK_PLAIN_STR, request.UserName!);
Assert.NotEqual(MOCK_PLAIN_STR, request.IdCardNumber!);
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.UserName!));
Assert.Equal(MOCK_PLAIN_STR, decryptor.Invoke(request.IdCardNumber!));
Assert.Equal(MOCK_CERT_SN, request.WechatpayCertificateSerialNumber!, ignoreCase: true);
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantRSACertificatePrivateKey))
{
using (var client = CreateMockClientUseRSA(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseRSA(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecuteGetPayrollCardWesureTokenValidationAsync(request);
AssertMockRequestModel(request, (cipher) => Utilities.RSAUtility.DecryptWithECB(RSA_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}

if (!string.IsNullOrEmpty(TestConfigs.WechatMerchantSM2CertificatePrivateKey))
{
using (var client = CreateMockClientUseSM2(autoEncrypt: false))
{
var request = GenerateMockRequestModel();
client.EncryptRequestSensitiveProperty(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}

using (var client = CreateMockClientUseSM2(autoEncrypt: true))
{
var request = GenerateMockRequestModel();
await client.ExecuteGetPayrollCardWesureTokenValidationAsync(request);
AssertMockRequestModel(request, (cipher) => Utilities.SM2Utility.Decrypt(SM2_PEM_PRIVATE_KEY, (EncodedString)cipher)!);
}
}
}

[Fact(DisplayName = "测试用例:加密请求中的敏感数据([POST] /profitsharing/orders)")]
public async Task TestEncryptRequestSensitiveProperty_CreateProfitSharingOrderRequest()
{
Expand Down

0 comments on commit 4030fc5

Please sign in to comment.