129 lines
3.0 KiB
Go
129 lines
3.0 KiB
Go
package cmb
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"time"
|
|
err2 "voucher/api/err"
|
|
v1 "voucher/api/v1"
|
|
"voucher/internal/biz/bo"
|
|
"voucher/internal/biz/vo"
|
|
)
|
|
|
|
func (v *Cmb) Notify(ctx context.Context, order *bo.OrderBo) (*bo.OrderNotifyBo, error) {
|
|
|
|
if order.Status.IsCanNotify() {
|
|
return nil, fmt.Errorf("订单状态不允许通知,orderNo:%s,orderStatusText:%s", order.OrderNo, order.Status.GetText())
|
|
}
|
|
|
|
event, err := order.Status.GetOrderNotifyEvent()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req := &bo.OrderNotifyBo{
|
|
OrderNo: order.OrderNo,
|
|
NotifyUrl: order.NotifyUrl,
|
|
Channel: order.Channel,
|
|
Event: event,
|
|
Type: order.Type,
|
|
}
|
|
|
|
request, orderNotify, err := v.notifyCreate(ctx, order, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
reply, err := v.CmbMixRepo.Request(ctx, request, order.NotifyUrl)
|
|
if err != nil {
|
|
return orderNotify, v.notifyFail(ctx, orderNotify.ID, err.Error())
|
|
}
|
|
|
|
if err = v.CmbMixRepo.VerifyResponse(ctx, reply); err != nil {
|
|
log.Errorf("回调通知招行返回验证结果发生错误,rep:%+v error:%s", reply, err.Error())
|
|
return orderNotify, v.notifyFail(ctx, orderNotify.ID, err.Error())
|
|
}
|
|
|
|
replyJson, _ := json.Marshal(reply)
|
|
|
|
if reply.RespCode == vo.CmbResponseStatusSuccess.GetValue() {
|
|
return orderNotify, v.notifySuccess(ctx, orderNotify.ID, string(replyJson))
|
|
}
|
|
|
|
return orderNotify, v.notifyFail(ctx, orderNotify.ID, reply.RespMsg)
|
|
}
|
|
|
|
func (v *Cmb) bizContent(_ context.Context, order *bo.OrderBo, orderNotify *bo.OrderNotifyBo) (string, error) {
|
|
|
|
cmbStatus, err := orderNotify.Event.GetCmbStatusText()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
req := &v1.CmbNotifyRequest{
|
|
Ticket: orderNotify.OrderNo,
|
|
Status: cmbStatus.GetValue(),
|
|
TransDate: time.Now().Format("20060102150405"),
|
|
OrgNo: v.bc.Cmb.OrgNo,
|
|
Attach: order.Attach,
|
|
Ext: "",
|
|
}
|
|
|
|
bizJsonBytes, err := json.Marshal(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(bizJsonBytes), nil
|
|
}
|
|
|
|
func (v *Cmb) notifyCreate(ctx context.Context, order *bo.OrderBo, req *bo.OrderNotifyBo) (*v1.CmbRequest, *bo.OrderNotifyBo, error) {
|
|
|
|
bizContent, err := v.bizContent(ctx, order, req)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
request, err := v.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
|
FuncName: vo.CmbNotifyFuncName,
|
|
BizContent: bizContent,
|
|
})
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
requestBytes, err := json.Marshal(request)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
req.Request = string(requestBytes)
|
|
|
|
orderNotify, err := v.OrderNotifyRepo.Create(ctx, req)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return request, orderNotify, err
|
|
}
|
|
|
|
func (v *Cmb) notifySuccess(ctx context.Context, notifyId uint64, responses string) error {
|
|
|
|
if len(responses) == 0 {
|
|
responses = "{}"
|
|
}
|
|
|
|
return v.OrderNotifyRepo.Success(ctx, notifyId, responses)
|
|
}
|
|
|
|
func (v *Cmb) notifyFail(ctx context.Context, notifyId uint64, errMsg string) error {
|
|
|
|
if err := v.OrderNotifyRepo.Fail(ctx, notifyId, errMsg); err != nil {
|
|
return err
|
|
}
|
|
|
|
return err2.ErrorNeedRetryNotify(errMsg)
|
|
}
|