Exemplo n.º 1
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
}
Exemplo n.º 2
0
func (ot *OverseaTrade) mustNotifyActionV2(ctx *kmgHttp.Context, f func(info OverseaTradeTransaction)) {
	kmgLog.Log("Alipay", "Oversea PayNotifyAction", ctx.GetInMap())
	var err error
	ctx.MustPost()
	info := OverseaTradeTransaction{}
	//info.NotifyId = ctx.MustInStr("notify_id") 这两项没有什么意义.
	//info.NotifyTime = kmgTime.MustFromMysqlFormatInLocation(ctx.MustInStr("notify_time"), kmgTime.BeijingZone)
	info.OutTradeNo = ctx.MustInStr("out_trade_no")

	info.Currency = ctx.MustInStr("currency")
	info.TotalFee, err = kmgStrconv.ParseFloat64(ctx.MustInStr("total_fee"))
	if err != nil {
		panic(err)
	}
	info.TradeStatus = OverseaTradeStatus(ctx.MustInStr("trade_status"))
	info.TradeNo = ctx.MustInStr("trade_no")
	err = ot.md5Verify(ctx.GetInMap())
	if err != nil {
		panic(err)
	}
	err = ot.VerifyNotify(ctx.MustInStr("notify_id"))
	if err != nil {
		panic(err)
	}
	// 向支付宝询问这个订单的情况
	oInfo := ot.MustSingleTransactionQuery(info.OutTradeNo)
	if oInfo.TradeStatus != info.TradeStatus {
		panic("两次查询订单状态不一致")
	}
	info.Subject = oInfo.Subject
	f(info)
	ctx.WriteString("success")
}
Exemplo n.º 3
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
}
Exemplo n.º 4
0
//缓存时间为1天
func MustGetExchangeRateWithCache(from string, to string) float64 {
	b, err := kmgCache.FileTtlCache("MustGetExchangeRateWithCache_"+from+"_"+to, func() (b []byte, ttl time.Duration, err error) {
		f := MustGetExchangeRate(from, to)
		return []byte(kmgStrconv.FormatFloat(f)), kmgTime.Day, nil
	})
	if err != nil {
		panic(err)
	}
	f, err := kmgStrconv.ParseFloat64(string(b))
	if err != nil {
		panic(err)
	}
	return f
}
Exemplo n.º 5
0
// 同步回调
// 调用前请清除您自己的参数.
// @deprecated 请使用 OverseaTrade.PayFinishCallback 和 OverseaTrade.PayCloseCallback
func (ot *OverseaTrade) MustReturnPage(ctx *kmgHttp.Context) (info OverseaTradeTransaction) {
	kmgLog.Log("Alipay", "Oversea PayReturnPage", ctx.GetInMap())
	var err error
	info.OutTradeNo = ctx.MustInStr("out_trade_no")
	info.Currency = ctx.MustInStr("currency")
	info.TotalFee, err = kmgStrconv.ParseFloat64(ctx.MustInStr("total_fee"))
	if err != nil {
		panic(err)
	}
	info.TradeStatus = OverseaTradeStatus(ctx.MustInStr("trade_status"))
	info.TradeNo = ctx.MustInStr("trade_no")
	//这个也可以验证数据,只是文档上面没写.
	err = ot.md5Verify(ctx.GetInMap())
	if err != nil {
		panic(err)
	}
	// 向支付宝询问这个订单的情况
	oInfo := ot.MustSingleTransactionQuery(info.OutTradeNo)
	if oInfo.TradeStatus != info.TradeStatus {
		panic("两次查询订单状态不一致")
	}
	info.Subject = oInfo.Subject
	return info
}