cmb
This commit is contained in:
parent
6e71d8ae44
commit
14b2491d07
|
|
@ -0,0 +1,111 @@
|
|||
package cmb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"time"
|
||||
v1 "voucher/api/v1"
|
||||
"voucher/internal/biz/bo"
|
||||
"voucher/internal/biz/vo"
|
||||
)
|
||||
|
||||
func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) error {
|
||||
|
||||
if !order.Channel.IsWeChat() {
|
||||
return fmt.Errorf("暂不支持订单%s渠道%s回调通知处理", order.OrderNo, order.Channel.GetText())
|
||||
}
|
||||
|
||||
orderWechat, err := v.orderWechat(ctx, order, orderOutRequestNo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bizContent, err := v.bizContent(ctx, orderWechat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
request, err := v.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||
FuncName: vo.CmbNotifyFuncName,
|
||||
BizContent: bizContent,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
requestBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
orderNotify, err := v.OrderNotifyRepo.Create(ctx, &bo.OrderNotifyBo{
|
||||
OrderNo: orderWechat.OrderNo,
|
||||
OutRequestNo: orderWechat.OutRequestNo,
|
||||
Request: string(requestBytes),
|
||||
NotifyUrl: order.NotifyUrl,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
x, err := v.CmbMixRepo.Request(ctx, request, v.bc.Cmb.NotifyUrl)
|
||||
if err != nil {
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, err.Error())
|
||||
}
|
||||
|
||||
bizStr, err := v.CmbMixRepo.VerifyResponse(ctx, x)
|
||||
if err != nil {
|
||||
log.Errorf("NotifyConsume CmbMixRepo.VerifyResponse error:%s", err.Error())
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, err.Error())
|
||||
}
|
||||
|
||||
var s *v1.CmbNotifyReply
|
||||
if err = json.Unmarshal([]byte(bizStr), &s); err != nil {
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, err.Error())
|
||||
}
|
||||
|
||||
if s.RespCode != vo.CmbResponseStatusSuccess.GetValue() {
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, s.RespMsg)
|
||||
}
|
||||
|
||||
return v.OrderNotifyRepo.Success(ctx, orderNotify.ID, bizStr)
|
||||
}
|
||||
|
||||
func (v *Cmb) orderWechat(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) (*bo.OrderWechatBo, error) {
|
||||
|
||||
orderWechat, err := v.OrderWechatRepo.GetByOutRequestNo(ctx, orderOutRequestNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("根据订单号%s获取微信订单失败:%s", orderWechat.OrderNo, err.Error())
|
||||
}
|
||||
|
||||
if !orderWechat.Status.CanNotify() {
|
||||
return nil, fmt.Errorf("微信订单状态错误,不能通知:%s", order.Status.GetText())
|
||||
}
|
||||
|
||||
return orderWechat, err
|
||||
}
|
||||
|
||||
func (v *Cmb) bizContent(_ context.Context, orderWechat *bo.OrderWechatBo) (string, error) {
|
||||
|
||||
status, err := orderWechat.Status.GetCmbStatusText()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req := &v1.CmbNotifyRequest{
|
||||
Ticket: orderWechat.OrderNo,
|
||||
Status: status.GetValue(),
|
||||
TransDate: time.Now().Format("20060102150405"),
|
||||
OrgNo: v.bc.Cmb.OrgNo,
|
||||
Ext: "",
|
||||
}
|
||||
|
||||
bizJsonBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(bizJsonBytes), nil
|
||||
}
|
||||
|
|
@ -2,14 +2,10 @@ package cmb
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
v1 "voucher/api/v1"
|
||||
"voucher/internal/biz/bo"
|
||||
"voucher/internal/biz/vo"
|
||||
"voucher/internal/pkg/lock"
|
||||
|
|
@ -26,6 +22,11 @@ func (v *Cmb) OrderConsume(ctx context.Context, order *bo.OrderBo) (outRequestNo
|
|||
return outRequestNo, fmt.Errorf("订单渠道错误,%s", order.Channel.GetText())
|
||||
}
|
||||
|
||||
// 注册通知标签 order.MerchantNo 批次创建商户, order.BatchNo 商品批次号
|
||||
if err = v.registerNotifyTag(ctx, order.MerchantNo, order.BatchNo); err != nil {
|
||||
return outRequestNo, err
|
||||
}
|
||||
|
||||
if err = v.ing(ctx, order.ID); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -35,10 +36,6 @@ func (v *Cmb) OrderConsume(ctx context.Context, order *bo.OrderBo) (outRequestNo
|
|||
return
|
||||
}
|
||||
|
||||
if err = v.registerNotifyTag(ctx, orderWechat.StockCreatorMchid, orderWechat.StockID); err != nil {
|
||||
return outRequestNo, err
|
||||
}
|
||||
|
||||
couponId, err := v.WechatCpnRepo.Order(ctx, orderWechat)
|
||||
if err != nil {
|
||||
return outRequestNo, v.fail(ctx, order, orderWechat, err.Error())
|
||||
|
|
@ -173,102 +170,3 @@ func (v *Cmb) fail(ctx context.Context, order *bo.OrderBo, orderWechat *bo.Order
|
|||
|
||||
return v.OrderRepo.Fail(ctx, order.ID)
|
||||
}
|
||||
|
||||
func (v *Cmb) orderWechat(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) (*bo.OrderWechatBo, error) {
|
||||
|
||||
orderWechat, err := v.OrderWechatRepo.GetByOutRequestNo(ctx, orderOutRequestNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("根据订单号%s获取微信订单失败:%s", orderWechat.OrderNo, err.Error())
|
||||
}
|
||||
|
||||
if !orderWechat.Status.CanNotify() {
|
||||
return nil, fmt.Errorf("微信订单状态错误,不能通知:%s", order.Status.GetText())
|
||||
}
|
||||
|
||||
return orderWechat, err
|
||||
}
|
||||
|
||||
func (v *Cmb) bizContent(_ context.Context, orderWechat *bo.OrderWechatBo) (string, error) {
|
||||
|
||||
status, err := orderWechat.Status.GetCmbStatusText()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req := &v1.CmbNotifyRequest{
|
||||
Ticket: orderWechat.OrderNo,
|
||||
Status: status.GetValue(),
|
||||
TransDate: time.Now().Format("20060102150405"),
|
||||
OrgNo: v.bc.Cmb.OrgNo,
|
||||
Ext: "",
|
||||
}
|
||||
|
||||
bizJsonBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(bizJsonBytes), nil
|
||||
}
|
||||
|
||||
func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) error {
|
||||
|
||||
if !order.Channel.IsWeChat() {
|
||||
return fmt.Errorf("暂不支持订单%s渠道%s回调通知处理", order.OrderNo, order.Channel.GetText())
|
||||
}
|
||||
|
||||
orderWechat, err := v.orderWechat(ctx, order, orderOutRequestNo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bizContent, err := v.bizContent(ctx, orderWechat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
request, err := v.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||
FuncName: vo.CmbNotifyFuncName,
|
||||
BizContent: bizContent,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
requestBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
orderNotify, err := v.OrderNotifyRepo.Create(ctx, &bo.OrderNotifyBo{
|
||||
OrderNo: orderWechat.OrderNo,
|
||||
OutRequestNo: orderWechat.OutRequestNo,
|
||||
Request: string(requestBytes),
|
||||
NotifyUrl: order.NotifyUrl,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
x, err := v.CmbMixRepo.Request(ctx, request, v.bc.Cmb.NotifyUrl)
|
||||
if err != nil {
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, err.Error())
|
||||
}
|
||||
|
||||
bizStr, err := v.CmbMixRepo.VerifyResponse(ctx, x)
|
||||
if err != nil {
|
||||
log.Errorf("NotifyConsume CmbMixRepo.VerifyResponse error:%s", err.Error())
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, err.Error())
|
||||
}
|
||||
|
||||
var s *v1.CmbNotifyReply
|
||||
if err = json.Unmarshal([]byte(bizStr), &s); err != nil {
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, err.Error())
|
||||
}
|
||||
|
||||
if s.RespCode != vo.CmbResponseStatusSuccess.GetValue() {
|
||||
return v.OrderNotifyRepo.Fail(ctx, orderNotify.ID, s.RespMsg)
|
||||
}
|
||||
|
||||
return v.OrderNotifyRepo.Success(ctx, orderNotify.ID, bizStr)
|
||||
}
|
||||
|
|
@ -35,11 +35,3 @@ func (v *Cmb) Order(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.P
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
func (v *Cmb) Query(ctx context.Context, productNo string) (reps *bo.OrderCreateRepBo, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (v *Cmb) Notify(ctx context.Context, order *bo.OrderBo) (err error) {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue