Skip to content

Commit

Permalink
test: Added missing test code for JS API payment.
Browse files Browse the repository at this point in the history
  • Loading branch information
real-zony committed Jan 10, 2024
1 parent 0295c02 commit 34dc464
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ private string HandleRequestObject(HttpMethod method, object body)
return WeChatReflectionHelper.ConvertToQueryString(body);
}

protected virtual async Task ValidateResponseAsync(HttpResponseMessage responseMessage)
protected virtual Task ValidateResponseAsync(HttpResponseMessage responseMessage)
{
if (responseMessage.StatusCode != HttpStatusCode.OK)
switch (responseMessage.StatusCode)
{
throw new CallWeChatPayApiException("微信支付 API 调用失败,状态码为非 200。");
case HttpStatusCode.OK:
return Task.CompletedTask;
case HttpStatusCode.Accepted:
return Task.CompletedTask;
case HttpStatusCode.NoContent:
return Task.CompletedTask;
default:
throw new CallWeChatPayApiException("微信支付 API 调用失败,状态码为非 200。");
}

await Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public class CloseOrderRequest
[Required]
[StringLength(32,MinimumLength = 6)]
[JsonProperty("out_trade_no")]
[JsonIgnore]
public string OutTradeNo { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class QueryRefundOrderRequest
[Required]
[StringLength(64, MinimumLength = 1)]
[JsonProperty("out_refund_no")]
[JsonIgnore]
public string OutRefundNo { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class AbpWeChatPayTestConsts
public const string OpenId = "";
public const string NotifyUrl = "";
public const string SerialNo = "";
public const string RefundNotifyUrl = "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public async Task CreateOrderAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.CreateOrderAsync(new CreateOrderRequest
var request = new CreateOrderRequest
{
MchId = service.MchId,
OutTradeNo = RandomStringHelper.GetRandomString(),
Expand All @@ -42,10 +40,158 @@ public async Task CreateOrderAsync_Test()
{
OpenId = AbpWeChatPayTestConsts.OpenId // 请替换为测试用户的 OpenId,具体 Id 可以在微信公众号平台-用户管理进行查看。
}
});
};

// Act
var response = await service.CreateOrderAsync(request);

// Assert
response.ShouldNotBeNull();
response.PrepayId.ShouldNotBeNullOrEmpty();
}

[Fact]
public async Task QueryOrderByOutTradeNumberAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.QueryOrderByOutTradeNumberAsync(new QueryOrderByOutTradeNumberRequest
{
MchId = AbpWeChatPayTestConsts.MchId,
OutTradeNo = "5dmsi17l83n34fku5z49phcpwa9kpz"
});

// Assert
response.ShouldNotBeNull();
response.TradeState.ShouldBe("SUCCESS");
}

[Fact]
public async Task QueryOrderByTransactionIdAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.QueryOrderByWechatNumberAsync(new QueryOrderByWechatNumberRequest
{
MchId = AbpWeChatPayTestConsts.MchId,
TransactionId = "4200002055202401099853138759"
});

// Assert
response.ShouldNotBeNull();
response.TradeState.ShouldBe("SUCCESS");
}

[Fact]
public async Task CloseOrderAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.CloseOrderAsync(new CloseOrderRequest
{
MchId = AbpWeChatPayTestConsts.MchId,
OutTradeNo = "1ne2k7qitdr78k9zytjpz0tm7qfg8p"
});

// Assert
response.ShouldBeNull();
}

[Fact]
public async Task RefundAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();
var request = new RefundOrderRequest
{
OutRefundNo = RandomStringHelper.GetRandomString(),
OutTradeNo = "kel9xerwcjib2zs8eixyazuis3qsmo",
NotifyUrl = AbpWeChatPayTestConsts.RefundNotifyUrl,
Amount = new RefundOrderRequest.AmountInfo
{
Refund = 1,
Total = 1,
Currency = "CNY"
}
};

// Act
var response = await service.RefundAsync(request);

// Assert
response.ShouldNotBeNull();
response.RefundId.ShouldNotBeNullOrEmpty();
}

[Fact]
public async Task QueryRefundOrderAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.QueryRefundOrderAsync(new QueryRefundOrderRequest
{
OutRefundNo = "r8z61t50kwbg9l1l5ay9s7i8qjyc89"
});

// Assert
response.ShouldNotBeNull();
}

[Fact]
public async Task GetTransactionBillAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.GetTransactionBillAsync(new GetTransactionBillRequest
{
BillDate = "2024-01-09"
});

// Assert
response.ShouldNotBeNull();
}

[Fact]
public async Task GetFundFlowBillAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();

// Act
var response = await service.GetFundFlowBillAsync(new GetFundFlowBillRequest
{
BillDate = "2024-01-09"
});

// Assert
response.ShouldNotBeNull();
}

[Fact]
public async Task DownloadBillFileAsync_Test()
{
// Arrange
var service = await _weChatPayServiceFactory.CreateAsync<JsPaymentService>();
var billResponse = await service.GetTransactionBillAsync(new GetTransactionBillRequest
{
BillDate = "2024-01-09"
});

// Act
var response = await service.DownloadBillFileAsync(billResponse.DownloadUrl);

// Assert
response.ShouldNotBeNull();
response.Length.ShouldBeGreaterThan(0);
}
}

0 comments on commit 34dc464

Please sign in to comment.