xingzai 1 год назад
Родитель
Сommit
d36960b117
3 измененных файлов с 49 добавлено и 20 удалено
  1. 10 17
      controllers/order.go
  2. 2 2
      models/order/order.go
  3. 37 1
      services/wx_pay.go

+ 10 - 17
controllers/order.go

@@ -98,14 +98,14 @@ func (this *OrderController) AddByArticle() {
 		br.ErrMsg = "创建订单信息失败,Err:" + err.Error()
 		br.ErrMsg = "创建订单信息失败,Err:" + err.Error()
 		return
 		return
 	}
 	}
-	JsapiApiResp, err := services.ExampleJsapiApiServicePrepay(item, user.UnionId)
+	codeUrl, err := services.ExampleNativeApiServicePrepay(item)
 	if err != nil {
 	if err != nil {
 		br.Msg = "创建订单信息失败"
 		br.Msg = "创建订单信息失败"
-		br.ErrMsg = "创建订单信息失败,ExampleJsapiApiServicePrepayErr:" + err.Error()
+		br.ErrMsg = "创建订单信息失败,ExampleNativeApiServicePrepay:" + err.Error()
 		return
 		return
 	}
 	}
-	resp := new(order.WxJsapiApiResp)
-	resp.Detail = JsapiApiResp
+	resp := new(order.WxNativeApiResp)
+	resp.CodeUrl = codeUrl
 	resp.OrderCode = item.OrderCode
 	resp.OrderCode = item.OrderCode
 	br.Data = resp
 	br.Data = resp
 	br.Ret = 200
 	br.Ret = 200
@@ -208,20 +208,15 @@ func (this *OrderController) AddByActivity() {
 		item.OrderCode = oldOrderCode
 		item.OrderCode = oldOrderCode
 		err = order.UpdateCygxOrder(item, oldOrderCode)
 		err = order.UpdateCygxOrder(item, oldOrderCode)
 	}
 	}
-	JsapiApiResp, err := services.ExampleJsapiApiServicePrepay(item, user.UnionId)
+	codeUrl, err := services.ExampleNativeApiServicePrepay(item)
 	if err != nil {
 	if err != nil {
 		br.Msg = "创建订单信息失败"
 		br.Msg = "创建订单信息失败"
-		br.ErrMsg = "创建订单信息失败,ExampleJsapiApiServicePrepayErr:" + err.Error()
+		br.ErrMsg = "创建订单信息失败,ExampleNativeApiServicePrepay:" + err.Error()
 		return
 		return
 	}
 	}
-	resp := new(order.WxJsapiApiResp)
-	resp.Detail = JsapiApiResp
+	resp := new(order.WxNativeApiResp)
+	resp.CodeUrl = codeUrl
 	resp.OrderCode = item.OrderCode
 	resp.OrderCode = item.OrderCode
-	if err != nil {
-		br.Msg = "创建订单信息失败"
-		br.ErrMsg = "创建订单信息失败,Err:" + err.Error()
-		return
-	}
 	br.Data = resp
 	br.Data = resp
 	br.Ret = 200
 	br.Ret = 200
 	br.Success = true
 	br.Success = true
@@ -317,10 +312,8 @@ func (this *OrderController) UserOrderList() {
 		item.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
 		item.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
 		resp.List = append(resp.List, item)
 		resp.List = append(resp.List, item)
 	}
 	}
-
 	page := paging.GetPaging(currentIndex, pageSize, total)
 	page := paging.GetPaging(currentIndex, pageSize, total)
 	resp.Paging = page
 	resp.Paging = page
-
 	br.Data = resp
 	br.Data = resp
 	br.Ret = 200
 	br.Ret = 200
 	br.Success = true
 	br.Success = true
@@ -374,8 +367,8 @@ func (this *OrderController) OrderDetail() {
 	br.Msg = "获取成功"
 	br.Msg = "获取成功"
 }
 }
 
 
