You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.2 KiB
64 lines
2.2 KiB
package pay
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/comm"
|
|
"yunyan/pb"
|
|
"yunyan/sys/pay/googleiap"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// GoogleIAPNotify 参考支付异步通知的风格:先校验,再更新订单并发货
|
|
// 说明:网关层解析 JSON/values 后封装到 PB 对象传入
|
|
func (this *apiComp) GoogleIAPNotify(session comm.IUserSession, req *pb.PayGoogleIAPNotifyReq) (resp []byte, errdata *pb.ErrorData) {
|
|
var (
|
|
user *pb.DBUser
|
|
)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
|
defer cancel()
|
|
var ok bool
|
|
var err error
|
|
if req.IsSubscription {
|
|
ok, err = googleiap.VerifySubscriptionPurchase(ctx, req.PackageName, req.ProductId, req.PurchaseToken)
|
|
} else {
|
|
ok, err = googleiap.VerifyProductPurchase(ctx, req.PackageName, req.ProductId, req.PurchaseToken)
|
|
}
|
|
if err != nil || !ok {
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_PayNotifyDecryptFail, Message: "Google IAP 校验失败:" + fmt.Sprintf("%v", err)}
|
|
return
|
|
}
|
|
|
|
// 查订单,按支付成功流程发货
|
|
model, err := this.module.model.getPayOrder(req.OrderId)
|
|
if err != nil {
|
|
resp = []byte(utils.ToString(map[string]string{"code": "Fail", "message": fmt.Sprintf("订单不存在: %s", req.OrderId)}))
|
|
return
|
|
}
|
|
if model.Status != pb.PayOrderStatus_PAY_ORDER_PENDING {
|
|
resp = []byte(utils.ToString(map[string]string{"code": "SUCCESS", "message": "成功"}))
|
|
return
|
|
}
|
|
// 4. 查询用户信息
|
|
if user, err = this.module.model.getUser(model.Uid); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: "查询用户信息失败:" + err.Error(),
|
|
}
|
|
return
|
|
}
|
|
model.Status = pb.PayOrderStatus_PAY_ORDER_PAID
|
|
model.PayTime = time.Now().Unix()
|
|
if err = this.deliverResourceByOrder(user, model); err != nil {
|
|
resp = []byte(utils.ToString(map[string]string{"code": "Fail", "message": fmt.Sprintf("发货资源失败: %v", err)}))
|
|
return
|
|
}
|
|
if err = this.module.model.updatePayOrder(model); err != nil {
|
|
resp = []byte(utils.ToString(map[string]string{"code": "Fail", "message": fmt.Sprintf("更新订单状态失败: %v", err)}))
|
|
return
|
|
}
|
|
resp = []byte(utils.ToString(map[string]string{"code": "SUCCESS", "message": "成功"}))
|
|
return
|
|
}
|
|
|