panghu 7 місяців тому
батько
коміт
b459e37e6f
3 змінених файлів з 88 додано та 7 видалено
  1. 6 2
      api/shop_order.go
  2. 5 5
      server/chinaums/chinaumsServer.go
  3. 77 0
      server/weChatServer.go

+ 6 - 2
api/shop_order.go

@@ -121,7 +121,9 @@ func (shopOrder ShopOrderController) GetOrderSn(c *gin.Context) {
 	}
 
 	chinaumsServer := chinaums.GetOrderSnServerInstance()
+	weChatServer := server.GetWeChatServerInstance()
 	var payUrl string
+	var notifyUrl string
 	if data.PayType == 1 { //微信支付
 		//查询当前用户openid
 		memberOpenIdModel := model.XMemberOpenID{}
@@ -137,12 +139,14 @@ func (shopOrder ShopOrderController) GetOrderSn(c *gin.Context) {
 		}
 		memberOpenId := memberOpenIdInfo.Openid
 		if isBank == 1 {
-			payUrl = chinaumsServer.ShopOrderWxH5Pay(shopMchidInfo, shopOrderModel.RsSn, shopOrderModel.PayMoney, "", memberOpenId)
+			payUrl = chinaumsServer.ShopOrderWxH5Pay(shopMchidInfo, shopOrderModel.RsSn, shopOrderModel.PayMoney, notifyUrl, memberOpenId)
+		} else {
+			wxPay = weChatServer.WeChatJsPay(shopOrderModel.RsSn, shopOrderModel.PayMoney, notifyUrl, '订单支付', memberOpenId)
 		}
 
 	} else { //支付宝支付
 		if isBank == 1 {
-			payUrl = chinaumsServer.ShopOrderAliH5Pay(shopMchidInfo, shopOrderModel.RsSn, shopOrderModel.PayMoney, "")
+			payUrl = chinaumsServer.ShopOrderAliH5Pay(shopMchidInfo, shopOrderModel.RsSn, shopOrderModel.PayMoney, notifyUrl)
 		}
 	}
 	shopOrder.responseSuccess(c, payUrl)

+ 5 - 5
server/chinaums/chinaumsServer.go

@@ -34,11 +34,11 @@ var (
 func GetOrderSnServerInstance() *ChinaumsServer {
 	chinaumsServerOnce.Do(func() {
 		chinaumsServerInstance = &ChinaumsServer{
-			appId:    viper.GetString("chinaums.appid"),
-			appKey:   viper.GetString("chinaums.appkey"),
-			appletId: viper.GetString("chinaums.appletid"),
-			wxAppId:  viper.GetString("chinaums.wxappid"),
-			signKey:  viper.GetString("chinaums.signkey"),
+			appId:    viper.GetString("chinaums.appId"),
+			appKey:   viper.GetString("chinaums.appKey"),
+			appletId: viper.GetString("chinaums.appletId"),
+			wxAppId:  viper.GetString("chinaums.wxAppId"),
+			signKey:  viper.GetString("chinaums.signKey"),
 		}
 	})
 	return chinaumsServerInstance

+ 77 - 0
server/weChatServer.go

@@ -0,0 +1,77 @@
+package server
+
+import (
+	"encoding/json"
+	"github.com/shopspring/decimal"
+	"github.com/spf13/viper"
+	"go_zh/pkg/http"
+	"sync"
+)
+
+type WeChatServer struct {
+	MchId      string
+	PrivateKey string
+	CertFile   string
+	KeyFile    string
+	AppId      string
+	AppSecret  string
+	AppletId   string
+	AppletKey  string
+}
+
+type AppletsOpenData struct {
+	Errormsg   string `json:"errmsg"`
+	Errorcode  int    `json:"errcode"`
+	Unionid    string `json:"unionid"`
+	SessionKey string `json:"session_key"`
+	Openid     string `json:"openid"`
+}
+
+var (
+	weChatServerInstance *WeChatServer
+	weChatServerOnce     sync.Once
+)
+
+// GetWeChatServerInstance 获取单例实例
+func GetWeChatServerInstance() *WeChatServer {
+	weChatServerOnce.Do(func() {
+		weChatServerInstance = &WeChatServer{
+			MchId:      viper.GetString("weChat.mchId"),
+			PrivateKey: viper.GetString("weChat.privateKey"),
+			CertFile:   viper.GetString("weChat.certFile"),
+			KeyFile:    viper.GetString("weChat.keyFile"),
+			AppId:      viper.GetString("weChat.appId"),
+			AppSecret:  viper.GetString("weChat.appSecret"),
+			AppletId:   viper.GetString("weChat.appletId"),
+			AppletKey:  viper.GetString("weChat.appletKey"),
+		}
+	})
+	return weChatServerInstance
+}
+
+// GetAppletsOpenId 微信小程序获取登录openId
+func (wx *WeChatServer) GetAppletsOpenId(code string) (AppletsOpenData, error) {
+
+	url := "https://api.weixin.qq.com/sns/jscode2session"
+	//发起http get请求
+
+	var appletsOpenRes AppletsOpenData
+	requestData := http.RequestData{}
+	err := requestData.ConstructRequest(url)
+	if err != nil {
+		return appletsOpenRes, err
+	}
+
+	requestData.SetData("grant_type", "authorization_code")
+	requestData.SetData("appid", wx.AppletId)
+	requestData.SetData("secret", wx.AppletKey)
+	requestData.SetData("js_code", code)
+
+	ret, err := requestData.GetRequest()
+
+	_ = json.Unmarshal(ret, &appletsOpenRes)
+	return appletsOpenRes, nil
+}
+func (wx *WeChatServer) WeChatJsPay(orderSn string, payMoney decimal.Decimal, notifyUrl string, boby string, wxOpenid string) {
+
+}