Coder Social home page Coder Social logo

open's Introduction

开放平台

alipay

OauthTokenRequest

client := alipay.NewClient(config.Gateway, config.AppId, config.PrivateKey, config.AliPayPublicKey, alipay.RSA2)

request := &alipay.OauthTokenRequest{
    GrantType: "authorization_code",
    Code:      authCode,
}

responseBase, err := client.Excute(request)
if err != nil {
    return
}
response := responseBase.(*alipay.OauthTokenResponse)

if response.IsSuccess() {
    //success
} else {
    //error
}

TradeCreateRequest

client := alipay.NewClient(config.Gateway, config.AppId, config.PrivateKey, config.AliPayPublicKey, alipay.RSA2)

var goodDetail = []*alipay.GoodsDetailItem{}

for _, l := range list {
    goodDetail = append(goodDetail, &alipay.GoodsDetailItem{
        GoodsId:   l.Id.Hex(),
        GoodsName: l.Name,
        Quantity:  strconv.Itoa(l.Count),
        Price:     strconv.FormatFloat(l.Price, 'f', 2, 64),
    })
}
request := &alipay.TradeCreateRequest{
    Subject:        subject,
    OutTradeNo:     outtradeno.Hex(),
    TotalAmount:    amount,
    BuyerId:        user.UserId,
    GoodsDetail:    goodDetail,
    TimeoutExpress: "6h",
}

responseBase, err := client.Excute(request)
if err != nil {
    c.JSON(510, cart.H{"error": err.Error()})
}
response := responseBase.(*alipay.TradeCreateResponse)
if response.IsSuccess() {
    //创建订单
    order := db.NewOrder()
    order.Id = outtradeno
    order.UserId = user.UserId
    order.TradeNo = response.TradeCreate.TradeNo
    order.Source = "alipay"
    order.Amount = amount
    order.StoreId = bson.ObjectIdHex(storeId)
    order.Status = 100
    order.List = list
    success, err := order.Insert()
    if success {
        user.Outtradeno = outtradeno.Hex()
        user.Set(c)
        c.JSON(200, cart.H{
            "tradeno":    response.TradeCreate.TradeNo,
            "outtradeno": outtradeno.Hex(),
        })
    } else {
        c.JSON(510, cart.H{"error": err.Error()})
    }

} else {
    c.JSON(510, cart.H{"error": response.TradeCreate.SubMsg})
}

wechat

open's People

Contributors

yangglive avatar

Watchers

James Cloos avatar JamieJiang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.