import EasySDKKernel;

type @kernel = EasySDKKernel

init(kernel: EasySDKKernel) {
  @kernel = kernel;
}

model AlipayTradeAppPayResponse {
  body: string(name='body', description='订单信息，字符串形式')
}

function pay(subject: string, outTradeNo: string, totalAmount: string): AlipayTradeAppPayResponse {
  var systemParams: map[string]string = {
    method = 'alipay.trade.app.pay',
    app_id = @kernel.getConfig("appId"),
    timestamp = @kernel.getTimestamp(),
    format = 'json',
    version = '1.0',
    alipay_sdk = @kernel.getSdkVersion(),
    charset = 'UTF-8',
    sign_type = @kernel.getConfig("signType"),
    app_cert_sn = @kernel.getMerchantCertSN(),
    alipay_root_cert_sn = @kernel.getAlipayRootCertSN()
  };

  var bizParams: map[string]any = {
    subject = subject,
    out_trade_no = outTradeNo,
    total_amount = totalAmount
  };

  var textParams: map[string]string = {
  };

  var sign =  @kernel.sign(systemParams, bizParams, textParams, @kernel.getConfig('merchantPrivateKey'));

  var response: map[string]string = {
    body = @kernel.generateOrderString(systemParams, bizParams, textParams, sign)
  };
  return response;
}
