Client.cs 14 KB

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