|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- package local_wxpay
-
- import (
- zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils/logx"
- "fmt"
- "github.com/iGoogle-ink/gopay"
- "github.com/iGoogle-ink/gopay/pkg/util"
- "github.com/iGoogle-ink/gopay/wechat"
- v3 "github.com/iGoogle-ink/gopay/wechat/v3"
- "strconv"
- "time"
- )
-
- func NewClient(appId, mchId, apiKey string, isProd bool) *wechat.Client {
- // 初始化微信客户端
- // appId:应用ID
- // mchId:商户ID
- // apiKey:API秘钥值
- // isProd:是否是正式环境
- client := wechat.NewClient(appId, mchId, apiKey, isProd)
- // 打开Debug开关,输出请求日志,默认关闭
- client.DebugSwitch = gopay.DebugOn
- // 设置国家:不设置默认 中国国内
- // wechat.China:中国国内
- // wechat.China2:中国国内备用
- // wechat.SoutheastAsia:东南亚
- // wechat.Other:其他国家
- client.SetCountry(wechat.China)
- // 添加微信证书 Path 路径
- // certFilePath:apiclient_cert.pem 路径
- // keyFilePath:apiclient_key.pem 路径
- // pkcs12FilePath:apiclient_cert.p12 路径
- // 返回err
- //client.AddCertFilePath()
-
- // 添加微信证书内容 Content
- // certFileContent:apiclient_cert.pem 内容
- // keyFileContent:apiclient_key.pem 内容
- // pkcs12FileContent:apiclient_cert.p12 内容
- // 返回err
- //client.AddCertFileContent()
- return client
- }
-
- // TradeAppPay is 微信APP支付
- func TradeAppPay(client *wechat.Client, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
- // 初始化 BodyMap
- bm := make(gopay.BodyMap)
- bm.Set("nonce_str", util.GetRandomString(32)).
- Set("body", subject).
- Set("out_trade_no", orderID).
- Set("total_fee", amount).
- Set("spbill_create_ip", "127.0.0.1").
- Set("notify_url", notifyUrl).
- Set("trade_type", wechat.TradeType_App).
- Set("sign_type", wechat.SignType_MD5)
- /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
- // 预下单
- wxRsp, err := client.UnifiedOrder(bm)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- //if !ok {
- // return nil, errors.New("验签失败")
- //}
- timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
- paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
- res := map[string]string{
- "appid": client.AppId,
- "partnerid": client.MchId,
- "prepayid": wxRsp.PrepayId,
- "sign": paySign,
- "package": "Sign=WXPay",
- "noncestr": wxRsp.NonceStr,
- "timestamp": timeStamp,
- }
- return res, nil
- }
-
- // TradeAppPay is 微信H5支付
- func TradeH5Pay(client *wechat.Client, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
- // 初始化 BodyMap
- bm := make(gopay.BodyMap)
- bm.Set("nonce_str", util.GetRandomString(32)).
- Set("body", subject).
- Set("out_trade_no", orderID).
- Set("total_fee", amount).
- Set("spbill_create_ip", "121.196.29.49").
- Set("notify_url", notifyUrl).
- Set("trade_type", wechat.TradeType_H5).
- Set("sign_type", wechat.SignType_MD5).
- SetBodyMap("scene_info", func(bm gopay.BodyMap) {
- bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
- bm.Set("type", "Wap")
- bm.Set("wap_url", "https://www.fumm.cc")
- bm.Set("wap_name", "zyos")
- })
- })
- /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
- // 预下单
- wxRsp, err := client.UnifiedOrder(bm)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
- packages := "prepay_id=" + wxRsp.PrepayId
- paySign := wechat.GetH5PaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
- fmt.Println("paySign===", paySign)
- r := map[string]string{
- "redirect_url": wxRsp.MwebUrl,
- }
- return r, nil
- }
-
- // TradeMiniProgPay is 微信小程序支付 ☑️
- func TradeMiniProgPay(client *wechat.Client, subject, orderID, amount, notifyUrl, openid string) (map[string]string, error) {
- // 初始化 BodyMap
- bm := make(gopay.BodyMap)
- bm.Set("nonce_str", util.GetRandomString(32)).
- Set("body", subject).
- Set("openid", openid).
- Set("out_trade_no", orderID).
- Set("total_fee", amount).
- Set("spbill_create_ip", "127.0.0.1").
- Set("notify_url", notifyUrl).
- Set("trade_type", wechat.TradeType_Mini).
- Set("sign_type", wechat.SignType_MD5)
- // 预下单
- wxRsp, err := client.UnifiedOrder(bm)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- fmt.Println(wxRsp)
- timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
- packages := "prepay_id=" + wxRsp.PrepayId
- paySign := wechat.GetMiniPaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
- res := map[string]string{
- "appId": client.AppId,
- "paySign": paySign,
- "signType": wechat.SignType_MD5,
- "package": packages,
- "nonceStr": wxRsp.NonceStr,
- "timeStamp": timeStamp,
- }
- return res, nil
- }
-
- // TradeAppPayV3 is 微信APP支付v3
- func TradeAppPayV3(client *v3.ClientV3, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
- // 初始化 BodyMap
- amountNew := zhios_pay_utils.AnyToFloat64(amount) * 100
- bm := make(gopay.BodyMap)
- bm.Set("nonce_str", util.GetRandomString(32)).
- Set("body", subject).
- Set("out_trade_no", orderID).
- Set("total_fee", amountNew).
- Set("spbill_create_ip", "127.0.0.1").
- Set("notify_url", notifyUrl).
- Set("trade_type", wechat.TradeType_App).
- Set("sign_type", wechat.SignType_MD5)
- /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
- //// 预下单
- //wxRsp, err := v3.UnifiedOrder(bm)
- //if err != nil {
- // _ = zhios_pay_logx.Warn(err)
- // return nil, err
- //}
- //_, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
- //if err != nil {
- // _ = zhios_pay_logx.Warn(err)
- // return nil, err
- //}
- ////if !ok {
- //// return nil, errors.New("验签失败")
- ////}
- //timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
- //paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
- //res := map[string]string{
- // "appid": client.AppId,
- // "partnerid": client.MchId,
- // "prepayid": wxRsp.PrepayId,
- // "sign": paySign,
- // "package": "Sign=WXPay",
- // "noncestr": wxRsp.NonceStr,
- // "timestamp": timeStamp,
- //}
- //return res, nil
- return nil, nil
- }
-
- //// TradeJSAPIPay is 微信JSAPI支付
- func TradeJSAPIPay(client *wechat.Client, subject, orderID, amount, notifyUrl, openid string) (map[string]string, error) {
- // 初始化 BodyMap
- bm := make(gopay.BodyMap)
- bm.Set("nonce_str", util.GetRandomString(32)).
- Set("body", subject).
- Set("out_trade_no", orderID).
- Set("total_fee", amount).
- Set("spbill_create_ip", "121.196.29.49").
- Set("notify_url", notifyUrl).
- Set("trade_type", wechat.TradeType_JsApi).
- Set("sign_type", wechat.SignType_MD5).
- Set("openid", openid).
- SetBodyMap("scene_info", func(bm gopay.BodyMap) {
- bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
- bm.Set("type", "Wap")
- bm.Set("wap_url", "https://www.fumm.cc")
- bm.Set("wap_name", "zyos")
- })
- })
- // 预下单
- wxRsp, err := client.UnifiedOrder(bm)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return nil, err
- }
- //if !ok {
- // return nil, errors.New("验签失败")
- //}
- timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
- //paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
- packages := "prepay_id=" + wxRsp.PrepayId
- paySign := wechat.GetJsapiPaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
-
- zhios_pay_logx.Info("wxRsp.PrepayId:" + wxRsp.PrepayId)
- zhios_pay_logx.Info("wxRsp.PrepayId:" + wxRsp.PrepayId)
- zhios_pay_logx.Info("wxRsp.PrepayId:" + openid)
- res := map[string]string{
- "appid": client.AppId,
- "partnerid": client.MchId,
- "prepayid": wxRsp.PrepayId,
- "sign": paySign,
- "package": "prepay_id=" + wxRsp.PrepayId,
- "noncestr": wxRsp.NonceStr,
- "timestamp": timeStamp,
- }
- return res, nil
- }
-
- // TradeH5PayV3 is 微信H5支付v3
- func TradeH5PayV3(client *wechat.Client, subject, orderID, amount, notifyUrl string) (string, error) {
- // 初始化 BodyMap
- bm := make(gopay.BodyMap)
- bm.Set("nonce_str", util.GetRandomString(32)).
- Set("body", subject).
- Set("out_trade_no", orderID).
- Set("total_fee", amount).
- Set("spbill_create_ip", "127.0.0.1").
- Set("notify_url", notifyUrl).
- Set("trade_type", wechat.TradeType_App).
- Set("device_info", "WEB").
- Set("sign_type", wechat.SignType_MD5).
- SetBodyMap("scene_info", func(bm gopay.BodyMap) {
- bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
- bm.Set("type", "Wap")
- bm.Set("wap_url", "https://www.fumm.cc")
- bm.Set("wap_name", "H5测试支付")
- })
- }) /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
- // 预下单
- wxRsp, err := client.UnifiedOrder(bm)
- if err != nil {
- _ = zhios_pay_logx.Warn(err)
- return "", err
- }
- // ====APP支付 paySign====
- timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
- // 获取APP支付的 paySign
- // 注意:package 参数因为是固定值,无需开发者再传入
- // appId:AppID
- // partnerid:partnerid
- // nonceStr:随机字符串
- // prepayId:统一下单成功后得到的值
- // signType:签名方式,务必与统一下单时用的签名方式一致
- // timeStamp:时间
- // apiKey:API秘钥值
- paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
- return paySign, nil
- }
-
- // TradeMiniProgPayV3 is 微信小程序支付v3
- func TradeMiniProgPayV3(client *v3.ClientV3, subject, orderID, amount, notifyUrl string) (string, error) {
- return "", nil
- }
|