main.tea 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import EasySDKKernel;
  2. type @kernel = EasySDKKernel
  3. init(kernel: EasySDKKernel) {
  4. @kernel = kernel;
  5. }
  6. model HuabeiConfig {
  7. hbFqNum: string(name='hb_fq_num'),
  8. hbFqSellerPercent: string(name='hb_fq_seller_percent')
  9. }
  10. model AlipayTradeCreateResponse {
  11. httpBody: string(name='http_body', description='响应原始字符串'),
  12. code: string(name='code'),
  13. msg: string(name='msg'),
  14. subCode: string(name='sub_code'),
  15. subMsg: string(name='sub_msg'),
  16. outTradeNo: string(name='out_trade_no'),
  17. tradeNo: string(name='trade_no')
  18. }
  19. api create(subject: string, outTradeNo: string, totalAmount: string, buyerId: string, extendParams: HuabeiConfig): AlipayTradeCreateResponse {
  20. var systemParams: map[string]string = {
  21. method = 'alipay.trade.create',
  22. app_id = @kernel.getConfig("appId"),
  23. timestamp = @kernel.getTimestamp(),
  24. format = 'json',
  25. version = '1.0',
  26. alipay_sdk = @kernel.getSdkVersion(),
  27. charset = 'UTF-8',
  28. sign_type = @kernel.getConfig("signType"),
  29. app_cert_sn = @kernel.getMerchantCertSN(),
  30. alipay_root_cert_sn = @kernel.getAlipayRootCertSN()
  31. };
  32. var bizParams: map[string]any = {
  33. subject = subject,
  34. out_trade_no = outTradeNo,
  35. total_amount = totalAmount,
  36. buyer_id = buyerId,
  37. extend_params = extendParams
  38. };
  39. var textParams: map[string]string = {
  40. };
  41. __request.protocol = @kernel.getConfig("protocol");
  42. __request.method = 'POST';
  43. __request.pathname = '/gateway.do';
  44. __request.headers = {
  45. host = @kernel.getConfig("gatewayHost"),
  46. content-type = 'application/x-www-form-urlencoded;charset=utf-8'
  47. };
  48. __request.query = @kernel.sortMap({
  49. sign = @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig("merchantPrivateKey")),
  50. ... systemParams,
  51. ... textParams
  52. });
  53. __request.body = @kernel.toUrlEncodedRequestBody(bizParams);
  54. } returns {
  55. var respMap: map[string]any = @kernel.readAsJson(__response, "alipay.trade.create");
  56. if (@kernel.isCertMode()) {
  57. if (@kernel.verify(respMap, @kernel.extractAlipayPublicKey(@kernel.getAlipayCertSN(respMap)))) {
  58. return @kernel.toRespModel(respMap);
  59. }
  60. } else {
  61. if (@kernel.verify(respMap, @kernel.getConfig("alipayPublicKey"))) {
  62. return @kernel.toRespModel(respMap);
  63. }
  64. }
  65. throw {
  66. message = '验签失败,请检查支付宝公钥设置是否正确。'
  67. }
  68. } runtime {
  69. ignoreSSL = @kernel.getConfig("ignoreSSL"),
  70. httpProxy = @kernel.getConfig("httpProxy"),
  71. connectTimeout = 15000,
  72. readTimeout = 15000,
  73. retry = {
  74. maxAttempts = 0
  75. }
  76. }