303 lines
7.5 KiB
Go
303 lines
7.5 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"time"
|
|
err2 "voucher/api/err"
|
|
v1 "voucher/api/v1"
|
|
"voucher/internal/biz/bo"
|
|
"voucher/internal/biz/kx"
|
|
"voucher/internal/biz/vo"
|
|
)
|
|
|
|
func (v *VoucherBiz) CmbOrder(ctx context.Context, request *v1.CmbRequest) (*v1.CmbReply, error) {
|
|
|
|
order, err := v.cmbOrder(ctx, request)
|
|
|
|
if err != nil {
|
|
return v.OrderFail(ctx, err)
|
|
}
|
|
|
|
reply, err := v.OrderSuccess(ctx, order.OrderNo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 通知kx
|
|
b, _ := json.Marshal(reply)
|
|
_ = v.bbToWx(ctx, order, string(b))
|
|
|
|
return reply, nil
|
|
}
|
|
|
|
func (c *VoucherBiz) cmbOrder(ctx context.Context, request *v1.CmbRequest) (*bo.OrderBo, error) {
|
|
|
|
bizContent, err := c.CmbMixRepo.OrderVerify(ctx, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctx2 := context.Background()
|
|
|
|
boReq := &bo.OrderCreateReqBo{
|
|
OutBizNo: bizContent.TransactionId,
|
|
ProductNo: bizContent.ActivityId,
|
|
Account: bizContent.CmbUid,
|
|
AppID: bizContent.AppId,
|
|
Attach: bizContent.Attach,
|
|
AccountType: vo.OrderAccountTypeOpenId,
|
|
Type: vo.OrderTypeCmb,
|
|
NotifyUrl: c.bc.Cmb.NotifyUrl,
|
|
}
|
|
|
|
order, err := c.Order(ctx2, boReq, bizContent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return order, nil
|
|
}
|
|
|
|
func (v *VoucherBiz) Order(ctx context.Context, req *bo.OrderCreateReqBo, cmbReq *v1.CmbOrderRequest) (order *bo.OrderBo, err error) {
|
|
|
|
product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo)
|
|
if err3 != nil {
|
|
return order, err3
|
|
}
|
|
|
|
order, err = v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo)
|
|
|
|
if err != nil && !err2.IsDbNotFound(err) {
|
|
return order, err
|
|
}
|
|
|
|
if order != nil {
|
|
|
|
if order.Status.IsFail() {
|
|
|
|
if err4 := v.orderRetry(ctx, order); err4 != nil {
|
|
return order, err4
|
|
}
|
|
}
|
|
|
|
return order, err
|
|
}
|
|
|
|
order, err = v.order(ctx, req, product, cmbReq)
|
|
if err != nil {
|
|
return order, err
|
|
}
|
|
|
|
return order, nil
|
|
}
|
|
|
|
func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.ProductBo, cmbReq *v1.CmbOrderRequest) (*bo.OrderBo, error) {
|
|
|
|
order, err := v.create(ctx, req, product)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 通知kx
|
|
_ = v.cmbToBB(ctx, cmbReq)
|
|
|
|
// 注册通知标签 order.MerchantNo 批次创建商户, order.BatchNo 商品批次号
|
|
if err = v.registerNotifyTag(ctx, order.MerchantNo, order.BatchNo); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 真实发放
|
|
voucherNo, err := v.WechatCpnRepo.Order(ctx, order)
|
|
if err != nil {
|
|
if err3 := v.fail(ctx, order, err); err3 != nil {
|
|
return nil, err3
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return order, v.success(ctx, order, voucherNo)
|
|
}
|
|
|
|
func (v *VoucherBiz) orderRetry(ctx context.Context, order *bo.OrderBo) error {
|
|
|
|
voucherNo, err := v.WechatCpnRepo.Order(ctx, order)
|
|
|
|
if err != nil {
|
|
if err3 := v.fail(ctx, order, err); err3 != nil {
|
|
return err3
|
|
}
|
|
return err
|
|
}
|
|
|
|
return v.success(ctx, order, voucherNo)
|
|
}
|
|
|
|
func (v *VoucherBiz) create(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.ProductBo) (*bo.OrderBo, error) {
|
|
|
|
o := &bo.OrderBo{
|
|
OrderNo: v.GenerateMixRepo.GeneratorString(ctx, fmt.Sprintf("%d%s", req.Type, req.OutBizNo)),
|
|
OutBizNo: req.OutBizNo,
|
|
ProductNo: req.ProductNo,
|
|
Account: req.Account,
|
|
AppID: req.AppID,
|
|
MerchantNo: product.MchId,
|
|
Channel: product.Channel,
|
|
BatchNo: product.BatchNo,
|
|
NotifyUrl: req.NotifyUrl,
|
|
AccountType: vo.OrderAccountTypeOpenId,
|
|
Type: req.Type,
|
|
Status: vo.OrderStatusIng, // 同步发放,状态至为发放中
|
|
Attach: req.Attach,
|
|
}
|
|
|
|
return v.OrderRepo.Create(ctx, o)
|
|
}
|
|
|
|
func (v *VoucherBiz) ing(ctx context.Context, id uint64) error {
|
|
return v.OrderRepo.Ing(ctx, id)
|
|
}
|
|
|
|
func (v *VoucherBiz) success(ctx context.Context, order *bo.OrderBo, voucherNo string) error {
|
|
order.VoucherNo = voucherNo
|
|
return v.OrderRepo.Success(ctx, order.ID, voucherNo)
|
|
}
|
|
|
|
func (v *VoucherBiz) fail(ctx context.Context, order *bo.OrderBo, errReq error) error {
|
|
|
|
if err := v.OrderRepo.Fail(ctx, order.ID, errReq.Error()); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err2.IsWechatAccountFail(errReq) || err2.IsWechatUserMaxCoupons(errReq) {
|
|
return nil // 过滤调该类型错误通知
|
|
}
|
|
|
|
return v.alarm(ctx, order, errReq.Error())
|
|
}
|
|
|
|
func (c *VoucherBiz) OrderSuccess(ctx context.Context, orderNo string) (*v1.CmbReply, error) {
|
|
|
|
bizReply := &v1.CmbOrderReply{
|
|
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
|
|
RespMsg: "成功",
|
|
CodeNo: orderNo,
|
|
}
|
|
|
|
replyBizContent, _ := json.Marshal(bizReply)
|
|
|
|
return c.GetResponse(ctx, replyBizContent)
|
|
}
|
|
|
|
func (c *VoucherBiz) OrderFail(ctx context.Context, err error) (*v1.CmbReply, error) {
|
|
|
|
se := errors.FromError(err)
|
|
|
|
if len(se.Reason) == 0 {
|
|
se.Reason = err2.CmbErr_CMB_UNKNOWN.String()
|
|
}
|
|
|
|
log.Errorf("order fail: %v", se)
|
|
|
|
bizReply := &v1.CmbOrderReply{
|
|
RespCode: vo.CmbResponseStatusFail.GetValue(),
|
|
RespMsg: se.Message,
|
|
CodeNo: "",
|
|
ThirdErrCode: se.Reason,
|
|
}
|
|
|
|
replyBizContent, _ := json.Marshal(bizReply)
|
|
|
|
return c.GetResponse(ctx, replyBizContent)
|
|
}
|
|
|
|
func (v *VoucherBiz) cmbToBB(ctx context.Context, cmbReq *v1.CmbOrderRequest) error {
|
|
|
|
req := &kx.CmbToBBRequest{
|
|
TransactionId: cmbReq.TransactionId,
|
|
ActivityId: cmbReq.ActivityId,
|
|
CmbUid: cmbReq.CmbUid,
|
|
CmbUidType: cmbReq.CmbUidType,
|
|
Timestamp: cmbReq.Timestamp,
|
|
AppId: cmbReq.AppId,
|
|
Attach: cmbReq.Attach,
|
|
}
|
|
|
|
return v.KxMixRepo.Request(ctx, req.GetSynNotice())
|
|
}
|
|
|
|
func (v *VoucherBiz) bbToWx(ctx context.Context, order *bo.OrderBo, cmbRes string) error {
|
|
|
|
wxRes, err := json.Marshal(map[string]string{"coupon_id": order.VoucherNo})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req := &kx.BBToWechatRequest{
|
|
StockId: order.BatchNo,
|
|
OutRequestNo: order.OrderNo,
|
|
AppId: order.AppID,
|
|
StockCreatorMhId: order.MerchantNo,
|
|
CouponValue: 0,
|
|
CouponMinimum: 0,
|
|
CouponId: order.VoucherNo,
|
|
WxRes: string(wxRes),
|
|
CmbRes: cmbRes,
|
|
TransactionId: order.OutBizNo,
|
|
}
|
|
|
|
return v.KxMixRepo.Request(ctx, req.GetSynNotice())
|
|
}
|
|
|
|
func (v *VoucherBiz) wxToBB(ctx context.Context, order *bo.OrderBo, wxReq *bo.WechatVoucherNotifyBo) error {
|
|
|
|
req := &kx.WechatToBBRequest{
|
|
ActivityId: order.BatchNo,
|
|
ActivityName: wxReq.PlainText.CouponName,
|
|
VoucherId: order.VoucherNo,
|
|
UserId: order.Account,
|
|
UseTime: wxReq.PlainText.ConsumeInformation.ConsumeTime,
|
|
UseAmount: wxReq.PlainText.ConsumeInformation.ConsumeAmount,
|
|
BizType: "V_USE",
|
|
RefundTime: "",
|
|
RefundAmount: "",
|
|
VoucherStatus: "",
|
|
OrderId: order.OutBizNo,
|
|
TradeNo: order.OrderNo,
|
|
GmtVoucherCreate: fmt.Sprintf("%d", order.ReceiveSuccessTime.Unix()),
|
|
}
|
|
|
|
inputFormat := time.RFC3339
|
|
|
|
if wxReq.PlainText.ConsumeInformation.ConsumeTime != "" {
|
|
useTime, _ := time.Parse(inputFormat, wxReq.PlainText.ConsumeInformation.ConsumeTime)
|
|
req.UseTime = fmt.Sprintf("%d", useTime.Unix())
|
|
}
|
|
|
|
return v.KxMixRepo.Request(ctx, req.GetSynNotice())
|
|
}
|
|
|
|
func (v *VoucherBiz) wxToBBRefund(ctx context.Context, order *bo.OrderBo) error {
|
|
|
|
req := &kx.WechatToBBRequest{
|
|
ActivityId: order.BatchNo,
|
|
ActivityName: "",
|
|
VoucherId: order.VoucherNo,
|
|
UserId: order.Account,
|
|
UseTime: "",
|
|
UseAmount: 0,
|
|
BizType: "V_REFUND",
|
|
RefundTime: "",
|
|
RefundAmount: "",
|
|
VoucherStatus: "",
|
|
OrderId: order.OutBizNo,
|
|
TradeNo: order.OrderNo,
|
|
GmtVoucherCreate: fmt.Sprintf("%d", order.ReceiveSuccessTime.Unix()),
|
|
}
|
|
|
|
return v.KxMixRepo.Request(ctx, req.GetSynNotice())
|
|
}
|