Example #1
0
// https://ripplecn.com/bridge?type=quote&amount=1%2FCNY&destination=z&address=ra5tSyQ2cvJUHfAvEdmC89HKSKZTn7xXMw&alipay_account=aa&full_name=bb&contact_info=cc
func (c *MainController) ApiQuote() {
	sa := c.Ctx.Request.URL.Query().Get("amount")
	address := c.Ctx.Request.URL.Query().Get("address")
	bank_name := c.Ctx.Request.URL.Query().Get("bank_name")
	card_number := c.Ctx.Request.URL.Query().Get("card_number")
	full_name := c.Ctx.Request.URL.Query().Get("full_name")
	contact_info := c.Ctx.Request.URL.Query().Get("contact_info")

	a, err := data.NewAmount(sa)
	if err != nil {
		glog.Error(err)
		resp := quoteErrorResp("the query amount err")
		sendResp(resp, c.Ctx)
		return
	}
	glog.Info("ApiQuote:", address, bank_name, card_number, full_name, contact_info, a, a.IsNative())
	sv := a.Value.String()
	am, err := strconv.ParseFloat(sv, 64)
	if err != nil {
		glog.Error(err)
		resp := quoteErrorResp("the query amount err2")
		sendResp(resp, c.Ctx)
		return
	}

	if a.IsNative() {
		pv, _ := data.NewNativeValue(1e6)
		a.Value, _ = a.Value.Divide(*pv)
	}

	currency := a.Currency.String()
	fee := am * models.Gconf.Fees.Rate
	min := models.Gconf.Fees.FeeMap[currency][0]
	max := models.Gconf.Fees.FeeMap[currency][1]
	if fee < min {
		fee = min
	}
	if fee > max {
		fee = max
	}
	acc, err := data.NewAccountFromAddress(models.Gconf.ColdWallet)
	if err != nil {
		glog.Fatal(err)
	}
	a.Issuer = *acc

	u := &models.User{
		UName:     full_name,
		UWallet:   address,
		UBankName: bank_name,
		UBankId:   card_number,
		UContact:  contact_info,
	}

	t := models.Withdrawal
	if a.IsNative() {
		t = models.Redeem
	}
	req, err := models.AddReq("", models.Gconf.ColdWallet, t, u, a.Currency.String(), am/1e6, fee)
	if err != nil {
		// glog.Error(err)
		return
	}

	fv, err := data.NewValue(fmt.Sprintf("%f", fee), a.IsNative())
	if err != nil {
		glog.Fatal(err)
	}

	a.Value, err = a.Value.Add(*fv)
	if err != nil {
		glog.Fatal(err)
	}

	if a.IsNative() {
		pv, _ := data.NewNativeValue(1e6)
		a.Value, _ = a.Value.Divide(*pv)
		a.Currency, _ = data.NewCurrency("ICC")
	}

	quote := &Quote{
		Address:        models.Gconf.ColdWallet,
		DestinationTag: 2147483647,
		Send:           []data.Amount{*a},
		InvoiceID:      req.InvoiceId,
	}
	resp := &QuoteResp{
		Result:    "success",
		QuoteJson: quote,
	}
	glog.Info("Quote:", address, full_name, "OK")
	sendResp(resp, c.Ctx)
}
Example #2
0
func (c *MainController) addReq(t int) error {
	amt, ok := c.GetSession("Amt").(*AmountInfo)
	if !ok {
		c.Ctx.Request.ParseMultipartForm(1024 * 1024 * 10)
		glog.Infof("%#+v", c.Ctx.Request)
		gbankId := c.GetString("gbankId")
		currency := c.GetString("currency")
		fee, err := c.GetFloat("fees")
		if err != nil {
			glog.Errorln(err)
			return err
		}
		a, err := c.GetFloat("amount")
		if err != nil {
			glog.Errorln(err)
			return err
		}
		amt = &AmountInfo{
			BankId:   gbankId,
			Currency: currency,
			Amount:   a,
			Fees:     fee,
		}
	}
	uname := c.GetString("name")
	uwallet := c.GetString("iccWallet")
	ubankName := c.GetString("bankName")
	ubankId := c.GetString("bankId")
	ucontact := c.GetString("contact")

	rf, header, err := c.Ctx.Request.FormFile("certificate")
	if err != nil {
		glog.Error(err)
		return err
	}
	defer rf.Close()

	filePath := "./certificates/" + RandToken() + "-" + header.Filename

	wf, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		glog.Error(err)
		return err
	}
	io.Copy(wf, rf)
	wf.Close()

	u := &models.User{
		UName:        uname,
		UWallet:      uwallet,
		UBankName:    ubankName,
		UBankId:      ubankId,
		UContact:     ucontact,
		UCertificate: filePath[1:],
	}

	_, err = models.AddReq(amt.BankId, "", t, u, amt.Currency, amt.Amount, amt.Fees)
	if err != nil {
		glog.Errorln(err)
		return err
	}
	return nil
}