main.tea 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. connectTimeout = 15000,
  70. readTimeout = 15000,
  71. retry = {
  72. maxAttempts = 0
  73. }
  74. }