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.
93 lines
2.6 KiB
93 lines
2.6 KiB
package pay
|
|
|
|
import (
|
|
"yunyan/comm"
|
|
"yunyan/pb"
|
|
"yunyan/sys/pay/wechat"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// @Summary 处理微信支付异步通知 [内部RPC]
|
|
// @Description 接收由HTTP回调网关转发来的已验证、解密的支付通知,并处理业务逻辑
|
|
// @Tags Pay
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param notify body pb.PayWeChatNotifyReq true "已解密的微信支付通知"
|
|
// @Success 200 {object} comm.HttpResult "处理结果"
|
|
// @Router /api/home/pay_wechatnotify [post]
|
|
func (this *apiComp) WeChatNotify(session comm.IUserSession, req *pb.PayWeChatNotifyReq) (resp []byte, errdata *pb.ErrorData) {
|
|
var (
|
|
transaction *wechat.WxPayTransaction
|
|
user *pb.DBUser
|
|
model *pb.DBPayOrder
|
|
err error
|
|
)
|
|
|
|
if transaction, err = wechat.DecryptWxPayNotify(
|
|
req.Resource.AssociatedData,
|
|
req.Resource.Nonce,
|
|
req.Resource.Ciphertext,
|
|
); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_PayNotifyDecryptFail,
|
|
Message: "解密微信支付通知失败:" + err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
// 3. 使用解密后的交易数据处理业务(如更新订单状态)
|
|
fmt.Printf("商户订单号: %s\n", transaction.OutTradeNo)
|
|
fmt.Printf("支付状态: %s\n", transaction.TradeState)
|
|
fmt.Printf("支付金额: %d 分\n", transaction.Amount.Total)
|
|
|
|
if model, err = this.module.model.getPayOrder(transaction.OutTradeNo); err != nil {
|
|
resp = []byte(utils.ToString(map[string]string{
|
|
"code": "Fail",
|
|
"message": fmt.Sprintf("订单不存在: %s", transaction.OutTradeNo),
|
|
}))
|
|
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()
|
|
err = this.deliverResourceByOrder(user, model)
|
|
if 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
|
|
}
|
|
|