ClientTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using NUnit.Framework;
  2. using Alipay.EasySDK.Factory;
  3. using Alipay.EasySDK.Payment.Common.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using Alipay.EasySDK.Kernel.Util;
  7. namespace UnitTest.Payment.Common
  8. {
  9. public class ClientTest
  10. {
  11. [SetUp]
  12. public void SetUp()
  13. {
  14. Factory.SetOptions(TestAccount.Mini.CONFIG);
  15. }
  16. [Test]
  17. public void TestCreate()
  18. {
  19. string outTradeNo = Guid.NewGuid().ToString();
  20. AlipayTradeCreateResponse response = Factory.Payment.Common().Create("iPhone6 16G",
  21. outTradeNo, "88.88", "2088002656718920");
  22. Assert.IsTrue(ResponseChecker.Success(response));
  23. Assert.AreEqual(response.Code, "10000");
  24. Assert.AreEqual(response.Msg, "Success");
  25. Assert.Null(response.SubCode);
  26. Assert.Null(response.SubMsg);
  27. Assert.NotNull(response.HttpBody);
  28. Assert.AreEqual(response.OutTradeNo, outTradeNo);
  29. Assert.True(response.TradeNo.StartsWith("202"));
  30. }
  31. [Test]
  32. public void TestCreateWithOptional()
  33. {
  34. string outTradeNo = Guid.NewGuid().ToString();
  35. AlipayTradeCreateResponse response = Factory.Payment.Common().Optional("goods_detail", GetGoodsDetail())
  36. .Create("iPhone6 16G", outTradeNo, "0.01", "2088002656718920");
  37. Assert.IsTrue(ResponseChecker.Success(response));
  38. Assert.AreEqual(response.Code, "10000");
  39. Assert.AreEqual(response.Msg, "Success");
  40. Assert.Null(response.SubCode);
  41. Assert.Null(response.SubMsg);
  42. Assert.NotNull(response.HttpBody);
  43. Assert.AreEqual(response.OutTradeNo, outTradeNo);
  44. Assert.True(response.TradeNo.StartsWith("202"));
  45. }
  46. private List<object> GetGoodsDetail()
  47. {
  48. List<object> goodsDetail = new List<object>();
  49. Dictionary<string, object> goodDetail = new Dictionary<string, object>();
  50. goodDetail.Add("goods_id", "apple-01");
  51. goodDetail.Add("goods_name", "iPhone6 16G");
  52. goodDetail.Add("quantity", 1);
  53. goodDetail.Add("price", "0.01");
  54. goodsDetail.Add(goodDetail);
  55. return goodsDetail;
  56. }
  57. [Test]
  58. public void TestQuery()
  59. {
  60. AlipayTradeQueryResponse response = Factory.Payment.Common().Query("6f149ddb-ab8c-4546-81fb-5880b4aaa318");
  61. Assert.IsTrue(ResponseChecker.Success(response));
  62. Assert.AreEqual(response.Code, "10000");
  63. Assert.AreEqual(response.Msg, "Success");
  64. Assert.Null(response.SubCode);
  65. Assert.Null(response.SubMsg);
  66. Assert.NotNull(response.HttpBody);
  67. Assert.AreEqual(response.OutTradeNo, "6f149ddb-ab8c-4546-81fb-5880b4aaa318");
  68. }
  69. [Test]
  70. public void TestCancel()
  71. {
  72. AlipayTradeCancelResponse response = Factory.Payment.Common().Cancel(CreateNewAndReturnOutTradeNo());
  73. Assert.IsTrue(ResponseChecker.Success(response));
  74. Assert.AreEqual(response.Code, "10000");
  75. Assert.AreEqual(response.Msg, "Success");
  76. Assert.Null(response.SubCode);
  77. Assert.Null(response.SubMsg);
  78. Assert.NotNull(response.HttpBody);
  79. Assert.AreEqual(response.Action, "close");
  80. }
  81. [Test]
  82. public void TestClose()
  83. {
  84. AlipayTradeCloseResponse response = Factory.Payment.Common().Close(CreateNewAndReturnOutTradeNo());
  85. Assert.IsTrue(ResponseChecker.Success(response));
  86. Assert.AreEqual(response.Code, "10000");
  87. Assert.AreEqual(response.Msg, "Success");
  88. Assert.Null(response.SubCode);
  89. Assert.Null(response.SubMsg);
  90. Assert.NotNull(response.HttpBody);
  91. }
  92. [Test]
  93. public void TestRefund()
  94. {
  95. AlipayTradeRefundResponse response = Factory.Payment.Common().Refund(CreateNewAndReturnOutTradeNo(), "0.01");
  96. Assert.IsFalse(ResponseChecker.Success(response));
  97. Assert.AreEqual(response.Code, "40004");
  98. Assert.AreEqual(response.Msg, "Business Failed");
  99. Assert.AreEqual(response.SubCode, "ACQ.TRADE_STATUS_ERROR");
  100. Assert.AreEqual(response.SubMsg, "交易状态不合法");
  101. Assert.NotNull(response.HttpBody);
  102. }
  103. [Test]
  104. public void TestQueryRefund()
  105. {
  106. AlipayTradeFastpayRefundQueryResponse response = Factory.Payment.Common().QueryRefund(
  107. "64628156-f784-4572-9540-485b7c91b850", "64628156-f784-4572-9540-485b7c91b850");
  108. Assert.IsTrue(ResponseChecker.Success(response));
  109. Assert.AreEqual(response.Code, "10000");
  110. Assert.AreEqual(response.Msg, "Success");
  111. Assert.IsNull(response.SubCode);
  112. Assert.IsNull(response.SubMsg);
  113. Assert.NotNull(response.HttpBody);
  114. Assert.AreEqual(response.RefundAmount, "0.01");
  115. Assert.AreEqual(response.TotalAmount, "0.01");
  116. }
  117. [Test]
  118. public void TestDownloadBill()
  119. {
  120. AlipayDataDataserviceBillDownloadurlQueryResponse response = Factory.Payment.Common().DownloadBill("trade", "2020-01");
  121. Assert.IsTrue(ResponseChecker.Success(response));
  122. Assert.AreEqual(response.Code, "10000");
  123. Assert.AreEqual(response.Msg, "Success");
  124. Assert.IsNull(response.SubCode);
  125. Assert.IsNull(response.SubMsg);
  126. Assert.NotNull(response.HttpBody);
  127. Assert.IsTrue(response.BillDownloadUrl.StartsWith("http://dwbillcenter.alipay.com/"));
  128. }
  129. private string CreateNewAndReturnOutTradeNo()
  130. {
  131. return Factory.Payment.Common().Create("iPhone6 16G", Guid.NewGuid().ToString(),
  132. "88.88", "2088002656718920").OutTradeNo;
  133. }
  134. }
  135. }