浏览代码

fix: sync pending virtual recharges

Codex 1 月之前
父节点
当前提交
95fa8c598b

+ 24 - 0
go/gopath/src/fohow.com/apps/controllers/pay_controller/virtual_pay_controller.go

@@ -174,6 +174,30 @@ func (self *PayController) GetVirtualRechargeStatus() {
 	self.ServeJSON()
 }
 
+// 同步当前用户未完成的余额虚拟支付充值订单
+func (self *PayController) SyncVirtualRechargePending() {
+	wxUser := self.GetCurrentWxUser(true)
+	if wxUser == nil {
+		self.ReturnError(403, apps.NoExist, "", nil)
+	}
+	orders := balance_model.GetPendingVirtualRechargeCashOrdersByWxUId(wxUser.Id, 10)
+	syncedCount := 0
+	for _, order := range orders {
+		if self.syncVirtualRechargePaid(order, wxUser.Openid) {
+			paidOrder := balance_model.GetRechargeCashOrderByOId(order.OrderId, false)
+			if paidOrder != nil && paidOrder.State == 1 {
+				syncedCount++
+			}
+		}
+	}
+	type Ret struct {
+		PendingCount int `json:"pending_count"`
+		SyncedCount  int `json:"synced_count"`
+	}
+	self.Data["json"] = &Ret{PendingCount: len(orders), SyncedCount: syncedCount}
+	self.ServeJSON()
+}
+
 // 微信小程序虚拟支付推送回调
 func (self *PayController) VirtualRechargeNotify() {
 	if echo := self.GetString("echostr"); echo != "" {

+ 13 - 0
go/gopath/src/fohow.com/apps/models/balance_model/cash_balance.go

@@ -391,6 +391,19 @@ func MarkRechargeCashOrderPaid(orderId, tradeNo, payWay string, paiedAt int64) (
 	return rows > 0, nil
 }
 
+func GetPendingVirtualRechargeCashOrdersByWxUId(wxUId int64, limit int) (orders []*RechargeCashOrder) {
+	if limit <= 0 {
+		limit = 10
+	}
+	tbn := new(RechargeCashOrder).TableName()
+	sql := fmt.Sprintf("SELECT * FROM `%s` WHERE `wx_user_id`=? AND `pay_way`=? AND (`state` IS NULL OR `state`<>1) AND `created_at`>=DATE_SUB(NOW(), INTERVAL 2 DAY) ORDER BY `id` DESC LIMIT ?", tbn)
+	if _, err := orm.NewOrm().Raw(sql, wxUId, PAY_WAY_TYPE_RECHARGE_VIRTUAL, limit).QueryRows(&orders); err != nil {
+		beego.BeeLogger.Error("GetPendingVirtualRechargeCashOrdersByWxUId wx_uid=[%d] err=[%s]", wxUId, err)
+		return nil
+	}
+	return orders
+}
+
 func GetRechargeCashOrderByOId(oId string, useCache bool) *RechargeCashOrder {
 	k := fmt.Sprintf("balance_model.GetRechargeCashOrderByOId[%s]", oId)
 	if useCache {

+ 1 - 0
go/gopath/src/fohow.com/routers/routes.go

@@ -139,6 +139,7 @@ func init() {
 	beego.Router("/v1/virtual_pay/recharge", &pay_controller.PayController{}, "post:CreateVirtualRecharge")
 	//查询虚拟支付充值余额订单状态
 	beego.Router("/v1/virtual_pay/recharge/status", &pay_controller.PayController{}, "get:GetVirtualRechargeStatus")
+	beego.Router("/v1/virtual_pay/recharge/sync_pending", &pay_controller.PayController{}, "get:SyncVirtualRechargePending")
 	//虚拟支付充值余额回调
 	beego.Router("/v1/virtual_pay/recharge/notify", &pay_controller.PayController{}, "post,get:VirtualRechargeNotify")
 	//----------- 文章相关 -----------