diff --git a/go.mod b/go.mod index b4dbd24..0de29a0 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/iGoogle-ink/gopay v1.5.36 github.com/iGoogle-ink/gotil v1.0.20 github.com/pkg/errors v0.9.1 + github.com/syyongx/php2go v0.9.6 go.uber.org/zap v1.16.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0 xorm.io/builder v0.3.10 // indirect diff --git a/md/wxpay.go b/md/wxpay.go index 7ce6595..5526176 100644 --- a/md/wxpay.go +++ b/md/wxpay.go @@ -10,6 +10,11 @@ type WxPayParams struct { PayWxApiKey string `json:"pay_wx_api_key"` NotifyUrl string `json:"notify_url"` ThirdPartyWechatOpenid string `json:"third_party_wechat_openid"` + Platform string `json:"platform"` + PayType string `json:"pay_type"` + PayThirdPartyId string `json:"pay_third_party_id"` + KuDianNotifyUrl string `json:"ku_dian_notify_url"` + } type WxPayCallback struct { diff --git a/pay/pay_by_kudian.go b/pay/pay_by_kudian.go index 7add27f..1cf7176 100644 --- a/pay/pay_by_kudian.go +++ b/pay/pay_by_kudian.go @@ -139,3 +139,61 @@ func MakePayParamsByKuDian(url, uniqueIdentifierOne, uniqueIdentifierTwo, applet r["wx_url"] = md.WX_APPLET_PAY_URL + redisKey + "&" + queryParams return r, nil } + +func KuDianAlipayConfig(req, payConfig map[string]string) (*md.KuDianAliAppPayParams, error) { + var payParams = &md.KuDianAliAppPayParams{ + OutTradeNo: payConfig["ord_id"], + NotifyUrl: payConfig["notify_url"], + Description: payConfig["subject"], + TotalAmount: payConfig["amount"], + Attach: req["mid"], + MustParams: struct { + ShopId string `json:"shop_id"` + KuDianMchId string `json:"ku_dian_mch_id"` + KuDianSecretKey string `json:"ku_dian_secret_key"` + }{ + ShopId: payConfig["pay_third_party_id"], + KuDianMchId: req["ku_dian_mch_id"], + KuDianSecretKey: req["ku_dian_secret_key"], + }, + } + if payConfig["trade_type"] != "" { + payParams.TradeType = payConfig["trade_type"] + } + if payConfig["wap_mame"] != "" { + payParams.SenceInfo.WapName = payConfig["wap_mame"] + } + if payConfig["return_url"] != "" { + payParams.SenceInfo.ReturnUrl = payConfig["return_url"] + } + if payConfig["wap_url"] != "" { + payParams.SenceInfo.WapUrl = payConfig["wap_url"] + } + if payConfig["trade_type"] != "" { + payParams.TradeType = payConfig["trade_type"] + } + + return payParams, nil +} + +func KuDianWxConfig(req, payConfig map[string]string) (*md.KuDianWxPayParams, error) { + var payParams = &md.KuDianWxPayParams{ + OutTradeNo: payConfig["ord_id"], + NotifyUrl: payConfig["notify_url"], + Description: payConfig["subject"], + TotalAmount: payConfig["amount"], + AppId: req["wx_app_id"], + OpenId: req["wx_open_id"], + Attach: req["mid"], + MustParams: struct { + ShopId string `json:"shop_id"` + KuDianMchId string `json:"ku_dian_mch_id"` + KuDianSecretKey string `json:"ku_dian_secret_key"` + }{ + ShopId: payConfig["pay_third_party_id"], + KuDianMchId: req["ku_dian_mch_id"], + KuDianSecretKey: req["ku_dian_secret_key"], + }, + } + return payParams, nil +} diff --git a/pay/pay_by_own.go b/pay/pay_by_own.go index d909cb8..cd63500 100644 --- a/pay/pay_by_own.go +++ b/pay/pay_by_own.go @@ -4,206 +4,162 @@ import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_alipay" local_wxpay "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_wechat" "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md" - zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils" - "fmt" - "github.com/iGoogle-ink/gopay" - v3 "github.com/iGoogle-ink/gopay/wechat/v3" - "github.com/iGoogle-ink/gotil/xlog" "github.com/pkg/errors" + "github.com/syyongx/php2go" + "strings" ) //自有支付 支付宝 -func Alipay(args map[string]string) (string, error) { +func Alipay(req map[string]string) (interface{}, error) { var paySet = &md.PayData{ - PayAppCertSn: args["pay_app_cert_sn"], - PayAlipayRootCertSn: args["pay_alipay_root_cert_sn"], - PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"], - PayAliUseType: args["pay_ali_use_type"], - WxAppletFilepathUrl: args["wx_applet_filepath_url"], + PayAppCertSn: req["pay_app_cert_sn"], + PayAlipayRootCertSn: req["pay_alipay_root_cert_sn"], + PayAlipayrsaPublicKey: req["pay_alipayrsa_public_key"], + PayAliUseType: req["pay_ali_use_type"], + WxAppletFilepathUrl: req["wx_applet_filepath_url"], } - if args["private_key"] == "" || args["app_id"] == "" { + if req["private_key"] == "" || req["app_id"] == "" { return "", errors.New("请在后台正确配置支付宝") } + prd := false + if req["cfg_prd"] == "1" { + prd = true + } var param interface{} var err error - switch args["platform"] { + switch req["platform"] { case md.PLATFORM_ALIPAY_APPLET: - param, err = local_alipay.TradeCreate(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet) + param, err = local_alipay.TradeCreate(req["app_id"], req["private_key"], req["subject"], req["ord_id"], req["amount"], req["notify_url"], req["rsa"], req["pkcs"], paySet) case md.PLATFORM_WAP: - param, err = local_alipay.TradeWapPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], args["page_url"], paySet) + if req["pay_type"] == "self" { + param, err = local_alipay.TradeCreate(req["app_id"], req["private_key"], req["subject"], req["ord_id"], req["amount"], req["notify_url"], req["rsa"], req["pkcs"], paySet) + } else if req["pay_type"] == "thirdParty" { + var actualPayMethod string + payConfigs := map[string]string{ + "ord_id": req["ord_id"], + "notify_url": req["kudian_notify_url"], + "subject": req["subject"], + "amount": req["amount"], + "pay_third_party_id": req["pay_third_party_id"], + "user_id": req["user_id"], + } + if strings.Contains(req["browser"], "ali_pay_browser") && req["user_id"] != "" { //判断是否在支付宝内浏览器打开 + actualPayMethod = "JSPAI" + payConfigs["trade_type"] = actualPayMethod + } else { + actualPayMethod = "H5" + payConfigs["trade_type"] = actualPayMethod + payConfigs["wap_mame"] = "支付" + payConfigs["return_url"] = "" + payConfigs["wap_url"] = req["wap_url"] + } + payParams := &md.KuDianAliAppPayParams{} + payParams, err = KuDianAlipayConfig(req, payConfigs) + if err != nil { + return nil, err + } + var r string + + r, err = AliJsApiPayByKuDian(payParams, prd) + if err != nil { + return nil, err + } + if actualPayMethod == "H5" { + r = php2go.URLEncode(r) + } + usePayMethod := "third_party" + return map[string]string{ + "params": r, + "actual_pay_method": actualPayMethod, + "use_pay_method": usePayMethod, + "orderId": req["ord_id"], + }, nil + } case md.PLATFORM_ANDROID, md.PLATFORM_IOS: - param, err = local_alipay.TradeAppPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet) + if req["pay_type"] == "self" { + param, err = local_alipay.TradeAppPay(req["app_id"], req["private_key"], req["subject"], req["ord_id"], req["amount"], req["notify_url"], req["rsa"], req["pkcs"], paySet) + } else if req["pay_type"] == "thirdParty" { + payConfigs := map[string]string{ + "ord_id": req["ord_id"], + "notify_url": req["kudian_notify_url"], + "subject": req["subject"], + "amount": req["amount"], + "pay_third_party_id": req["pay_third_party_id"], + } + payParams := &md.KuDianAliAppPayParams{} + payParams, err = KuDianAlipayConfig(req, payConfigs) + if err != nil { + return nil, err + } + var r string + r, err = AliAppPayByKuDian(payParams, prd) + if err != nil { + return nil, err + } + r = php2go.URLEncode(r) + usePayMethod := "third_party" + return map[string]string{ + "params": r, + "use_pay_method": usePayMethod, + "orderId": req["ord_id"], + }, nil + } default: - return "", errors.New("支付平台不存在") - } - if err != nil { - fmt.Println("支付宝错误日志") - fmt.Println(param) - fmt.Println(err) - return "", errors.New("支付宝订单创建失败") - } - return zhios_pay_utils.AnyToString(param), nil -} -func AlipayApp(args map[string]string) (string, error) { - var paySet = &md.PayData{ - PayAppCertSn: args["pay_app_cert_sn"], - PayAlipayRootCertSn: args["pay_alipay_root_cert_sn"], - PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"], - PayAliUseType: args["pay_ali_use_type"], - WxAppletFilepathUrl: args["wx_applet_filepath_url"], - } - if args["private_key"] == "" || args["app_id"] == "" { - return "", errors.New("请在后台正确配置支付宝") - } - param, err := local_alipay.TradeAppPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet) - if err != nil { - fmt.Println("支付宝错误日志") - fmt.Println(param) - fmt.Println(err) - return "", errors.New("支付宝订单创建失败") - } - return zhios_pay_utils.AnyToString(param), nil -} - -func AlipayWap(args map[string]string) (string, error) { - - var paySet = &md.PayData{ - PayAppCertSn: args["pay_app_cert_sn"], - PayAlipayRootCertSn: args["pay_alipay_root_cert_sn"], - PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"], - PayAliUseType: args["pay_ali_use_type"], - WxAppletFilepathUrl: args["wx_applet_filepath_url"], - } - if args["private_key"] == "" || args["app_id"] == "" { - return "", errors.New("请在后台正确配置支付宝") - } - param, err := local_alipay.TradeWapPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], args["page_url"], paySet) - if err != nil { - fmt.Println("支付宝错误日志") - fmt.Println(param) - fmt.Println(err) - return "", errors.New("支付宝订单创建失败") - } - return zhios_pay_utils.AnyToString(param), nil -} - -func AlipayApplet(args map[string]string) (string, error) { - var paySet = &md.PayData{ - PayAppCertSn: args["pay_app_cert_sn"], - PayAlipayRootCertSn: args["pay_alipay_root_cert_sn"], - PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"], - PayAliUseType: args["pay_ali_use_type"], - WxAppletFilepathUrl: args["wx_applet_filepath_url"], - } - if args["private_key"] == "" || args["app_id"] == "" { - return "", errors.New("请在后台正确配置支付宝") + return "", errors.New("渠道不存在") } - param, err := local_alipay.TradeCreate(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet) if err != nil { - fmt.Println("支付宝错误日志") - fmt.Println(param) - fmt.Println(err) - return "", errors.New("支付宝订单创建失败") + return "", err } - return zhios_pay_utils.AnyToString(param), nil + return param, nil } //自有支付 微信支付 +func WxPay(params map[string]string) (interface{}, error) { + var r interface{} + var err error + prd := false + if params["cfg_prd"] == "1" { + prd = true + } + client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true) + switch params["platform"] { + case md.PLATFORM_WX_APPLET: + if params["pay_type"] == "self" { //自有支付 + r, err = local_wxpay.TradeMiniProgPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"]) + } else if params["pay_type"] == "thirdParty" { + payConfigs := map[string]string{ + "ord_id": params["ord_id"], + "notify_url": params["ku_dian_notify_url"], + "subject": params["subject"], + "amount": params["amount"], + "pay_third_party_id": params["pay_third_party_id"], + } + payParams := &md.KuDianWxPayParams{} + payParams, err = KuDianWxConfig(params, payConfigs) + if err != nil { + return nil, err + } + r, err = WxAppletPayByKuDian(payParams, prd) + r = map[string]interface{}{ + "params": r, + "orderId": params["ord_id"], + } + } + case md.PLATFORM_WAP: + if strings.Contains(params["browser"], "wx_pay_browser") { + r, err = local_wxpay.TradeJSAPIPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"]) -// app支付v2 -func WxAppPay(params *md.WxPayParams) (map[string]string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - apiKey := params.PayWxApiKey - notifyUrl := params.NotifyUrl - client := local_wxpay.NewClient(appId, mchId, apiKey, true) - r, err := local_wxpay.TradeAppPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl) - return r, err -} - -// H5支付v2 -func WxH5Pay(params *md.WxPayParams) (map[string]string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - apiKey := params.PayWxApiKey - notifyUrl := params.NotifyUrl - client := local_wxpay.NewClient(appId, mchId, apiKey, true) - r, err := local_wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl) - return r, err -} - -// 小程序v2 -func WxMiniProgPay(params *md.WxPayParams) (map[string]string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - apiKey := params.PayWxApiKey - notifyUrl := params.NotifyUrl - client := local_wxpay.NewClient(appId, mchId, apiKey, true) - if params.ThirdPartyWechatOpenid == "" { - return nil, errors.New("请先授权微信") + } else { + r, err = local_wxpay.TradeH5Pay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"]) + } + case md.PLATFORM_ANDROID, md.PLATFORM_IOS, md.PLATFORM_JSAPI: + r, err = local_wxpay.TradeAppPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"]) + default: + return nil, errors.New("渠道不存在") } - r, err := local_wxpay.TradeMiniProgPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, params.ThirdPartyWechatOpenid) - return r, err -} - -// app支付V3 -func WxAppPayV3(params *md.WxPayParams) (map[string]string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - notifyUrl := params.NotifyUrl - SerialNo := params.PayWxApiKey - ApiV3Key := params.PayWxApiKey - PKContent := params.PayWxApiKey - client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent) if err != nil { - xlog.Error(err) return nil, err } - client.DebugSwitch = gopay.DebugOff - r, err := local_wxpay.TradeAppPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl) - return r, err -} - -// 微信JSAPI支付 -func WxAppJSAPIPay(params *md.WxPayParams) (map[string]string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - apiKey := params.PayWxApiKey - notifyUrl := params.NotifyUrl - - client := local_wxpay.NewClient(appId, mchId, apiKey, true) - if params.ThirdPartyWechatOpenid == "" { - return nil, errors.New("请先授权微信") - } - r, err := local_wxpay.TradeJSAPIPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, params.ThirdPartyWechatOpenid) - return r, err -} + return r, nil -// H5支付V3 -func WxH5PayV3(params *md.WxPayParams) (string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - apiKey := params.PayWxApiKey - notifyUrl := params.NotifyUrl - client := local_wxpay.NewClient(appId, mchId, apiKey, false) - _, err := local_wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl) - return "", err -} - -// 小程序V3 -func WxMiniProgPayV3(params *md.WxPayParams) (string, error) { - appId := params.PayWxAppid - mchId := params.PayWxMchId - notifyUrl := params.NotifyUrl - SerialNo := params.PayWxApiKey - ApiV3Key := params.PayWxApiKey - PKContent := params.PayWxApiKey - client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent) - if err != nil { - xlog.Error(err) - return "", err - } - client.DebugSwitch = gopay.DebugOff - r, err := local_wxpay.TradeMiniProgPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl) - return r, err }