package pay import ( "yunyan/comm" "yunyan/pb" "yunyan/sys/pay/alipay" "yunyan/utils" "fmt" "net/url" "time" ) // @Summary 处理微信支付异步通知 [内部RPC] // @Description 接收由HTTP回调网关转发来的已验证、解密的支付通知,并处理业务逻辑 // @Tags Pay // @Accept json // @Produce json // @Security BearerAuth // @Param notify body pb.PayAlipayNotifyReq true "支付宝异步通知参数" // @Success 200 {object} comm.HttpResult "处理结果" // @Router /api/home/pay_alipaynotify [post] func (this *apiComp) AlipayNotify(session comm.IUserSession, req *pb.PayAlipayNotifyReq) (resp []byte, errdata *pb.ErrorData) { var ( ok bool model *pb.DBPayOrder user *pb.DBUser err error ) params := alipayNotifyToUrlValues(req) this.module.Debugf("AlipayNotify params:%v", params) // 2. 验证并解密支付通知 if ok, err = alipay.DecryptWxPayNotify(params); !ok { errdata = &pb.ErrorData{ Code: pb.ErrorCode_PayNotifyDecryptFail, Message: "解密微信支付通知失败:" + err.Error(), } return } // 3. 使用解密后的交易数据处理业务(如更新订单状态) fmt.Printf("商户订单号: %s\n", req.OutTradeNo) fmt.Printf("支付状态: %s\n", req.TradeStatus) fmt.Printf("支付金额: %s 元\n", req.TotalAmount) if model, err = this.module.model.getPayOrder(req.OutTradeNo); err != nil { resp = []byte(utils.ToString(map[string]string{ "code": "Fail", "message": fmt.Sprintf("订单不存在: %s", req.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 } // 将 PayAlipayNotifyReq PB对象 转换为 url.Values(不判断空值,完整写入所有字段) func alipayNotifyToUrlValues(notify *pb.PayAlipayNotifyReq) url.Values { values := url.Values{} // 直接映射所有字段,不判断空值 values.Set("app_id", notify.AppId) values.Set("auth_app_id", notify.AuthAppId) values.Set("buyer_id", notify.BuyerId) values.Set("buyer_logon_id", notify.BuyerLogonId) values.Set("buyer_pay_amount", notify.BuyerPayAmount) values.Set("charset", notify.Charset) values.Set("fund_bill_list", notify.FundBillList) values.Set("gmt_create", notify.GmtCreate) values.Set("gmt_payment", notify.GmtPayment) values.Set("invoice_amount", notify.InvoiceAmount) values.Set("notify_id", notify.NotifyId) values.Set("notify_time", notify.NotifyTime) values.Set("notify_type", notify.NotifyType) values.Set("out_trade_no", notify.OutTradeNo) values.Set("point_amount", notify.PointAmount) values.Set("receipt_amount", notify.ReceiptAmount) values.Set("seller_email", notify.SellerEmail) values.Set("seller_id", notify.SellerId) values.Set("sign", notify.Sign) values.Set("sign_type", notify.SignType) values.Set("subject", notify.Subject) values.Set("total_amount", notify.TotalAmount) values.Set("trade_no", notify.TradeNo) values.Set("trade_status", notify.TradeStatus) values.Set("version", notify.Version) return values }