-// @Title 通过活动创建订单
-// @Description 通过活动创建订单接口
+// @Title 取消订单
+// @Description 取消订单接口
 // @Param	request	body models.ActivitySingnupRep true "type json string"
 // @Param	request	body models.ActivitySingnupRep true "type json string"
 // @Success Ret=200 {object} models.SignupStatus
 // @Success Ret=200 {object} models.SignupStatus
 // @router /cancel [post]
 // @router /cancel [post]

+ 2 - 2
models/order/order.go

@@ -7,8 +7,8 @@ import (
 	"time"
 	"time"
 )
 )
 
 
-type WxJsapiApiResp struct {
-	Detail    PrepayWithRequestPaymentResponse
+type WxNativeApiResp struct {
+	CodeUrl   string `comment:"微信支付二维码链接"`
 	OrderCode string `comment:"订单编号"`
 	OrderCode string `comment:"订单编号"`
 }
 }
 
 

+ 37 - 1
services/wx_pay.go

@@ -8,6 +8,7 @@ import (
 	"github.com/wechatpay-apiv3/wechatpay-go/core"
 	"github.com/wechatpay-apiv3/wechatpay-go/core"
 	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
 	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
 	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
 	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
+	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
 	payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
 	payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
 	"hongze/hongze_web_mfyx/models"
 	"hongze/hongze_web_mfyx/models"
 	"hongze/hongze_web_mfyx/models/order"
 	"hongze/hongze_web_mfyx/models/order"
@@ -17,7 +18,7 @@ import (
 )
 )
 
 
 const (
 const (
-	MchPKFileName              = "./utils/cert/apiclient_key.pem"
+	MchPKFileName              = "../hongze_mfyx/utils/cert/apiclient_key.pem"
 	Mchid                      = "1624495680"
 	Mchid                      = "1624495680"
 	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
 	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
 	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
 	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
@@ -36,6 +37,7 @@ func getWechatClient() (context.Context, *core.Client, error) {
 	opts := []core.ClientOption{
 	opts := []core.ClientOption{
 		option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
 		option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
 	}
 	}
+
 	client, err := core.NewClient(ctx, opts...)
 	client, err := core.NewClient(ctx, opts...)
 	if err != nil {
 	if err != nil {
 		log.Printf("new wechat pay client err:%s", err)
 		log.Printf("new wechat pay client err:%s", err)
@@ -92,6 +94,40 @@ func ExampleJsapiApiServicePrepay(orderDetail *order.CygxOrder, unionId string)
 	return
 	return
 }
 }
 
 
+//func init() {
+//	ExampleNativeApiService_Prepay()
+//}
+
+func ExampleNativeApiServicePrepay(orderDetail *order.CygxOrder) (codeUrl string, err error) {
+	defer func() {
+		if err != nil {
+			fmt.Println(err)
+			go utils.SendAlarmMsg(fmt.Sprint("生成预支付交易单失败 ExampleNativeApiServicePrepay, err:", err.Error(), "OrderCode:", orderDetail.OrderCode), 2)
+		}
+	}()
+	ctx, client, _ := getWechatClient()
+	svc := native.NativeApiService{Client: client}
+	resp, _, err := svc.Prepay(ctx,
+		native.PrepayRequest{
+			Appid:         core.String(utils.WxAppId),
+			Mchid:         core.String(Mchid),
+			Description:   core.String(orderDetail.SourceTitle),
+			OutTradeNo:    core.String(orderDetail.OrderCode),
+			TimeExpire:    core.Time(time.Now()),
+			Attach:        core.String(""),
+			NotifyUrl:     core.String(utils.WxPayJsapiNotifyUrl),
+			GoodsTag:      core.String(""),
+			SupportFapiao: core.Bool(false),
+			Amount: &native.Amount{
+				Currency: core.String("CNY"),
+				Total:    core.Int64(int64(orderDetail.OrderMoney * 100)), // 分
+			},
+		},
+	)
+	codeUrl = *resp.CodeUrl
+	return
+}
+
 type WechatPayCallback struct {
 type WechatPayCallback struct {
 	MchID          string    `json:"mchid"`
 	MchID          string    `json:"mchid"`
 	AppID          string    `json:"appid"`
 	AppID          string    `json:"appid"`