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.
 
 
 
 
 
 

63 lines
2.1 KiB

package pay
import (
"context"
"yunyan/comm"
"yunyan/pb"
"yunyan/sys/pay/appleiap"
"yunyan/utils"
"fmt"
"time"
)
// AppleIAPNotify 参考支付异步通知的风格:先校验,再更新订单并发货
// 说明:网关层解析 JSON/values 后封装到 PB 对象传入
func (this *apiComp) AppleIAPNotify(session comm.IUserSession, req *pb.PayAppleIAPNotifyReq) (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.UseTransactionId {
ok, err = appleiap.VerifyTransaction(ctx, req.ReceiptData)
} else {
ok, err = appleiap.VerifyReceipt(ctx, req.ReceiptData)
}
if err != nil || !ok {
errdata = &pb.ErrorData{Code: pb.ErrorCode_PayNotifyDecryptFail, Message: "Apple 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
}