Client.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // This file is auto-generated, don't edit it. Thanks.
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using Tea;
  8. using Tea.Utils;
  9. using Alipay.EasySDK.Util.Generic.Models;
  10. namespace Alipay.EasySDK.Util.Generic
  11. {
  12. public class Client
  13. {
  14. protected Alipay.EasySDK.Kernel.Client _kernel;
  15. public Client(Alipay.EasySDK.Kernel.Client kernel)
  16. {
  17. this._kernel = kernel;
  18. }
  19. public AlipayOpenApiGenericResponse Execute(string method, Dictionary<string, string> textParams, Dictionary<string, object> bizParams)
  20. {
  21. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  22. {
  23. {"connectTimeout", 15000},
  24. {"readTimeout", 15000},
  25. {"retry", new Dictionary<string, int?>
  26. {
  27. {"maxAttempts", 0},
  28. }},
  29. };
  30. TeaRequest _lastRequest = null;
  31. Exception _lastException = null;
  32. long _now = System.DateTime.Now.Millisecond;
  33. int _retryTimes = 0;
  34. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  35. {
  36. if (_retryTimes > 0)
  37. {
  38. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  39. if (backoffTime > 0)
  40. {
  41. TeaCore.Sleep(backoffTime);
  42. }
  43. }
  44. _retryTimes = _retryTimes + 1;
  45. try
  46. {
  47. TeaRequest request_ = new TeaRequest();
  48. Dictionary<string, string> systemParams = new Dictionary<string, string>
  49. {
  50. {"method", method},
  51. {"app_id", this._kernel.GetConfig("appId")},
  52. {"timestamp", this._kernel.GetTimestamp()},
  53. {"format", "json"},
  54. {"version", "1.0"},
  55. {"alipay_sdk", this._kernel.GetSdkVersion()},
  56. {"charset", "UTF-8"},
  57. {"sign_type", this._kernel.GetConfig("signType")},
  58. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  59. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  60. };
  61. request_.Protocol = this._kernel.GetConfig("protocol");
  62. request_.Method = "POST";
  63. request_.Pathname = "/gateway.do";
  64. request_.Headers = new Dictionary<string, string>
  65. {
  66. {"host", this._kernel.GetConfig("gatewayHost")},
  67. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  68. };
  69. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  70. (
  71. new Dictionary<string, string>()
  72. {
  73. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  74. },
  75. systemParams,
  76. textParams
  77. ));
  78. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  79. _lastRequest = request_;
  80. TeaResponse response_ = TeaCore.DoAction(request_, runtime_);
  81. Dictionary<string, object> respMap = this._kernel.ReadAsJson(response_, method);
  82. if (this._kernel.IsCertMode())
  83. {
  84. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  85. {
  86. return TeaModel.ToObject<AlipayOpenApiGenericResponse>(this._kernel.ToRespModel(respMap));
  87. }
  88. }
  89. else
  90. {
  91. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  92. {
  93. return TeaModel.ToObject<AlipayOpenApiGenericResponse>(this._kernel.ToRespModel(respMap));
  94. }
  95. }
  96. throw new TeaException(new Dictionary<string, string>
  97. {
  98. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  99. });
  100. }
  101. catch (Exception e)
  102. {
  103. if (TeaCore.IsRetryable(e))
  104. {
  105. _lastException = e;
  106. continue;
  107. }
  108. throw e;
  109. }
  110. }
  111. throw new TeaUnretryableException(_lastRequest, _lastException);
  112. }
  113. public async Task<AlipayOpenApiGenericResponse> ExecuteAsync(string method, Dictionary<string, string> textParams, Dictionary<string, object> bizParams)
  114. {
  115. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  116. {
  117. {"connectTimeout", 15000},
  118. {"readTimeout", 15000},
  119. {"retry", new Dictionary<string, int?>
  120. {
  121. {"maxAttempts", 0},
  122. }},
  123. };
  124. TeaRequest _lastRequest = null;
  125. Exception _lastException = null;
  126. long _now = System.DateTime.Now.Millisecond;
  127. int _retryTimes = 0;
  128. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  129. {
  130. if (_retryTimes > 0)
  131. {
  132. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  133. if (backoffTime > 0)
  134. {
  135. TeaCore.Sleep(backoffTime);
  136. }
  137. }
  138. _retryTimes = _retryTimes + 1;
  139. try
  140. {
  141. TeaRequest request_ = new TeaRequest();
  142. Dictionary<string, string> systemParams = new Dictionary<string, string>
  143. {
  144. {"method", method},
  145. {"app_id", this._kernel.GetConfig("appId")},
  146. {"timestamp", this._kernel.GetTimestamp()},
  147. {"format", "json"},
  148. {"version", "1.0"},
  149. {"alipay_sdk", this._kernel.GetSdkVersion()},
  150. {"charset", "UTF-8"},
  151. {"sign_type", this._kernel.GetConfig("signType")},
  152. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  153. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  154. };
  155. request_.Protocol = this._kernel.GetConfig("protocol");
  156. request_.Method = "POST";
  157. request_.Pathname = "/gateway.do";
  158. request_.Headers = new Dictionary<string, string>
  159. {
  160. {"host", this._kernel.GetConfig("gatewayHost")},
  161. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  162. };
  163. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  164. (
  165. new Dictionary<string, string>()
  166. {
  167. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  168. },
  169. systemParams,
  170. textParams
  171. ));
  172. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  173. _lastRequest = request_;
  174. TeaResponse response_ = await TeaCore.DoActionAsync(request_, runtime_);
  175. Dictionary<string, object> respMap = await this._kernel.ReadAsJsonAsync(response_, method);
  176. if (this._kernel.IsCertMode())
  177. {
  178. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  179. {
  180. return TeaModel.ToObject<AlipayOpenApiGenericResponse>(this._kernel.ToRespModel(respMap));
  181. }
  182. }
  183. else
  184. {
  185. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  186. {
  187. return TeaModel.ToObject<AlipayOpenApiGenericResponse>(this._kernel.ToRespModel(respMap));
  188. }
  189. }
  190. throw new TeaException(new Dictionary<string, string>
  191. {
  192. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  193. });
  194. }
  195. catch (Exception e)
  196. {
  197. if (TeaCore.IsRetryable(e))
  198. {
  199. _lastException = e;
  200. continue;
  201. }
  202. throw e;
  203. }
  204. }
  205. throw new TeaUnretryableException(_lastRequest, _lastException);
  206. }
  207. /// <summary>
  208. /// ISV代商户代用,指定appAuthToken
  209. /// </summary>
  210. /// <param name="appAuthToken">代调用token</param>
  211. /// <returns>本客户端,便于链式调用</returns>
  212. public Client Agent(string appAuthToken)
  213. {
  214. _kernel.InjectTextParam("app_auth_token", appAuthToken);
  215. return this;
  216. }
  217. /// <summary>
  218. /// 用户授权调用,指定authToken
  219. /// </summary>
  220. /// <param name="authToken">用户授权token</param>
  221. /// <returns>本客户端,便于链式调用</returns>
  222. public Client Auth(string authToken)
  223. {
  224. _kernel.InjectTextParam("auth_token", authToken);
  225. return this;
  226. }
  227. /// <summary>
  228. /// 设置异步通知回调地址,此处设置将在本调用中覆盖Config中的全局配置
  229. /// </summary>
  230. /// <param name="url">异步通知回调地址,例如:https://www.test.com/callback </param>
  231. /// <returns>本客户端,便于链式调用</returns>
  232. public Client AsyncNotify(string url)
  233. {
  234. _kernel.InjectTextParam("notify_url", url);
  235. return this;
  236. }
  237. /// <summary>
  238. /// 将本次调用强制路由到后端系统的测试地址上,常用于线下环境内外联调,沙箱与线上环境设置无效
  239. /// </summary>
  240. /// <param name="testUrl">后端系统测试地址</param>
  241. /// <returns>本客户端,便于链式调用</returns>
  242. public Client Route(string testUrl)
  243. {
  244. _kernel.InjectTextParam("ws_service_url", testUrl);
  245. return this;
  246. }
  247. /// <summary>
  248. /// 设置API入参中没有的其他可选业务请求参数(biz_content下的字段)
  249. /// </summary>
  250. /// <param name="key">业务请求参数名称(biz_content下的字段名,比如timeout_express)</param>
  251. /// <param name="value">
  252. /// 业务请求参数的值,一个可以序列化成JSON的对象
  253. /// 如果该字段是一个字符串类型(String、Price、Date在SDK中都是字符串),请使用string储存
  254. /// 如果该字段是一个数值型类型(比如:Number),请使用long储存
  255. /// 如果该字段是一个复杂类型,请使用嵌套的Dictionary指定各下级字段的值
  256. /// 如果该字段是一个数组,请使用List储存各个值
  257. /// 对于更复杂的情况,也支持Dictionary和List的各种组合嵌套,比如参数是值是个List,List中的每种类型是一个复杂对象
  258. /// </param>
  259. /// <returns>本客户端,便于链式调用</returns>
  260. public Client Optional(string key, object value)
  261. {
  262. _kernel.InjectBizParam(key, value);
  263. return this;
  264. }
  265. /// <summary>
  266. /// 批量设置API入参中没有的其他可选业务请求参数(biz_content下的字段)
  267. /// optional方法的批量版本
  268. /// </summary>
  269. /// <param name="optionalArgs">可选参数集合,每个参数由key和value组成,key和value的格式请参见optional方法的注释</param>
  270. /// <returns>本客户端,便于链式调用</returns>
  271. public Client BatchOptional(Dictionary<string, object> optionalArgs)
  272. {
  273. foreach (var pair in optionalArgs)
  274. {
  275. _kernel.InjectBizParam(pair.Key, pair.Value);
  276. }
  277. return this;
  278. }
  279. }
  280. }