Esempio n. 1
0
// 实现接口
func (d *DefaultAlipayClient) ExecuteWithToken(r request.AlipayRequest, token string) (response.AlipayResponse, error) {

	// 请求
	msg, rp, err := d.post(r, token)
	if err != nil {
		return nil, err
	}

	log.Debugf("alipay return : %s", msg)

	// body
	resp := r.GetResponse()
	resp.SetBody(msg)

	// replace
	k := xstrings.ToSnakeCase(resp.ToStr())
	if strings.Contains(msg, k) {
		msg = strings.Replace(msg, k, "response", 1)
	} else {
		msg = strings.Replace(msg, "error_response", "response", 1)
	}

	// convert
	var t = &struct {
		Response response.AlipayResponse `json:"response"`
		Sign     string                  `json:"sign"`
	}{
		resp, "",
	}
	err = json.Unmarshal([]byte(msg), t)
	if err != nil {
		log.Error(err)
	}

	// 当发生安全机制接入错误时
	// 详细见https://fuwu.alipay.com/platform/doc.htm#c09
	if !resp.IsSuccess() {
		logger.SecureError(rp, resp)
	}
	return resp, nil
}
Esempio n. 2
0
func (d *DefaultAlipayClient) post(r request.AlipayRequest, token string) (string, map[string]string, error) {
	// 获取必须参数
	rp := make(map[string]string)
	rp[constants.AppId] = d.AppId
	rp[constants.Method] = r.GetApiMethod()
	rp[constants.SignType] = signType // TODO
	rp[constants.Timestamp] = time.Now().Format("2006-01-02 15:03:04")
	rp[constants.Version] = r.GetApiVersion()
	rp[constants.Charset] = d.Charset
	utils.PutAll(rp, r.GetTextParams())
	// 可选参数
	// rp[constants.Format] = d.Format

	// 请求报文
	content := utils.PrepareContent(rp)
	// 签名
	signed, err := sign.RsaSign(content, d.PrivKey)
	rp[constants.Sign] = signed

	// 编码查询参数
	values := utils.BuildQuery(rp)
	// 请求
	result, err := http.Post(d.ServerURL, "application/x-www-form-urlencoded;charset=utf-8", strings.NewReader(values.Encode()))
	if err != nil {
		log.Error(err)
	}
	msg, err := ioutil.ReadAll(result.Body)
	if err != nil {
		log.Error(err)
	}
	return string(msg), rp, err
}