main.tea 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. connectTimeout = 15000,
  124. readTimeout = 15000,
  125. retry = {
  126. maxAttempts = 0
  127. }
  128. }
  129. api preCreate(subject: string, outTradeNo: string, totalAmount: string): AlipayTradePrecreateResponse {
  130. var systemParams: map[string]string = {
  131. method = 'alipay.trade.precreate',
  132. app_id = @kernel.getConfig("appId"),
  133. timestamp = @kernel.getTimestamp(),
  134. format = 'json',
  135. version = '1.0',
  136. alipay_sdk = @kernel.getSdkVersion(),
  137. charset = 'UTF-8',
  138. sign_type = @kernel.getConfig("signType"),
  139. app_cert_sn = @kernel.getMerchantCertSN(),
  140. alipay_root_cert_sn = @kernel.getAlipayRootCertSN()
  141. };
  142. var bizParams: map[string]any = {
  143. subject = subject,
  144. out_trade_no = outTradeNo,
  145. total_amount = totalAmount
  146. };
  147. var textParams: map[string]string = {
  148. };
  149. __request.protocol = @kernel.getConfig("protocol");
  150. __request.method = 'POST';
  151. __request.pathname = '/gateway.do';
  152. __request.headers = {
  153. host = @kernel.getConfig("gatewayHost"),
  154. content-type = 'application/x-www-form-urlencoded;charset=utf-8'
  155. };
  156. __request.query = @kernel.sortMap({
  157. sign = @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig("merchantPrivateKey")),
  158. ... systemParams,
  159. ... textParams
  160. });
  161. __request.body = @kernel.toUrlEncodedRequestBody(bizParams);
  162. } returns {
  163. var respMap: map[string]any = @kernel.readAsJson(__response, "alipay.trade.precreate");
  164. if (@kernel.isCertMode()) {
  165. if (@kernel.verify(respMap, @kernel.extractAlipayPublicKey(@kernel.getAlipayCertSN(respMap)))) {
  166. return @kernel.toRespModel(respMap);
  167. }
  168. } else {
  169. if (@kernel.verify(respMap, @kernel.getConfig("alipayPublicKey"))) {
  170. return @kernel.toRespModel(respMap);
  171. }
  172. }
  173. throw {
  174. message = '验签失败,请检查支付宝公钥设置是否正确。'
  175. }
  176. } runtime {
  177. connectTimeout = 15000,
  178. readTimeout = 15000,
  179. retry = {
  180. maxAttempts = 0
  181. }
  182. }