Exemple #1
0
func MustGetExchangeRate(from string, to string) float64 {
	if !kmgStrings.IsAllAphphabet(from) {
		panic(fmt.Errorf("[MustGetExchangeRate] fromName [%s] should like USD", from))
	}
	if !kmgStrings.IsAllAphphabet(to) {
		panic(fmt.Errorf("[MustGetExchangeRate] toName [%s] should like USD", to))
	}
	if from == to {
		panic(fmt.Errorf("[MustGetExchangeRate] fromName [%s] =toName [%s] "))
	}
	out := kmgHttp.MustUrlGetContent(kmgHttp.MustSetParameterMapToUrl("https://query.yahooapis.com/v1/public/yql", map[string]string{
		"q":        fmt.Sprintf(`select * from yahoo.finance.xchange where pair="%s%s"`, from, to),
		"format":   "json",
		"env":      "store://datatables.org/alltableswithkeys",
		"callback": "",
	}))
	//out should look like {"query":{"count":1,"created":"2015-06-01T02:37:44Z","lang":"en","results":{"rate":{"id":"JPYCNY","Name":"JPY/CNY","Rate":"0.0501","Date":"6/1/2015","Time":"3:37am","Ask":"0.0501","Bid":"0.0501"}}}}
	resp := yqlRateResponse{}
	kmgJson.MustUnmarshal(out, &resp)
	if resp.Query.Results.Rate.Rate == "N/A" {
		panic(fmt.Errorf("[MustGetExchangeRate] Currency [%s][%s] not exist", from, to)) //有一种货币不存在.
	}
	rate, err := kmgStrconv.ParseFloat64(resp.Query.Results.Rate.Rate)
	if err != nil {
		panic(err)
	}
	return rate
}
Exemple #2
0
//下载汇率文件接口
func (ot *OverseaTrade) MustGetExchangeRateList() (output []ExchangeRate) {
	query := map[string]string{
		"service": "forex_rate_file",
		"partner": ot.PartnerId,
	}
	ot.md5Sign(query)
	content := kmgHttp.MustUrlGetContent(kmgHttp.MustSetParameterMapToUrl("https://mapi.alipay.com/gateway.do", query))

	lineList := strings.Split(string(content), "\n")
	output = make([]ExchangeRate, 0, len(lineList))
	for _, line := range lineList {
		line := strings.TrimSpace(line)
		if line == "" {
			continue
		}
		part := strings.Split(line, "|")
		if len(part) < 4 {
			panic(fmt.Errorf("[MustGetExchangeRateList] format error"))
		}
		t, err := time.Parse("20060102150405", part[0]+part[1]) //考虑别处基本不会使用,就直接写在这个地方了.
		if err != nil {
			panic(err)
		}
		rate, err := kmgStrconv.ParseFloat64(part[3])
		if err != nil {
			panic(err)
		}
		output = append(output, ExchangeRate{
			Time:     t,
			Currency: part[2],
			Rate:     rate,
		})
	}
	return output
}
Exemple #3
0
func TestStartProfileOnAddr(ot *testing.T) {
	StartProfileOnAddr("abc", "127.0.0.1:51001")
	time.Sleep(10 * time.Millisecond)
	content := kmgHttp.MustUrlGetContent("http://127.0.0.1:51001/abc/gc")
	kmgTest.Equal(content, []byte("SUCCESS"), string(content))

	resp, err := http.Get("http://127.0.0.1:51001/abc")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(resp.StatusCode, 200)

	resp, err = http.Get("http://127.0.0.1:51001/")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(resp.StatusCode, 404)

	StartProfileOnAddr("", "127.0.0.1:51002")
	time.Sleep(10 * time.Millisecond)
	content = kmgHttp.MustUrlGetContent("http://127.0.0.1:51002/gc")
	kmgTest.Equal(content, []byte("SUCCESS"), string(content))

	resp, err = http.Get("http://127.0.0.1:51002/")
	kmgTest.Equal(err, nil)
	kmgTest.Equal(resp.StatusCode, 200)
}
Exemple #4
0
// 单条交易查询接口
func (ot *OverseaTrade) SingleTransactionQuery(outTradeId string) (tran *OverseaTradeTransaction, err error) {
	query := map[string]string{
		"service":        "single_trade_query",
		"partner":        ot.PartnerId,
		"_input_charset": "utf-8",
		"out_trade_no":   outTradeId,
	}
	ot.md5Sign(query)
	content := kmgHttp.MustUrlGetContent(kmgHttp.MustSetParameterMapToUrl("https://mapi.alipay.com/gateway.do", query))
	response := overseaTradeTransactionQueryResponse{}
	err = xml.Unmarshal(content, &response)
	if err != nil {
		return nil, err
	}
	if response.IsSuccess != "T" {
		return nil, fmt.Errorf("[支付宝单条交易查询接口错误] [%s]", response.Error)
	}
	return &OverseaTradeTransaction{
		TradeNo:     response.TradeNo,
		OutTradeNo:  response.OutTradeNo,
		Subject:     response.Subject,
		TradeStatus: response.TradeStatus,
	}, nil
}