Skip to content

Commit

Permalink
新增统一下单借口
Browse files Browse the repository at this point in the history
  • Loading branch information
panjinjie authored and panjinjie committed Oct 26, 2017
1 parent e7589fd commit 17858b9
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 83 deletions.
79 changes: 39 additions & 40 deletions alipay.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ppp

import (
"fmt"
"strconv"
"time"

Expand All @@ -15,6 +14,7 @@ var (
aliPayUrl string //alipay的地址
aliPayServiceProviderId string //收佣商户号
aliPayAppId string //应用ID
aliPayNotifyUrl string //异步通知地址
)

const (
Expand All @@ -32,12 +32,14 @@ type AliPayInit struct {
Url string
ServiceProviderId string
ConfigPath string
NotifyUrl string
}

func (a *AliPayInit) Init() {
aliPayUrl = a.Url
aliPayAppId = a.AppId
aliPayServiceProviderId = a.ServiceProviderId
aliPayNotifyUrl = a.NotifyUrl
loadAliPayCertKey(a.ConfigPath)
}

Expand All @@ -53,9 +55,17 @@ func (A *AliPay) AuthSigned(request *AuthRequest, resp *Response) error {
resp.Code = AuthErr
return nil
}
auth.Status = request.Status
updateToken(auth.MchId, PAYTYPE_ALIPAY, bson.M{"$set": bson.M{"status": request.Status, "account": request.Account}})
updateUserMulti(bson.M{"mchid": auth.MchId, "type": PAYTYPE_ALIPAY, "status": bson.M{"$ne": UserFreeze}}, bson.M{"$set": bson.M{"status": request.Status}})
updateToken(auth.MchId, PAYTYPE_ALIPAY, bson.M{"$set": bson.M{"status": AuthStatusSucc, "account": request.Account}})
updateUserMulti(bson.M{"mchid": auth.MchId, "type": PAYTYPE_ALIPAY, "status": bson.M{"$ne": UserFreeze}}, bson.M{"$set": bson.M{"status": UserSucc}})
//验证权限是否真实开通
trade := TradeResult{}
A.TradeInfo(&TradeRequest{r: rsys{mchid: auth.MchId}, TradeId: "test123"}, &trade)
if trade.Code == AuthErr {
//撤销
updateToken(auth.MchId, PAYTYPE_ALIPAY, bson.M{"$set": bson.M{"status": AuthStatusWaitSigned, "account": request.Account}})
updateUserMulti(bson.M{"mchid": auth.MchId, "type": PAYTYPE_ALIPAY, "status": bson.M{"$ne": UserFreeze}}, bson.M{"$set": bson.M{"status": UserWaitVerify}})
resp.Code = AuthErr
}
return nil
}

Expand Down Expand Up @@ -86,18 +96,12 @@ func (A *AliPay) BarCodePay(request *BarCodePayRequest, resp *TradeResult) error
sysParams["method"] = "alipay.trade.pay"
sysParams["biz_content"] = string(jsonEncode(params))
//设置子商户数据
user := getUser(request.UserId, PAYTYPE_ALIPAY)
fmt.Printf("%+v,%v,\n", user, request.UserId)
if user.Status != UserSucc {
resp.Code = AuthErr
return nil
}
//获取授权
auth := getToken(user.MchId, PAYTYPE_ALIPAY)
auth := A.token(request.UserId, request.r.mchid)
if auth.Status != AuthStatusSucc {
resp.Code = AuthErrNotSigned
return nil
}
request.r.mchid = auth.MchId
sysParams["app_auth_token"] = auth.Token
sysParams["sign"] = base64Encode(AliPaySigner(sysParams))
//请求并除错
Expand Down Expand Up @@ -148,7 +152,7 @@ func (A *AliPay) BarCodePay(request *BarCodePayRequest, resp *TradeResult) error
//循环
//获取一次一直到成功
for getNowSec()-request.r.time < 30 {
A.TradeInfo(&TradeRequest{OutTradeId: request.OutTradeId, r: request.r, UserId: request.UserId}, &trade)
A.TradeInfo(&TradeRequest{OutTradeId: request.OutTradeId, r: request.r}, &trade)
if trade.Code == 0 && trade.Data.Status == TradeStatusSucc {
//订单存在且支付
//取消撤销
Expand Down Expand Up @@ -187,13 +191,13 @@ func (A *AliPay) BarCodePay(request *BarCodePayRequest, resp *TradeResult) error
Status: TradeStatusSucc,
TradeId: tmpresult["trade_no"].(string),
}
resp.Code = 0
resp.Code = Succ
saveTrade(resp.Data)
}
//撤销
if needCancel {
response := Response{}
A.Cancel(&TradeRequest{OutTradeId: request.OutTradeId, UserId: request.UserId}, &response)
A.Cancel(&TradeRequest{OutTradeId: request.OutTradeId}, &response)
}
return nil
}
Expand All @@ -204,19 +208,15 @@ func (A *AliPay) Refund(request *RefundRequest, resp *TradeResult) error {
if request.r.time == 0 {
request.r.time = getNowSec()
}
user := getUser(request.UserId, PAYTYPE_ALIPAY)
if user.Status != UserSucc {
resp.Code = AuthErr
return nil
}
//获取授权
auth := getToken(user.MchId, PAYTYPE_ALIPAY)
auth := A.token(request.UserId, request.r.mchid)
if auth.Status != AuthStatusSucc {
resp.Code = AuthErrNotSigned
return nil
}
request.r.mchid = auth.MchId
trade := TradeResult{}
A.TradeInfo(&TradeRequest{r: request.r, UserId: request.UserId, OutTradeId: request.OutTradeId}, &trade)
A.TradeInfo(&TradeRequest{r: request.r, OutTradeId: request.OutTradeId}, &trade)
if trade.Code != 0 {
resp.Code = trade.Code
resp.SourceData = trade.SourceData
Expand All @@ -229,7 +229,7 @@ func (A *AliPay) Refund(request *RefundRequest, resp *TradeResult) error {
params := map[string]interface{}{
"out_trade_no": request.OutTradeId,
"trade_no": request.TradeId,
"out_request_no": request.RefundId,
"out_request_no": request.OutRefundId,
"refund_reason": request.Memo,
"refund_amount": float64(request.Amount) / 100.0,
}
Expand Down Expand Up @@ -292,17 +292,13 @@ func (A *AliPay) Cancel(request *TradeRequest, resp *Response) error {
if request.r.time == 0 {
request.r.time = getNowSec()
}
user := getUser(request.UserId, PAYTYPE_ALIPAY)
if user.Status != UserSucc {
resp.Code = AuthErr
return nil
}
//获取授权
auth := getToken(user.MchId, PAYTYPE_ALIPAY)
auth := A.token(request.UserId, request.r.mchid)
if auth.Status != AuthStatusSucc {
resp.Code = AuthErrNotSigned
return nil
}
request.r.mchid = auth.MchId
params := map[string]interface{}{
"out_trade_no": request.OutTradeId,
"trade_no": request.TradeId,
Expand Down Expand Up @@ -350,17 +346,13 @@ func (A *AliPay) TradeInfo(request *TradeRequest, resp *TradeResult) error {
if request.r.time == 0 {
request.r.time = getNowSec()
}
user := getUser(request.UserId, PAYTYPE_ALIPAY)
if user.Status != UserSucc {
resp.Code = AuthErr
return nil
}
//获取授权
auth := getToken(user.MchId, PAYTYPE_ALIPAY)
auth := A.token(request.UserId, request.r.mchid)
if auth.Status != AuthStatusSucc {
resp.Code = AuthErrNotSigned
return nil
}
request.r.mchid = auth.MchId
q := bson.M{"source": PAYTYPE_ALIPAY}
if request.OutTradeId != "" {
q["outtradeid"] = request.OutTradeId
Expand All @@ -369,10 +361,6 @@ func (A *AliPay) TradeInfo(request *TradeRequest, resp *TradeResult) error {
q["tradeid"] = request.TradeId
}
trade := getTrade(q)
/*if trade.Id == "" {
resp.Code = TradeErrNotFound
return nil
}*/
params := map[string]interface{}{
"out_trade_no": request.OutTradeId,
"trade_no": request.TradeId,
Expand Down Expand Up @@ -565,7 +553,6 @@ func (A *AliPay) request(url string, okey string) (interface{}, int, error) {
//需重试
return nil, -1, err
}
fmt.Println(string(body))
result := map[string]interface{}{}
if err := jsonDecode(body, &result); err != nil {
return nil, 0, err
Expand Down Expand Up @@ -606,6 +593,18 @@ func (A *AliPay) errorCheck(data map[string]interface{}) (int, error) {
}
}

func (a *AliPay) token(userid, mchid string) authBase {
auth := authBase{}
if mchid == "" {
user := getUser(userid, PAYTYPE_ALIPAY)
if user.Status != UserSucc {
return auth
}
mchid = user.MchId
}
return getToken(mchid, PAYTYPE_ALIPAY)
}

/*组装系统级请求参数*/
func (a *AliPay) sysParams() map[string]string {
return map[string]string{
Expand Down
12 changes: 11 additions & 1 deletion databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ func saveUser(user User) error {

//获取用户信息
func getUser(userid string, t string) User {
return getUserQuery(bson.M{"userid": userid, "type": t})
}
func getUserQuery(q bson.M) User {
session := DBPool.Get()
defer session.Close()
user := User{}
session.DB().C("user").Find(bson.M{"userid": userid, "type": t}).One(&user)
session.DB().C("user").Find(q).One(&user)
return user
}

Expand Down Expand Up @@ -61,6 +64,13 @@ func saveToken(auth authBase) error {
return session.DB().C("auth").Insert(auth)
}

//删除授权
func deleteToken(mchid, t string) error {
session := DBPool.Get()
defer session.Close()
return session.DB().C("auth").Remove(bson.M{"mchid": mchid, "type": t})
}

//获取交易
func getTrade(q bson.M) Trade {
session := DBPool.Get()
Expand Down
11 changes: 11 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type Status int

const (
Succ = 0 //成功
AuthErr = 9001 //授权错误
AuthErrNotSigned = 9002 //未签约

Expand Down Expand Up @@ -53,6 +54,7 @@ const (
type rsys struct {
retry int
time int64
mchid string
}

//条码支付请求
Expand All @@ -76,6 +78,15 @@ type WapPayRequest struct {
ItemDes string //商品表述
ShopId string //店铺ID
Ex string //共用回传参数
UserId string
IPAddr string
Scene Scene //场景
}

type Scene struct {
//详情看wxpay的统一下单中的scene
Url string //请求地址
Name string //请求名称
}

//支付单详情
Expand Down
11 changes: 9 additions & 2 deletions rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ func initAliPay() {
}
var err error
if ali.AppId, err = config.GetString("appid"); err != nil {
log.Fatal("Init Error:Not Found wxpay:appid")
log.Fatal("Init Error:Not Found alipay:appid")
}
if ali.Url, err = config.GetString("url"); err != nil {
log.Fatal("Init Error:Not Found wxpay:url")
log.Fatal("Init Error:Not Found alipay:url")
}
if ali.NotifyUrl, err = config.GetString("notify"); err != nil {
log.Fatal("Init Error:Not Found alipay:notify_url")
}

ali.Init()
}
func initWXPay() {
Expand All @@ -98,5 +102,8 @@ func initWXPay() {
if wx.ApiKey, err = config.GetString("key"); err != nil {
log.Fatal("Init Error:Not Found wxpay:apikey")
}
if wx.NotifyUrl, err = config.GetString("notify"); err != nil {
log.Fatal("Init Error:Not Found wxpay:notify_url")
}
wx.Init()
}
4 changes: 3 additions & 1 deletion user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ppp

import "gopkg.in/mgo.v2/bson"
import (
"gopkg.in/mgo.v2/bson"
)

const (
FC_ACCOUNT_REGIST string = "Account.Regist"
Expand Down
Loading

0 comments on commit 17858b9

Please sign in to comment.