main.tea 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import EasySDKKernel;
  2. type @kernel = EasySDKKernel
  3. init(kernel: EasySDKKernel) {
  4. @kernel = kernel;
  5. }
  6. model TradeFundBill {
  7. fundChannel: string(name='fund_channel'),
  8. bankCode: string(name='bank_code'),
  9. amount: string(name='amount'),
  10. realAmount: string(name='real_amount')
  11. }
  12. model VoucherDetail {
  13. id: string(name='id'),
  14. name: string(name='name'),
  15. type: string(name='type'),
  16. amount: string(name='amount'),
  17. merchantContribute: string(name='merchant_contribute'),
  18. otherContribute: string(name='other_contribute'),
  19. memo: string(name='memo'),
  20. templateId: string(name='template_id'),
  21. purchaseBuyerContribute: string(name='purchase_buyer_contribute'),
  22. purchaseMerchantContribute: string(name='purchase_merchant_contribute'),
  23. purchaseAntContribute: string(name='purchase_ant_contribute')
  24. }
  25. model AlipayTradePayResponse {
  26. httpBody: string(name='http_body', description='响应原始字符串'),
  27. code: string(name='code'),
  28. msg: string(name='msg'),
  29. subCode: string(name='sub_code'),
  30. subMsg: string(name='sub_msg'),
  31. tradeNo: string(name='trade_no'),
  32. outTradeNo: string(name='out_trade_no'),
  33. buyerLogonId: string(name='buyer_logon_id'),
  34. settleAmount: string(name='settle_amount'),
  35. payCurrency: string(name='pay_currency'),
  36. payAmount: string(name='pay_amount'),
  37. settleTransRate: string(name='settle_trans_rate'),
  38. transPayRate: string(name='trans_pay_rate'),
  39. totalAmount: string(name='total_amount'),
  40. transCurrency: string(name='trans_currency'),
  41. settleCurrency: string(name='settle_currency'),
  42. receiptAmount: string(name='receipt_amount'),
  43. buyerPayAmount: string(name='buyer_pay_amount'),
  44. pointAmount: string(name='point_amount'),
  45. invoiceAmount: string(name='invoice_amount'),
  46. gmtPayment: string(name='gmt_payment'),
  47. fundBillList: [ TradeFundBill ](name='fund_bill_list'),
  48. cardBalance: string(name='card_balance'),
  49. storeName: string(name='store_name'),
  50. buyerUserId: string(name='buyer_user_id'),
  51. discountGoodsDetail: string(name='discount_goods_detail'),
  52. voucherDetailList: [ VoucherDetail ](name='voucher_detail_list'),
  53. advanceAmount: string(name='advance_amount'),
  54. authTradePayMode: string(name='auth_trade_pay_mode'),
  55. chargeAmount: string(name='charge_amount'),
  56. chargeFlags: string(name='charge_flags'),
  57. settlementId: string(name='settlement_id'),
  58. businessParams: string(name='business_params'),
  59. buyerUserType: string(name='buyer_user_type'),
  60. mdiscountAmount: string(name='mdiscount_amount'),
  61. discountAmount: string(name='discount_amount'),
  62. buyerUserName: string(name='buyer_user_name')
  63. }
  64. model AlipayTradePrecreateResponse {
  65. httpBody: string(name='http_body', description='响应原始字符串'),
  66. code: string(name='code'),
  67. msg: string(name='msg'),
  68. subCode: string(name='sub_code'),
  69. subMsg: string(name='sub_msg'),
  70. outTradeNo: string(name='out_trade_no'),
  71. qrCode: string(name='qr_code'),
  72. }
  73. api pay(subject: string, outTradeNo: string, totalAmount: string, authCode: string): AlipayTradePayResponse {
  74. var systemParams: map[string]string = {
  75. method = 'alipay.trade.pay',
  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. subject = subject,
  88. out_trade_no = outTradeNo,
  89. total_amount = totalAmount,
  90. auth_code = authCode,
  91. scene = 'bar_code'
  92. };
  93. var textParams: map[string]string = {
  94. };
  95. __request.protocol = @kernel.getConfig("protocol");
  96. __request.method = 'POST';
  97. __request.pathname = '/gateway.do';
  98. __request.headers = {
  99. host = @kernel.getConfig("gatewayHost"),
  100. content-type = 'application/x-www-form-urlencoded;charset=utf-8'
  101. };
  102. __request.query = @kernel.sortMap({
  103. sign = @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig("merchantPrivateKey")),
  104. ... systemParams,
  105. ... textParams
  106. });
  107. __request.body = @kernel.toUrlEncodedRequestBody(bizParams);
  108. } returns {
  109. var respMap: map[string]any = @kernel.readAsJson(__response, "alipay.trade.pay");
  110. if (@kernel.isCertMode()) {
  111. if (@kernel.verify(respMap, @kernel.extractAlipayPublicKey(@kernel.getAlipayCertSN(respMap)))) {
  112. return @kernel.toRespModel(respMap);
  113. }
  114. } else {
  115. if (@kernel.verify(respMap, @kernel.getConfig("alipayPublicKey"))) {
  116. return @kernel.toRespModel(respMap);
  117. }
  118. }
  119. throw {
  120. message = '验签失败,请检查支付宝公钥设置是否正确。'
  121. }
  122. } runtime {
  123. ignoreSSL = @kernel.getConfig("ignoreSSL"),
  124. httpProxy = @kernel.getConfig("httpProxy"),
  125. connectTimeout = 15000,
  126. readTimeout = 15000,
  127. retry = {
  128. maxAttempts = 0
  129. }
  130. }
  131. api preCreate(subject: string, outTradeNo: string, totalAmount: string): AlipayTradePrecreateResponse {
  132. var systemParams: map[string]string = {
  133. method = 'alipay.trade.precreate',
  134. app_id = @kernel.getConfig("appId"),
  135. timestamp = @kernel.getTimestamp(),
  136. format = 'json',
  137. version = '1.0',
  138. alipay_sdk = @kernel.getSdkVersion(),
  139. charset = 'UTF-8',
  140. sign_type = @kernel.getConfig("signType"),
  141. app_cert_sn = @kernel.getMerchantCertSN(),
  142. alipay_root_cert_sn = @kernel.getAlipayRootCertSN()
  143. };
  144. var bizParams: map[string]any = {
  145. subject = subject,
  146. out_trade_no = outTradeNo,
  147. total_amount = totalAmount
  148. };
  149. var textParams: map[string]string = {
  150. };
  151. __request.protocol = @kernel.getConfig("protocol");
  152. __request.method = 'POST';
  153. __request.pathname = '/gateway.do';
  154. __request.headers = {
  155. host = @kernel.getConfig("gatewayHost"),
  156. content-type = 'application/x-www-form-urlencoded;charset=utf-8'
  157. };
  158. __request.query = @kernel.sortMap({
  159. sign = @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig("merchantPrivateKey")),
  160. ... systemParams,
  161. ... textParams
  162. });
  163. __request.body = @kernel.toUrlEncodedRequestBody(bizParams);
  164. } returns {
  165. var respMap: map[string]any = @kernel.readAsJson(__response, "alipay.trade.precreate");
  166. if (@kernel.isCertMode()) {
  167. if (@kernel.verify(respMap, @kernel.extractAlipayPublicKey(@kernel.getAlipayCertSN(respMap)))) {
  168. return @kernel.toRespModel(respMap);
  169. }
  170. } else {
  171. if (@kernel.verify(respMap, @kernel.getConfig("alipayPublicKey"))) {
  172. return @kernel.toRespModel(respMap);
  173. }
  174. }
  175. throw {
  176. message = '验签失败,请检查支付宝公钥设置是否正确。'
  177. }
  178. } runtime {
  179. ignoreSSL = @kernel.getConfig("ignoreSSL"),
  180. httpProxy = @kernel.getConfig("httpProxy"),
  181. connectTimeout = 15000,
  182. readTimeout = 15000,
  183. retry = {
  184. maxAttempts = 0
  185. }
  186. }