Pārlūkot izejas kodu

build many wx pay function

abiao 5 gadi atpakaļ
vecāks
revīzija
a859fd7c3f

+ 3 - 0
go/gopath/src/fohow.com/conf/app.conf

@@ -114,6 +114,9 @@ WxMchKeyFile = "/opt/wxpay/fohow_wx/apiclient_key.pem"
 TakeWxMchCertFile= "/opt/wxpay/take_wx/apiclient_cert.pem"
 TakeWxMchKeyFile = "/opt/wxpay/take_wx/apiclient_key.pem"
 
+#乐福优选商户号证书
+LehuMchCertFile= "/opt/wxpay/wx/apiclient_cert.pem"
+LehuMchKeyFile = "/opt/wxpay/wx/apiclient_key.pem"
 
 #######################################################################
 [prod]

+ 77 - 229
go/gopath/src/fohow.com/libs/wx_mp/pay.go

@@ -23,23 +23,17 @@ import (
 )
 
 const (
-	appId = "wx84f6c33a9fa3977d" //小程序APPID
-
+	//玖玖好购
 	mchId  = "1604238155"
 	apiKey = "w7gb9ur5hb9g5326vy3sr0q4cobmrpgq"
 
-	//微信公众号使用小程序的商户号和api加密
-	gzhMchId  = "1604238155"
-	gzhApiKey = "w7gb9ur5hb9g5326vy3sr0q4cobmrpgq"
-	gzhAppId  = "wx6c8e31c921527d39" //公众号APPID
-
-	//微信公众号-独立商户号
-	wxMchId  = "1604238155"
-	wxApiKey = "w7gb9ur5hb9g5326vy3sr0q4cobmrpgq"
+	//乐福优选
+	lehuMchId  = "1581904571"
+	lehuApiKey = "r1sbdza2mgs2zo3x2u6w8plgh91z0gqq"
 
-	//提现商户号
-	takeWxMchId  = "1424016502"
-	takeWxApiKey = "8r76o4CWzgWqTTlrNwtnWH9URpdXyveO"
+	//微信公众号使用小程序的商户号和api加密
+	gzhMchId  = ""
+	gzhApiKey = ""
 
 	PAY_SUCCESS = "SUCCESS"
 	PAY_FAIL    = "FAIL"
@@ -52,29 +46,30 @@ const (
 	PAY_OPTION_CHECK = "OPTION_CHECK"
 )
 
-var mchProxy = mch.NewProxy(appId, mchId, apiKey, nil)
-var gzhMchProxy = mch.NewProxy(gzhAppId, gzhMchId, gzhApiKey, nil)
+//小程序端微信支付client
+func getMchProxyInitClient() *mch.Proxy {
 
-//小程序商户
-var tlsHttpCilent, _ = mch.NewTLSHttpClient(
-	beego.AppConfig.String("MchCertFile"),
-	beego.AppConfig.String("MchKeyFile"))
-var mchTLSProxy = mch.NewProxy(appId, mchId, apiKey, tlsHttpCilent)
+	appId := beego.AppConfig.String("WxFohowXcxAppId") //获取小程序渠道appId
 
-//小程序提现商户
-var takeTlsHttpCilent, _ = mch.NewTLSHttpClient(
-	beego.AppConfig.String("TakeWxMchCertFile"),
-	beego.AppConfig.String("TakeWxMchKeyFile"))
-var takeMchTLSProxy = mch.NewProxy(appId, takeWxMchId, takeWxApiKey, takeTlsHttpCilent)
+	mchProxy := mch.NewProxy(appId, mchId, apiKey, nil)
+	return mchProxy
+}
+func getMchTLSProxyInitClient() *mch.Proxy {
+	appId := beego.AppConfig.String("WxFohowXcxAppId") //获取小程序渠道appId
+	var tlsHttpCilent, _ = mch.NewTLSHttpClient(
+		beego.AppConfig.String("MchCertFile"),
+		beego.AppConfig.String("MchKeyFile"))
+	mchTLSProxy := mch.NewProxy(appId, mchId, apiKey, tlsHttpCilent)
+	return mchTLSProxy
+}
 
-//公众号商户
-var wxMchProxy = mch.NewProxy(beego.AppConfig.String("WxMPAppId"),
-	wxMchId, wxApiKey, nil)
-var wxTlsHttpCilent, _ = mch.NewTLSHttpClient(
-	beego.AppConfig.String("WxMchCertFile"),
-	beego.AppConfig.String("WxMchKeyFile"))
-var wxMchTLSProxy = mch.NewProxy(beego.AppConfig.String("WxMPAppId"),
-	wxMchId, wxApiKey, wxTlsHttpCilent)
+// 商户渠道信息
+type MechantPayInfo struct {
+	MchId       string //商户号
+	ApiKey      string //api密钥
+	MchKeyFile  string //私钥路径
+	MchCertFile string //证书路径
+}
 
 // 支付结果通知参数
 type PayResult struct {
@@ -112,25 +107,33 @@ type PayResult struct {
 }
 
 // 获取jsSDK微信支付需要的数据
-func GetPayData(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (ret map[string]string) {
-	prepayId := getPayPrepayId(openid, outTradeNo, totalPrice, body, notifyUrl, remoteIp)
-	u := uuid.NewV4().String()
-	us := strings.Split(u, "-")
-	nonce_str := strings.Join(us, "")
-	ret = map[string]string{
-		"appId":     appId,
-		"timeStamp": fmt.Sprintf("%d", time.Now().Unix()),
-		"package":   fmt.Sprintf("prepay_id=%s", prepayId),
-		"signType":  "MD5",
-		"nonceStr":  nonce_str}
-	ret["paySign"] = mch.Sign(ret, apiKey, nil)
+func GetMechantInfo(mechantCode string) (ret MechantPayInfo) {
+	switch mechantCode {
+	case "fohow":
+		ret.MchId = mchId
+		ret.ApiKey = apiKey
+		ret.MchKeyFile = beego.AppConfig.String("WxMchKeyFile")
+		ret.MchCertFile = beego.AppConfig.String("MchCertFile")
+	case "lehu":
+		ret.MchId = lehuMchId
+		ret.ApiKey = lehuApiKey
+		ret.MchKeyFile = beego.AppConfig.String("LehuMchKeyFile")
+		ret.MchCertFile = beego.AppConfig.String("LehuMchCertFile")
+	default:
+		ret.MchId = mchId
+		ret.ApiKey = apiKey
+		ret.MchKeyFile = beego.AppConfig.String("WxMchKeyFile")
+		ret.MchCertFile = beego.AppConfig.String("MchCertFile")
+	}
 	return ret
 
 }
 
 // 获取jsSDK微信支付需要的数据
-func GetPayDataLimitPay(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (ret map[string]string) {
-	prepayId := getPayPrepayIdLimitPay(openid, outTradeNo, totalPrice, body, notifyUrl, remoteIp)
+func GetPayData(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp, payCode string) (ret map[string]string) {
+	appId := beego.AppConfig.String("WxFohowXcxAppId") //获取小程序渠道appId
+	mechantInfo := GetMechantInfo(payCode)
+	prepayId := getPayPrepayId(openid, outTradeNo, totalPrice, body, notifyUrl, remoteIp, payCode, true)
 	u := uuid.NewV4().String()
 	us := strings.Split(u, "-")
 	nonce_str := strings.Join(us, "")
@@ -140,104 +143,25 @@ func GetPayDataLimitPay(openid, outTradeNo string, totalPrice int64, body, notif
 		"package":   fmt.Sprintf("prepay_id=%s", prepayId),
 		"signType":  "MD5",
 		"nonceStr":  nonce_str}
-	ret["paySign"] = mch.Sign(ret, apiKey, nil)
-	return ret
-
-}
-
-// 获取jsSDK微信支付需要的数据
-func GetGzhPayData(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (ret map[string]string) {
-	prepayId := getGzhPayPrepayId(openid, outTradeNo, totalPrice, body, notifyUrl, remoteIp)
-	u := uuid.NewV4().String()
-	us := strings.Split(u, "-")
-	nonce_str := strings.Join(us, "")
-	ret = map[string]string{
-		"appId":     gzhAppId,
-		"timeStamp": fmt.Sprintf("%d", time.Now().Unix()),
-		"package":   fmt.Sprintf("prepay_id=%s", prepayId),
-		"signType":  "MD5",
-		"nonceStr":  nonce_str}
-	ret["paySign"] = mch.Sign(ret, gzhApiKey, nil)
+	ret["paySign"] = mch.Sign(ret, mechantInfo.ApiKey, nil)
 	return ret
 
 }
 
-//订单退款接口
-func GetRefundDataPay(outTradeNo, outRefundNo string, totalPrice int64, transactionId, remark string) (ret map[string]string) {
-	outTradeNo = fmt.Sprintf("%s_%d", outTradeNo, time.Now().Unix())
-	u := uuid.NewV4().String()
-	us := strings.Split(u, "-")
-	nonce_str := strings.Join(us, "")
-	req := map[string]string{
-		"mch_id":         mchId,
-		"appid":          appId,
-		"nonce_str":      nonce_str, //fmt.Sprintf("%d", time.Now().Unix()),
-		"transaction_id": transactionId,
-		//"out_trade_no":   outTradeNo,
-		"out_refund_no": outRefundNo,
-		"total_fee":     fmt.Sprintf("%d", totalPrice), //分为单位,订单总金额
-		"refund_fee":    fmt.Sprintf("%d", totalPrice), //分为单位,退款总金额
-		//"notify_url":       notifyUrl,
-		"refund_desc": remark,
-	}
-	sign := mch.Sign(req, apiKey, nil)
-	req["sign"] = sign
-	beego.BeeLogger.Warn("%v", req)
-	ret, err := pay.Refund(mchTLSProxy, req)
-	if err != nil {
-		beego.BeeLogger.Error("getGetRefundDataPay err[%s]", err)
-		//fmt.Println(err)
-		return nil
-	}
-	return ret
-}
-
 // 获取支付预授权码
 //  [pay.go:76] [E] GetPrepayId err[return_code: "FAIL", return_msg: "不识别的参数wxappid"]
 // 2016/07/07 11:07:02 [pay_controller.go:303] [W] 生成微信支付订单号:DS20160707503D648C,传的参数:openid=o7lR2txmVPh1pSPjIvto2LO_XVYU,
 // 价格:9.90, 名称:希客购, 客户端IP=119.132.31.240;生成的参数:%!v(MISSING)
-func getGzhPayPrepayId(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (prepayId string) {
-	outTradeNo = fmt.Sprintf("%s_%d", outTradeNo, time.Now().Unix())
-	u := uuid.NewV4().String()
-	us := strings.Split(u, "-")
-	nonce_str := strings.Join(us, "")
-	req := map[string]string{
-		"mch_id":           gzhMchId,
-		"appid":            gzhAppId,
-		"nonce_str":        nonce_str, //fmt.Sprintf("%d", time.Now().Unix()),
-		"body":             body,
-		"out_trade_no":     outTradeNo,
-		"total_fee":        fmt.Sprintf("%d", totalPrice), //分为单位
-		"spbill_create_ip": remoteIp,
-		"notify_url":       notifyUrl,
-		// "product_id":       outTradeNo,
-		"trade_type": "JSAPI",
-		// "limit_pay":  "no_credit",
-		//"用户在商户 appid 下的唯一标识,trade_type 为 JSAPI时,此参数必传,获取方式见表头说明。",
-		"openid": openid}
-	sign := mch.Sign(req, gzhApiKey, nil)
-	req["sign"] = sign
-	beego.BeeLogger.Warn("%v", req)
-	ret, err := pay.UnifiedOrder(gzhMchProxy, req)
-	if err != nil {
-		beego.BeeLogger.Error("getGzhPayPrepayId err[%s]", err)
-		//fmt.Println(err)
-		return ""
-	}
-	return ret["prepay_id"]
-}
+func getPayPrepayId(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp, payCode string, limit bool) (prepayId string) {
+	appId := beego.AppConfig.String("WxFohowXcxAppId") //获取小程序渠道appId
+	mechantInfo := GetMechantInfo(payCode)             //商户号配置信息
 
-// 获取支付预授权码
-//  [pay.go:76] [E] GetPrepayId err[return_code: "FAIL", return_msg: "不识别的参数wxappid"]
-// 2016/07/07 11:07:02 [pay_controller.go:303] [W] 生成微信支付订单号:DS20160707503D648C,传的参数:openid=o7lR2txmVPh1pSPjIvto2LO_XVYU,
-// 价格:9.90, 名称:希客购, 客户端IP=119.132.31.240;生成的参数:%!v(MISSING)
-func getPayPrepayId(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (prepayId string) {
 	outTradeNo = fmt.Sprintf("%s_%d", outTradeNo, time.Now().Unix())
 	u := uuid.NewV4().String()
 	us := strings.Split(u, "-")
 	nonce_str := strings.Join(us, "")
 	req := map[string]string{
-		"mch_id":           mchId,
+		"mch_id":           mechantInfo.MchId,
 		"appid":            appId,
 		"nonce_str":        nonce_str, //fmt.Sprintf("%d", time.Now().Unix()),
 		"body":             body,
@@ -250,7 +174,11 @@ func getPayPrepayId(openid, outTradeNo string, totalPrice int64, body, notifyUrl
 		// "limit_pay":  "no_credit",
 		//"用户在商户 appid 下的唯一标识,trade_type 为 JSAPI时,此参数必传,获取方式见表头说明。",
 		"openid": openid}
-	sign := mch.Sign(req, apiKey, nil)
+
+	if limit {
+		req["limit_pay"] = "no_credit"
+	}
+	sign := mch.Sign(req, mechantInfo.ApiKey, nil)
 	req["sign"] = sign
 	//beego.BeeLogger.Warn("%v", req)
 	ret, err := pay.UnifiedOrder(mchProxy, req)
@@ -262,39 +190,34 @@ func getPayPrepayId(openid, outTradeNo string, totalPrice int64, body, notifyUrl
 	return ret["prepay_id"]
 }
 
-// 获取支付预授权码--------限制不能使用信用卡
-//  [pay.go:76] [E] GetPrepayId err[return_code: "FAIL", return_msg: "不识别的参数wxappid"]
-// 2016/07/07 11:07:02 [pay_controller.go:303] [W] 生成微信支付订单号:DS20160707503D648C,传的参数:openid=o7lR2txmVPh1pSPjIvto2LO_XVYU,
-// 价格:9.90, 名称:希客购, 客户端IP=119.132.31.240;生成的参数:%!v(MISSING)
-func getPayPrepayIdLimitPay(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (prepayId string) {
+//订单退款接口
+func GetRefundDataPay(outTradeNo, outRefundNo string, totalPrice int64, transactionId, remark string) (ret map[string]string) {
 	outTradeNo = fmt.Sprintf("%s_%d", outTradeNo, time.Now().Unix())
 	u := uuid.NewV4().String()
 	us := strings.Split(u, "-")
 	nonce_str := strings.Join(us, "")
 	req := map[string]string{
-		"mch_id":           mchId,
-		"appid":            appId,
-		"nonce_str":        nonce_str, //fmt.Sprintf("%d", time.Now().Unix()),
-		"body":             body,
-		"out_trade_no":     outTradeNo,
-		"total_fee":        fmt.Sprintf("%d", totalPrice), //分为单位
-		"spbill_create_ip": remoteIp,
-		"notify_url":       notifyUrl,
-		// "product_id":       outTradeNo,
-		"trade_type": "JSAPI",
-		"limit_pay":  "no_credit",
-		//"用户在商户 appid 下的唯一标识,trade_type 为 JSAPI时,此参数必传,获取方式见表头说明。",
-		"openid": openid}
+		"mch_id":         mchId,
+		"appid":          appId,
+		"nonce_str":      nonce_str, //fmt.Sprintf("%d", time.Now().Unix()),
+		"transaction_id": transactionId,
+		//"out_trade_no":   outTradeNo,
+		"out_refund_no": outRefundNo,
+		"total_fee":     fmt.Sprintf("%d", totalPrice), //分为单位,订单总金额
+		"refund_fee":    fmt.Sprintf("%d", totalPrice), //分为单位,退款总金额
+		//"notify_url":       notifyUrl,
+		"refund_desc": remark,
+	}
 	sign := mch.Sign(req, apiKey, nil)
 	req["sign"] = sign
 	beego.BeeLogger.Warn("%v", req)
-	ret, err := pay.UnifiedOrder(mchProxy, req)
+	ret, err := pay.Refund(mchTLSProxy, req)
 	if err != nil {
-		beego.BeeLogger.Error("GetPrepayId err[%s]", err)
+		beego.BeeLogger.Error("getGetRefundDataPay err[%s]", err)
 		//fmt.Println(err)
-		return ""
+		return nil
 	}
-	return ret["prepay_id"]
+	return ret
 }
 
 func getTradeNo(prefix string) string {
@@ -346,81 +269,6 @@ func Transfers(openid string, rmb int64, tradeNo, check, realName, desc string)
 	// }
 }
 
-// 获取jsSDK微信支付需要的数据-微信公众号商户号
-func GetWxPayData(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (ret map[string]string) {
-	prepayId := getWxPayPrepayId(openid, outTradeNo, totalPrice, body, notifyUrl, remoteIp)
-	u := uuid.NewV4().String()
-	us := strings.Split(u, "-")
-	nonce_str := strings.Join(us, "")
-	ret = map[string]string{
-		"appId":     beego.AppConfig.String("WxMPAppId"),
-		"timeStamp": fmt.Sprintf("%d", time.Now().Unix()),
-		"package":   fmt.Sprintf("prepay_id=%s", prepayId),
-		"signType":  "MD5",
-		"nonceStr":  nonce_str}
-	ret["paySign"] = mch.Sign(ret, wxApiKey, nil)
-	return ret
-}
-
-//微信公众号商户号
-func getWxPayPrepayId(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (prepayId string) {
-	outTradeNo = fmt.Sprintf("%s_%d", outTradeNo, time.Now().Unix())
-	u := uuid.NewV4().String()
-	us := strings.Split(u, "-")
-	nonce_str := strings.Join(us, "")
-	req := map[string]string{
-		"mch_id":           wxMchId,
-		"appid":            beego.AppConfig.String("WxMPAppId"),
-		"nonce_str":        nonce_str, //fmt.Sprintf("%d", time.Now().Unix()),
-		"body":             body,
-		"out_trade_no":     outTradeNo,
-		"total_fee":        fmt.Sprintf("%d", totalPrice), //分为单位
-		"spbill_create_ip": remoteIp,
-		"notify_url":       notifyUrl,
-		// "product_id":       outTradeNo,
-		"trade_type": "JSAPI",
-		// "limit_pay":  "no_credit",
-		//"用户在商户 appid 下的唯一标识,trade_type 为 JSAPI时,此参数必传,获取方式见表头说明。",
-		"openid": openid}
-	sign := mch.Sign(req, wxApiKey, nil)
-	req["sign"] = sign
-	beego.BeeLogger.Warn("%v", req)
-	ret, err := pay.UnifiedOrder(wxMchProxy, req)
-	if err != nil {
-		beego.BeeLogger.Error("GetPrepayId err[%s]", err)
-		//fmt.Println(err)
-		return ""
-	}
-	return ret["prepay_id"]
-}
-
-//微信公众号商户号代付
-func WxTransfers(openid string, rmb int64, tradeNo, check, realName, desc string) map[string]string {
-	req := map[string]string{
-		"mch_appid":        beego.AppConfig.String("WxMPAppId"),
-		"mchid":            wxMchId,
-		"nonce_str":        fmt.Sprintf("%d", time.Now().Unix()),
-		"partner_trade_no": tradeNo,
-		"openid":           openid,
-		"check_name":       check,
-		"re_user_name":     realName,
-		"amount":           fmt.Sprintf("%d", rmb),
-		"desc":             desc,
-		"spbill_create_ip": "8.129.187.89"}
-	sign := mch.Sign(req, wxApiKey, nil)
-	req["sign"] = sign
-
-	ret, err := promotion.Transfers(wxMchTLSProxy, req)
-	if err != nil {
-		beego.BeeLogger.Error("WxTransfers err: %s", err)
-	}
-
-	if ret["return_code"] != "SUCCESS" {
-		return nil
-	}
-	return ret
-}
-
 // 校验支付结果回调参数
 func VerifyWxPayResult(payResult *PayResult) bool {
 	sign := payResult.Sign