main.tea 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import EasySDKKernel;
  2. type @kernel = EasySDKKernel
  3. init(kernel: EasySDKKernel) {
  4. @kernel = kernel;
  5. }
  6. model AlipaySystemOauthTokenResponse {
  7. httpBody: string(name='http_body', description='响应原始字符串'),
  8. code: string(name='code'),
  9. msg: string(name='msg'),
  10. subCode: string(name='sub_code'),
  11. subMsg: string(name='sub_msg'),
  12. userId: string(name='user_id'),
  13. accessToken: string(name='access_token'),
  14. expiresIn: long(name='expires_in'),
  15. refreshToken: string(name='refresh_token'),
  16. reExpiresIn: long(name='re_expires_in')
  17. }
  18. api getToken(code: string): AlipaySystemOauthTokenResponse {
  19. var systemParams: map[string]string = {
  20. method = 'alipay.system.oauth.token',
  21. app_id = @kernel.getConfig("appId"),
  22. timestamp = @kernel.getTimestamp(),
  23. format = 'json',
  24. version = '1.0',
  25. alipay_sdk = @kernel.getSdkVersion(),
  26. charset = 'UTF-8',
  27. sign_type = @kernel.getConfig("signType"),
  28. app_cert_sn = @kernel.getMerchantCertSN(),
  29. alipay_root_cert_sn = @kernel.getAlipayRootCertSN()
  30. };
  31. var bizParams: map[string]any = {
  32. };
  33. var textParams: map[string]string = {
  34. grant_type = 'authorization_code',
  35. code = code
  36. };
  37. __request.protocol = @kernel.getConfig("protocol");
  38. __request.method = 'POST';
  39. __request.pathname = '/gateway.do';
  40. __request.headers = {
  41. host = @kernel.getConfig("gatewayHost"),
  42. content-type = 'application/x-www-form-urlencoded;charset=utf-8'
  43. };
  44. __request.query = @kernel.sortMap({
  45. sign = @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig("merchantPrivateKey")),
  46. ... systemParams,
  47. ... textParams
  48. });
  49. __request.body = @kernel.toUrlEncodedRequestBody(bizParams);
  50. } returns {
  51. var respMap: object = @kernel.readAsJson(__response, "alipay.system.oauth.token");
  52. if (@kernel.isCertMode()) {
  53. if (@kernel.verify(respMap, @kernel.extractAlipayPublicKey(@kernel.getAlipayCertSN(respMap)))) {
  54. return @kernel.toRespModel(respMap);
  55. }
  56. } else {
  57. if (@kernel.verify(respMap, @kernel.getConfig("alipayPublicKey"))) {
  58. return @kernel.toRespModel(respMap);
  59. }
  60. }
  61. throw {
  62. message = '验签失败,请检查支付宝公钥设置是否正确。'
  63. }
  64. } runtime {
  65. ignoreSSL = @kernel.getConfig("ignoreSSL"),
  66. httpProxy = @kernel.getConfig("httpProxy"),
  67. connectTimeout = 15000,
  68. readTimeout = 15000,
  69. retry = {
  70. maxAttempts = 0
  71. }
  72. }
  73. api refreshToken(refreshToken: string): AlipaySystemOauthTokenResponse {
  74. var systemParams: map[string]string = {
  75. method = 'alipay.system.oauth.token',
  76. app_id = @kernel.getConfig("appId"),
  77. timestamp = @kernel.getTimestamp(),
  78. format = 'json',
  79. version = '1.0',
  80. alipay_sdk = @kernel.getSdkVersion(),
  81. charset = 'UTF-8',
  82. sign_type = @kernel.getConfig("signType"),
  83. app_cert_sn = @kernel.getMerchantCertSN(),
  84. alipay_root_cert_sn = @kernel.getAlipayRootCertSN()
  85. };
  86. var bizParams: map[string]any = {
  87. };
  88. var textParams: map[string]string = {
  89. grant_type = 'refresh_token',
  90. refresh_token = refreshToken
  91. };
  92. __request.protocol = @kernel.getConfig("protocol");
  93. __request.method = 'POST';
  94. __request.pathname = '/gateway.do';
  95. __request.headers = {
  96. host = @kernel.getConfig("gatewayHost"),
  97. content-type = 'application/x-www-form-urlencoded;charset=utf-8'
  98. };
  99. __request.query = @kernel.sortMap({
  100. sign = @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig("merchantPrivateKey")),
  101. ... systemParams,
  102. ... textParams
  103. });
  104. __request.body = @kernel.toUrlEncodedRequestBody(bizParams);
  105. } returns {
  106. var respMap: object = @kernel.readAsJson(__response, "alipay.system.oauth.token");
  107. if (@kernel.isCertMode()) {
  108. if (@kernel.verify(respMap, @kernel.extractAlipayPublicKey(@kernel.getAlipayCertSN(respMap)))) {
  109. return @kernel.toRespModel(respMap);
  110. }
  111. } else {
  112. if (@kernel.verify(respMap, @kernel.getConfig("alipayPublicKey"))) {
  113. return @kernel.toRespModel(respMap);
  114. }
  115. }
  116. throw {
  117. message = '验签失败,请检查支付宝公钥设置是否正确。'
  118. }
  119. } runtime {
  120. ignoreSSL = @kernel.getConfig("ignoreSSL"),
  121. httpProxy = @kernel.getConfig("httpProxy"),
  122. connectTimeout = 15000,
  123. readTimeout = 15000,
  124. retry = {
  125. maxAttempts = 0
  126. }
  127. }