Browse Source

test lehu wx pay

abiao 5 years ago
parent
commit
735078207c

+ 2 - 1
go/gopath/src/fohow.com/apps/controllers/pay_controller/after_pay_controller.go

@@ -88,7 +88,8 @@ func (self *PayController) wxPayBalanceAsync() {
 	}
 
 	// 签名校验参数
-	if !wx_mp.VerifyPayResult(params) {
+	//wx_mp.VerifyPayResult
+	if !wx_mp.LehuVerifyPayResult(params) {
 		beego.BeeLogger.Error("VerifyPayResult not pass")
 		self.Ctx.WriteString(wx_mp.MapToXmlString(notifyResponse))
 		return

+ 3 - 1
go/gopath/src/fohow.com/apps/controllers/pay_controller/pay_balance_controller.go

@@ -42,7 +42,9 @@ func (self *PayController) wxPayBalance(orderId string) {
 	notifyUrl := fmt.Sprintf("%s/v1/pay/%s/async/%s", beego.AppConfig.String("ApiHost"), BALANCE_TARGET, balance_model.PAY_WAY_TYPE_SERVICE_WXPAY) // balance "service_wxpay"
 	body := "FOHOW玖玖-购买代金券"
 	//payData := wx_mp.GetPayDataLimitPay(wxUser.Openid, balanceOrder.OrderId, totalPrice, body, notifyUrl, self.Ctx.Input.IP())
-	payData := wx_mp.GetPayDataLimitPay(wxUser.Openid, balanceOrder.OrderId, totalPrice, body, notifyUrl, self.Ctx.Input.IP())
+	//payData := wx_mp.GetPayDataLimitPay(wxUser.Openid, balanceOrder.OrderId, totalPrice, body, notifyUrl, self.Ctx.Input.IP())
+	payData := wx_mp.GetLehuPayDataLimitPay(wxUser.Openid, balanceOrder.OrderId, totalPrice, body, notifyUrl, self.Ctx.Input.IP())
+
 	//返回数据
 	type PayData struct {
 		PayData map[string]string `json:"pay_data"`

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

@@ -114,6 +114,10 @@ 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]

+ 98 - 0
go/gopath/src/fohow.com/libs/wx_mp/lehu_pay.go

@@ -0,0 +1,98 @@
+package wx_mp
+
+import (
+	"fmt"
+	"github.com/astaxie/beego"
+	"github.com/chanxuehong/wechat/mch/pay"
+	"github.com/uuid"
+	"strings"
+	"time"
+
+	// "github.com/astaxie/beego/context"
+	"github.com/chanxuehong/wechat/mch"
+	// "github.com/chanxuehong/wechat/mp/user"
+	// "go.xikego.com/apps/models/shop_model"
+	// "go.xikego.com/cache"
+)
+
+const (
+
+	//乐福收款公众号
+	lehuWxMchId  = "1581904571"
+	lehuWxApiKey = "r1sbdza2mgs2zo3x2u6w8plgh91z0gqq"
+)
+
+var lehumchProxy = mch.NewProxy(appId, lehuWxMchId, lehuWxApiKey, nil)
+
+//小程序乐福商户
+var lehuTlsHttpCilent, _ = mch.NewTLSHttpClient(
+	beego.AppConfig.String("LehuMchCertFile"),
+	beego.AppConfig.String("LehuMchKeyFile"))
+var lehuMchTLSProxy = mch.NewProxy(appId, lehuWxMchId, lehuWxApiKey, lehuTlsHttpCilent)
+
+// 获取jsSDK微信支付需要的数据
+func GetLehuPayDataLimitPay(openid, outTradeNo string, totalPrice int64, body, notifyUrl, remoteIp string) (ret map[string]string) {
+	prepayId := getLehuPayPrepayIdLimitPay(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, lehuWxApiKey, 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 getLehuPayPrepayIdLimitPay(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":           lehuWxMchId,
+		"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}
+	sign := mch.Sign(req, lehuWxApiKey, nil)
+	req["sign"] = sign
+	beego.BeeLogger.Warn("%v", req)
+	ret, err := pay.UnifiedOrder(mchProxy, req)
+	if err != nil {
+		beego.BeeLogger.Error("GetPrepayId err[%s]", err)
+		//fmt.Println(err)
+		return ""
+	}
+	return ret["prepay_id"]
+}
+
+// 校验支付结果回调参数--乐福微信
+func LehuVerifyPayResult(payResult *PayResult) bool {
+	sign := payResult.Sign
+	params, err := XmlStructToMap(payResult)
+	if err != nil {
+		beego.BeeLogger.Error("transfer xmlstruct to map err=[%s]", err)
+		return false
+	}
+	// beego.BeeLogger.Warn("*****params: %v", params)
+	// beego.BeeLogger.Warn("*****sign: %s", sign)
+	mchSign := mch.Sign(params, lehuWxApiKey, nil)
+	// beego.BeeLogger.Warn("*****mch sign: %s", mchSign)
+	return sign == mchSign
+}