From 6275d5fa75b168463ca4f02881304f4294b08681 Mon Sep 17 00:00:00 2001 From: jiaoboxiang Date: Fri, 16 Dec 2022 11:42:48 +0800 Subject: [PATCH 01/15] =?UTF-8?q?=E4=BB=98=E5=91=97=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/luso_bank/macao_luso.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/luso_bank/macao_luso.go b/lib/luso_bank/macao_luso.go index 7e94171..7ae2a34 100644 --- a/lib/luso_bank/macao_luso.go +++ b/lib/luso_bank/macao_luso.go @@ -70,7 +70,7 @@ func (s *signParam) SetSign(privateKeyStr string) error { func (payParam *PayParam) Send(prd, isNeedEncode bool, method string) (string, error) { url := "https://qrpaytest.lusobank.com.mo:443" if prd { - url = "https://qrpaytest.lusobank.com.mo:8443" + url = "https://qrpay.lusobank.com.mo:443" } switch method { case "create": From fd95d3c9322ab8bd9538001cb8989bb42ff82501 Mon Sep 17 00:00:00 2001 From: jiaoboxiang Date: Fri, 30 Dec 2022 15:18:16 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=E4=BB=98=E5=91=97=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/fb_pay_test.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/test/fb_pay_test.go b/test/fb_pay_test.go index 3f1d984..4cde019 100644 --- a/test/fb_pay_test.go +++ b/test/fb_pay_test.go @@ -77,3 +77,89 @@ func TestFBPayOrderWxconfig(t *testing.T) { } t.Logf("回调数据:%s\n", send) } + +func TestFBAccreditQuery(t *testing.T) { + requestParameters := fb.GetRequestParametersByVendorSn("2022120518415543840a") + requestParameters.Method = "openapi.share.accredit.query" + requestParameters.SignMethod = "md5" + requestParameters.Nonce = time.Now().Format("20060102150405") + param := make(map[string]interface{}) + param["merchant_id"] = 1989190 + for key, value := range param { + if value == "" { + delete(param, key) + } + } + requestParameters.SetBizContent(param) + requestParameters.SetSign("c5d47b9e515594313d80fb9903ece2d8") + send, err := requestParameters.Send(false) + if err != nil { + t.Error(err) + } + t.Logf("回调数据:%s\n", send) +} + +func TestFBAccountOrderSwitch(t *testing.T) { + requestParameters := fb.GetRequestParametersByVendorSn("2022120518415543840a") + requestParameters.Method = "openapi.agent.account.order.switch" + requestParameters.SignMethod = "md5" + requestParameters.Nonce = time.Now().Format("20060102150405") + param := make(map[string]interface{}) + param["merchant_id"] = 1989190 + param["state"] = 1 + param["share_percent"] = 60 + for key, value := range param { + if value == "" { + delete(param, key) + } + } + requestParameters.SetBizContent(param) + requestParameters.SetSign("c5d47b9e515594313d80fb9903ece2d8") + send, err := requestParameters.Send(false) + if err != nil { + t.Error(err) + } + t.Logf("回调数据:%s\n", send) +} + +func TestFBMerchantIncomeSuccess(t *testing.T) { + requestParameters := fb.GetRequestParametersByVendorSn("2022120518415543840a") + requestParameters.Method = "openapi.merchant.income.status.query" + requestParameters.SignMethod = "md5" + requestParameters.Nonce = time.Now().Format("20060102150405") + param := make(map[string]interface{}) + param["merchant_code"] = "139334168531126" + for key, value := range param { + if value == "" { + delete(param, key) + } + } + requestParameters.SetBizContent(param) + requestParameters.SetSign("c5d47b9e515594313d80fb9903ece2d8") + send, err := requestParameters.Send(false) + if err != nil { + t.Error(err) + } + t.Logf("回调数据:%s\n", send) +} + +func TestFBUrlSet(t *testing.T) { + requestParameters := fb.GetRequestParametersByVendorSn("2022120518415543840a") + requestParameters.Method = "fbpay.pay.callback.config" + requestParameters.SignMethod = "md5" + requestParameters.Nonce = time.Now().Format("20060102150405") + param := make(map[string]interface{}) + param["second_callback_url"] = "http://api.zhiyingos.com/api/v1/comm/pay/fb/callback" + for key, value := range param { + if value == "" { + delete(param, key) + } + } + requestParameters.SetBizContent(param) + requestParameters.SetSign("c5d47b9e515594313d80fb9903ece2d8") + send, err := requestParameters.Send(false) + if err != nil { + t.Error(err) + } + t.Logf("回调数据:%s\n", send) +} From 52520fb4ee79ce525c548abd54caa69300782ecc Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 1 Feb 2023 17:15:45 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/fb_pay/fb_pay.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/fb_pay/fb_pay.go b/lib/fb_pay/fb_pay.go index d551c9d..4fb0939 100644 --- a/lib/fb_pay/fb_pay.go +++ b/lib/fb_pay/fb_pay.go @@ -20,7 +20,18 @@ func GetOrderQrcodeByVendorSn(vendorSn string) *RequestParameters { rp.Method = OrderQrcode return &rp } - +func GetRefundByVendorSn(vendorSn string) *RequestParameters { + var rp RequestParameters + rp.VendorSn = vendorSn + rp.Method = Refund + return &rp +} +func GetRefundQueryByVendorSn(vendorSn string) *RequestParameters { + var rp RequestParameters + rp.VendorSn = vendorSn + rp.Method = RefundQuery + return &rp +} func GetAggregateCodeByVendorSn(vendorSn string) *RequestParameters { var rp RequestParameters rp.VendorSn = vendorSn From fce67e3255c402ebe191573866ae9d94ed1f7532 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Tue, 11 Apr 2023 14:37:29 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md/alipay.go | 59 ++++++++ pay/alipay.go | 394 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 453 insertions(+) create mode 100644 pay/alipay.go diff --git a/md/alipay.go b/md/alipay.go index 17799ac..0dd45de 100644 --- a/md/alipay.go +++ b/md/alipay.go @@ -15,3 +15,62 @@ type PayData struct { PriKey string `json:"pay_ali_new_private_key"` WxAppletFilepathUrl string `json:"wx_applet_filepath_url"` } + +type ZhimaCreditPayafteruseCreditagreementQuery struct { + ZhimaCreditPayafteruseCreditagreementQueryResponse struct { + Code string `json:"code"` + Msg string `json:"msg"` + AgreementStatus string `json:"agreement_status"` + BizTime string `json:"biz_time"` + CreditAgreementId string `json:"credit_agreement_id"` + OutAgreementNo string `json:"out_agreement_no"` + } `json:"zhima_credit_payafteruse_creditagreement_query_response"` + AlipayCertSn string `json:"alipay_cert_sn"` + Sign string `json:"sign"` +} +type ZhimaCreditPayafteruseCreditbizorderOrder struct { + ZhimaCreditPayafteruseCreditbizorderOrderResponse struct { + Code string `json:"code"` + Msg string `json:"msg"` + OutOrderNo string `json:"out_order_no"` + CreditBizOrderId string `json:"credit_biz_order_id"` + } `json:"zhima_credit_payafteruse_creditbizorder_order_response"` + Sign string `json:"sign"` +} +type ZhimaCreditPayafteruseCreditbizorderQuery struct { + ZhimaCreditPayafteruseCreditbizorderQueryResponse struct { + Code string `json:"code"` + Msg string `json:"msg"` + CreditBizOrderId string `json:"credit_biz_order_id"` + CreditAgreementId string `json:"credit_agreement_id"` + TotalAmount float64 `json:"total_amount"` + CreateTime string `json:"create_time"` + ZmServiceId string `json:"zm_service_id"` + ProductCode string `json:"product_code"` + OrderStatus string `json:"order_status"` + TradeNo string `json:"trade_no"` + } `json:"zhima_credit_payafteruse_creditbizorder_query_response"` + Sign string `json:"sign"` +} +type AlipayFundTransUniTransfer struct { + AlipayFundTransUniTransferResponse struct { + Code string `json:"code"` + Msg string `json:"msg"` + OutBizNo string `json:"out_biz_no"` + OrderId string `json:"order_id"` + PayFundOrderId string `json:"pay_fund_order_id"` + Status string `json:"status"` + TransDate string `json:"trans_date"` + } `json:"alipay_fund_trans_uni_transfer_response"` + Sign string `json:"sign"` +} +type ZhimaCreditPayafteruseCreditbizorderFinish struct { + ZhimaCreditPayafteruseCreditbizorderFinishResponse struct { + Code string `json:"code"` + Msg string `json:"msg"` + OutRequestNo string `json:"out_request_no"` + CreditBizOrderId string `json:"credit_biz_order_id"` + OrderStatus string `json:"order_status"` + } `json:"zhima_credit_payafteruse_creditbizorder_finish_response"` + Sign string `json:"sign"` +} diff --git a/pay/alipay.go b/pay/alipay.go new file mode 100644 index 0000000..8ee0ea6 --- /dev/null +++ b/pay/alipay.go @@ -0,0 +1,394 @@ +package pay + +import ( + "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" + "encoding/json" + "errors" + "fmt" + "github.com/iGoogle-ink/gopay" + "github.com/iGoogle-ink/gopay/alipay" + "github.com/iGoogle-ink/gopay/pkg/util" + "github.com/iGoogle-ink/gopay/pkg/xhttp" + "github.com/iGoogle-ink/gopay/pkg/xlog" + "time" +) + +const ( + baseUrl = "https://openapi.alipay.com/gateway.do" + sandboxBaseUrl = "https://openapi.alipaydev.com/gateway.do" + baseUrlUtf8 = "https://openapi.alipay.com/gateway.do?charset=utf-8" + sandboxBaseUrlUtf8 = "https://openapi.alipaydev.com/gateway.do?charset=utf-8" +) + +func Comm(args map[string]string) *alipay.Client { + paySet := AlipayCofing(args) + if args["private_key"] == "" || args["app_id"] == "" { + return nil + } + //初始化支付宝客户端 + // appID 是在支付宝申请的APPID + // priKey 是支付宝私钥 + // code 支付宝授权码 + client := alipay.NewClient(args["app_id"], args["private_key"], true) + client.DebugSwitch = gopay.DebugOn + //判断密钥的类型 + rsa_type := alipay.RSA2 + pkcs_type := alipay.PKCS1 + if args["rsa"] == "1" { + rsa_type = alipay.RSA + } + if args["pkcs"] == "1" { + pkcs_type = alipay.PKCS8 + } + if paySet.PayAliUseType == "1" { + rsa_type = alipay.RSA2 + pkcs_type = alipay.PKCS8 + } + //配置公共参数 + client.SetCharset("utf-8"). + SetSignType(rsa_type). + SetPrivateKeyType(pkcs_type) + //新支付宝支付 + if paySet.PayAliUseType == "1" { + client.SetAppCertSN(paySet.PayAppCertSn) + aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6" + client.SetAliPayRootCertSN(aliPayRootCertSN) + client.SetAliPayPublicCertSN(paySet.PayAlipayrsaPublicKey) + } + fmt.Println(client) + return client + +} +func OpenAuthTokenApp(args map[string]string) (*alipay.SystemOauthTokenResponse, error) { + client := Comm(args) + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + body := make(gopay.BodyMap) + body.Set("grant_type", "authorization_code") + body.Set("code", args["code"]) + + payParam, err := client.SystemOauthToken(body) + if err != nil { + return nil, err + } + return payParam, nil +} + +func CreditagreementSign(args map[string]string) (string, error) { + client := Comm(args) + client.AppAuthToken = args["app_auth_token"] + if client == nil { + return "", errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_agreement_no", args["out_agreement_no"]) + bz.Set("zm_service_id", args["zm_service_id"]) + bz.Set("category_id", args["category_id"]) + }) + + aliPsp := "" + bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditagreement.sign", &aliPsp) + if err != nil { + xlog.Error(err) + return "", err + } + return zhios_pay_utils.AnyToString(bs), nil +} +func CreditagreementQuery(args map[string]string) (*md.ZhimaCreditPayafteruseCreditagreementQuery, error) { + client := Comm(args) + client.AppAuthToken = args["app_auth_token"] + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_agreement_no", args["out_agreement_no"]) + }) + + var aliPsp md.ZhimaCreditPayafteruseCreditagreementQuery + bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditagreement.query", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := bs.(*md.ZhimaCreditPayafteruseCreditagreementQuery) + return res, nil +} + +//信用下单 +func CreditagreementOrder(args map[string]string) (*md.ZhimaCreditPayafteruseCreditbizorderOrder, error) { + client := Comm(args) + client.AppAuthToken = args["app_auth_token"] + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_order_no", args["out_order_no"]) + bz.Set("credit_agreement_id", args["credit_agreement_id"]) + bz.Set("category_id", args["category_id"]) + bz.Set("order_amount", args["order_amount"]) + bz.Set("amount_type", args["amount_type"]) + bz.Set("subject", args["subject"]) + }) + + var aliPsp md.ZhimaCreditPayafteruseCreditbizorderOrder + bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditbizorder.order", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := bs.(*md.ZhimaCreditPayafteruseCreditbizorderOrder) + return res, nil +} + +//查询信用订单 +func CreditagreementOrderQuery(args map[string]string) (*md.ZhimaCreditPayafteruseCreditbizorderQuery, error) { + client := Comm(args) + client.AppAuthToken = args["app_auth_token"] + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("credit_biz_order_id", args["credit_biz_order_id"]) + }) + + var aliPsp md.ZhimaCreditPayafteruseCreditbizorderQuery + bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditbizorder.query", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := bs.(*md.ZhimaCreditPayafteruseCreditbizorderQuery) + return res, nil +} + +//转账 +func AlipayFundTransUniTransfer(args map[string]string) (*md.AlipayFundTransUniTransfer, error) { + client := Comm(args) + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + payeeInfo := map[string]string{ + "identity": args["identity"], + "identity_type": args["identity_type"], + "name": args["name"], + } + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_biz_no", args["out_biz_no"]) + bz.Set("trans_amount", args["trans_amount"]) + bz.Set("product_code", args["product_code"]) + bz.Set("biz_scene", args["biz_scene"]) + bz.Set("order_title", args["order_title"]) + bz.Set("payee_info", payeeInfo) + }) + + var aliPsp md.AlipayFundTransUniTransfer + bs, err := PostAliPayAPISelfV2(client, bm, "alipay.fund.trans.uni.transfer", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := bs.(*md.AlipayFundTransUniTransfer) + return res, nil +} + +//扣款 +func AlipayTradePay(args map[string]string) { + client := Comm(args) + if client == nil { + return + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + extendParams := map[string]string{ + "creditTradeScene": args["creditTradeScene"], + } + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("is_async_pay", args["is_async_pay"]) + bz.Set("total_amount", args["total_amount"]) + bz.Set("auth_code", args["auth_code"]) + bz.Set("scene", args["scene"]) + bz.Set("order_title", args["order_title"]) + bz.Set("extend_params", extendParams) + }) + var aliPsp = "" + PostAliPayAPISelfV2(client, bm, "alipay.trade.pay", &aliPsp) + + return +} +func AlipayTradeQuery(args map[string]string) (*md.AlipayFundTransUniTransfer, error) { + client := Comm(args) + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_trade_no", args["out_trade_no"]) + }) + + var aliPsp md.AlipayFundTransUniTransfer + bs, err := PostAliPayAPISelfV2(client, bm, "alipay.trade.query", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := bs.(*md.AlipayFundTransUniTransfer) + return res, nil +} + +//结束信用服务订单 +func CreditagreementOrderFinish(args map[string]string) (*md.ZhimaCreditPayafteruseCreditbizorderFinish, error) { + client := Comm(args) + client.AppAuthToken = args["app_auth_token"] + if client == nil { + return nil, errors.New("获取失败") + } + //请求参数 + bm := make(gopay.BodyMap) + // biz_content + bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_request_no", args["out_request_no"]) + bz.Set("credit_biz_order_id", args["credit_biz_order_id"]) + bz.Set("is_fulfilled", args["is_fulfilled"]) + bz.Set("remark", args["remark"]) + }) + + var aliPsp md.ZhimaCreditPayafteruseCreditbizorderFinish + bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditbizorder.finish", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := bs.(*md.ZhimaCreditPayafteruseCreditbizorderFinish) + return res, nil +} + +func PostAliPayAPISelfV2(a *alipay.Client, bm gopay.BodyMap, method string, aliRsp interface{}) (aliRsps interface{}, err error) { + var ( + bs, bodyBs []byte + ) + // check if there is biz_content + bz := bm.GetInterface("biz_content") + if bzBody, ok := bz.(gopay.BodyMap); ok { + if bodyBs, err = json.Marshal(bzBody); err != nil { + return aliRsp, fmt.Errorf("json.Marshal(%v):%w", bzBody, err) + } + bm.Set("biz_content", string(bodyBs)) + } + + if bs, err = doAliPaySelf(a, bm, method); err != nil { + return aliRsp, err + } + switch method { + case "alipay.trade.app.pay": + return string(bs), nil + case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify", "zhima.credit.payafteruse.creditagreement.sign": + return string(bs), nil + default: + if err = json.Unmarshal(bs, aliRsp); err != nil { + return aliRsp, err + } + return aliRsp, nil + } +} + +// 向支付宝发送自定义请求 +func doAliPaySelf(a *alipay.Client, bm gopay.BodyMap, method string) (bs []byte, err error) { + var ( + url, sign string + ) + bm.Set("method", method) + + // check public parameter + checkPublicParam(a, bm) + + // check sign + if bm.GetString("sign") == "" { + sign, err = alipay.GetRsaSign(bm, bm.GetString("sign_type"), a.PrivateKeyType, a.PrivateKey) + if err != nil { + return nil, fmt.Errorf("GetRsaSign Error: %v", err) + } + bm.Set("sign", sign) + } + + if a.DebugSwitch == gopay.DebugOn { + req, _ := json.Marshal(bm) + xlog.Debugf("Alipay_Request: %s", req) + } + param := alipay.FormatURLParam(bm) + switch method { + case "alipay.trade.app.pay": + return []byte(param), nil + case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify", "zhima.credit.payafteruse.creditagreement.sign": + return []byte(baseUrl + "?" + param), nil + default: + httpClient := xhttp.NewClient() + url = baseUrlUtf8 + res, bs, errs := httpClient.Type(xhttp.TypeForm).Post(url).SendString(param).EndBytes() + if len(errs) > 0 { + return nil, errs[0] + } + if a.DebugSwitch == gopay.DebugOn { + xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs)) + } + if res.StatusCode != 200 { + return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode) + } + return bs, nil + } +} +func checkPublicParam(a *alipay.Client, bm gopay.BodyMap) { + bm.Set("format", "JSON") + + if bm.GetString("app_id") == "" && a.AppId != util.NULL { + bm.Set("app_id", a.AppId) + } + if bm.GetString("app_cert_sn") == "" && a.AppCertSN != util.NULL { + bm.Set("app_cert_sn", a.AppCertSN) + } + if bm.GetString("alipay_root_cert_sn") == "" && a.AliPayRootCertSN != util.NULL { + bm.Set("alipay_root_cert_sn", a.AliPayRootCertSN) + } + if bm.GetString("return_url") == "" && a.ReturnUrl != util.NULL { + bm.Set("return_url", a.ReturnUrl) + } + bm.Set("charset", "utf-8") + if bm.GetString("charset") == "" && a.Charset != util.NULL { + bm.Set("charset", a.Charset) + } + bm.Set("sign_type", alipay.RSA2) + if bm.GetString("sign_type") == "" && a.SignType != util.NULL { + bm.Set("sign_type", a.SignType) + } + bm.Set("timestamp", time.Now().Format(util.TimeLayout)) + + bm.Set("version", "1.0") + if bm.GetString("notify_url") == "" && a.NotifyUrl != util.NULL { + bm.Set("notify_url", a.NotifyUrl) + } + if bm.GetString("app_auth_token") == "" && a.AppAuthToken != util.NULL { + bm.Set("app_auth_token", a.AppAuthToken) + } + if bm.GetString("auth_token") == "" && a.AuthToken != util.NULL { + bm.Set("auth_token", a.AuthToken) + } +} From 75a90f71657e4db148f71d9a7e48315b59d0625e Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Tue, 11 Apr 2023 19:33:00 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay/alipay.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pay/alipay.go b/pay/alipay.go index 8ee0ea6..cdab74c 100644 --- a/pay/alipay.go +++ b/pay/alipay.go @@ -56,6 +56,7 @@ func Comm(args map[string]string) *alipay.Client { client.SetAliPayRootCertSN(aliPayRootCertSN) client.SetAliPayPublicCertSN(paySet.PayAlipayrsaPublicKey) } + client.IsProd = true fmt.Println(client) return client @@ -76,6 +77,18 @@ func OpenAuthTokenApp(args map[string]string) (*alipay.SystemOauthTokenResponse, } return payParam, nil } +func UserInfoShare(args map[string]string) (*alipay.UserInfoShareResponse, error) { + client := Comm(args) + client.AuthToken = args["auth_token"] + if client == nil { + return nil, errors.New("获取失败") + } + payParam, err := client.UserInfoShare() + if err != nil { + return nil, err + } + return payParam, nil +} func CreditagreementSign(args map[string]string) (string, error) { client := Comm(args) From 8228abe9c833a8a1b142d5cc5ffb0addd69318f9 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 12 Apr 2023 10:21:54 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay/alipay.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pay/alipay.go b/pay/alipay.go index cdab74c..3c878ab 100644 --- a/pay/alipay.go +++ b/pay/alipay.go @@ -235,11 +235,13 @@ func AlipayTradePay(args map[string]string) { "creditTradeScene": args["creditTradeScene"], } bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) { + bz.Set("out_trade_no", args["out_trade_no"]) bz.Set("is_async_pay", args["is_async_pay"]) bz.Set("total_amount", args["total_amount"]) bz.Set("auth_code", args["auth_code"]) + bz.Set("product_code", args["product_code"]) bz.Set("scene", args["scene"]) - bz.Set("order_title", args["order_title"]) + bz.Set("subject", args["subject"]) bz.Set("extend_params", extendParams) }) var aliPsp = "" From 0a9b0d3b30c551edf51f09984ebd567b65b8613d Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 12 Apr 2023 16:47:05 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay/alipay.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pay/alipay.go b/pay/alipay.go index 3c878ab..92dcd9e 100644 --- a/pay/alipay.go +++ b/pay/alipay.go @@ -225,6 +225,9 @@ func AlipayFundTransUniTransfer(args map[string]string) (*md.AlipayFundTransUniT //扣款 func AlipayTradePay(args map[string]string) { client := Comm(args) + if args["notify_url"] != "" { + client.NotifyUrl = args["notify_url"] + } if client == nil { return } From 14ea57f9ee82a7927ca88862725d6c756628ae27 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 12 Apr 2023 17:50:20 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md/alipay.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ pay/alipay.go | 16 +++++---- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/md/alipay.go b/md/alipay.go index 0dd45de..01dff9f 100644 --- a/md/alipay.go +++ b/md/alipay.go @@ -74,3 +74,96 @@ type ZhimaCreditPayafteruseCreditbizorderFinish struct { } `json:"zhima_credit_payafteruse_creditbizorder_finish_response"` Sign string `json:"sign"` } +type AlipayTradePay struct { + AlipayTradePayResponse struct { + Code string `json:"code"` + Msg string `json:"msg"` + TradeNo string `json:"trade_no"` + OutTradeNo string `json:"out_trade_no"` + OpenId string `json:"open_id"` + BuyerLogonId string `json:"buyer_logon_id"` + TransPayRate string `json:"trans_pay_rate"` + TotalAmount float64 `json:"total_amount"` + TransCurrency string `json:"trans_currency"` + SettleCurrency string `json:"settle_currency"` + SettleAmount string `json:"settle_amount"` + PayCurrency string `json:"pay_currency"` + PayAmount string `json:"pay_amount"` + SettleTransRate string `json:"settle_trans_rate"` + ReceiptAmount string `json:"receipt_amount"` + BuyerPayAmount float64 `json:"buyer_pay_amount"` + PointAmount float64 `json:"point_amount"` + InvoiceAmount float64 `json:"invoice_amount"` + GmtPayment string `json:"gmt_payment"` + FundBillList []struct { + FundChannel string `json:"fund_channel"` + BankCode string `json:"bank_code"` + Amount int `json:"amount"` + RealAmount float64 `json:"real_amount"` + FundType string `json:"fund_type"` + } `json:"fund_bill_list"` + CardBalance float64 `json:"card_balance"` + StoreName string `json:"store_name"` + DiscountGoodsDetail string `json:"discount_goods_detail"` + BuyerUserId string `json:"buyer_user_id"` + AsyncPaymentMode string `json:"async_payment_mode"` + VoucherDetailList []struct { + Id string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Amount int `json:"amount"` + MerchantContribute int `json:"merchant_contribute"` + OtherContribute int `json:"other_contribute"` + Memo string `json:"memo"` + TemplateId string `json:"template_id"` + OtherContributeDetail []struct { + ContributeType string `json:"contribute_type"` + ContributeAmount int `json:"contribute_amount"` + } `json:"other_contribute_detail"` + PurchaseBuyerContribute float64 `json:"purchase_buyer_contribute"` + PurchaseMerchantContribute float64 `json:"purchase_merchant_contribute"` + PurchaseAntContribute float64 `json:"purchase_ant_contribute"` + } `json:"voucher_detail_list"` + AdvanceAmount string `json:"advance_amount"` + AuthTradePayMode string `json:"auth_trade_pay_mode"` + ChargeAmount string `json:"charge_amount"` + ChargeFlags string `json:"charge_flags"` + SettlementId string `json:"settlement_id"` + BusinessParams string `json:"business_params"` + BuyerUserType string `json:"buyer_user_type"` + MdiscountAmount string `json:"mdiscount_amount"` + DiscountAmount string `json:"discount_amount"` + BuyerUserName string `json:"buyer_user_name"` + ReceiptCurrencyType string `json:"receipt_currency_type"` + CreditPayMode string `json:"credit_pay_mode"` + CreditBizOrderId string `json:"credit_biz_order_id"` + EnterprisePayInfo struct { + IsUseEnterprisePay bool `json:"is_use_enterprise_pay"` + InvoiceAmount int `json:"invoice_amount"` + BizInfo string `json:"biz_info"` + } `json:"enterprise_pay_info"` + CanTurnToAppPay string `json:"can_turn_to_app_pay"` + HybAmount string `json:"hyb_amount"` + BkagentRespInfo struct { + BindtrxId string `json:"bindtrx_id"` + BindclrissrId string `json:"bindclrissr_id"` + BindpyeracctbkId string `json:"bindpyeracctbk_id"` + BkpyeruserCode string `json:"bkpyeruser_code"` + EstterLocation string `json:"estter_location"` + } `json:"bkagent_resp_info"` + ChargeInfoList struct { + ChargeFee float64 `json:"charge_fee"` + OriginalChargeFee float64 `json:"original_charge_fee"` + SwitchFeeRate string `json:"switch_fee_rate"` + IsRatingOnTradeReceiver string `json:"is_rating_on_trade_receiver"` + IsRatingOnSwitch string `json:"is_rating_on_switch"` + ChargeType string `json:"charge_type"` + SubFeeDetailList []struct { + ChargeFee float64 `json:"charge_fee"` + OriginalChargeFee float64 `json:"original_charge_fee"` + SwitchFeeRate string `json:"switch_fee_rate"` + } `json:"sub_fee_detail_list"` + } `json:"charge_info_list"` + } `json:"alipay_trade_pay_response"` + Sign string `json:"sign"` +} diff --git a/pay/alipay.go b/pay/alipay.go index 92dcd9e..d37a8ea 100644 --- a/pay/alipay.go +++ b/pay/alipay.go @@ -223,13 +223,13 @@ func AlipayFundTransUniTransfer(args map[string]string) (*md.AlipayFundTransUniT } //扣款 -func AlipayTradePay(args map[string]string) { +func AlipayTradePay(args map[string]string) (*md.AlipayTradePay, error) { client := Comm(args) if args["notify_url"] != "" { client.NotifyUrl = args["notify_url"] } if client == nil { - return + return nil, errors.New("获取失败") } //请求参数 bm := make(gopay.BodyMap) @@ -247,10 +247,14 @@ func AlipayTradePay(args map[string]string) { bz.Set("subject", args["subject"]) bz.Set("extend_params", extendParams) }) - var aliPsp = "" - PostAliPayAPISelfV2(client, bm, "alipay.trade.pay", &aliPsp) - - return + var aliPsp md.AlipayTradePay + v2, err := PostAliPayAPISelfV2(client, bm, "alipay.trade.pay", &aliPsp) + if err != nil { + xlog.Error(err) + return nil, err + } + res := v2.(*md.AlipayTradePay) + return res, nil } func AlipayTradeQuery(args map[string]string) (*md.AlipayFundTransUniTransfer, error) { client := Comm(args) From 085582175c189edf027072f8f162d0ffcd2f9518 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Mon, 17 Apr 2023 14:52:36 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay/alipay.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pay/alipay.go b/pay/alipay.go index d37a8ea..de70fe1 100644 --- a/pay/alipay.go +++ b/pay/alipay.go @@ -103,6 +103,7 @@ func CreditagreementSign(args map[string]string) (string, error) { bz.Set("out_agreement_no", args["out_agreement_no"]) bz.Set("zm_service_id", args["zm_service_id"]) bz.Set("category_id", args["category_id"]) + bz.Set("return_back_link", args["return_back_link"]) }) aliPsp := "" From a88080cf9981e7d9e4acf99a0401f67929081689 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Thu, 11 May 2023 18:54:32 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md/boc.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/md/boc.go b/md/boc.go index d6abdb3..3371809 100644 --- a/md/boc.go +++ b/md/boc.go @@ -110,10 +110,11 @@ type CreateQRTradeReq struct { type CreateQRTradeResponse struct { MacaoBOCPublicResponse - LogNo string `json:"logNo"` - Amount string `json:"amount"` - Result string `json:"result"` - PayURL string `json:"payUrl"` + LogNo string `json:"logNo"` + Amount string `json:"amount"` + Result string `json:"result"` + PayURL string `json:"payUrl"` + PayQrcode string `json:"payQrcode"` } type OfflineResultResponse struct { From cae439d4da55b9c4574d58382e912b47d8d3956d Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 17 May 2023 15:08:21 +0800 Subject: [PATCH 11/15] test --- lib/local_alipay/api.go | 54 +++++++++++++++++++++++++++++++++++++++++ pay/pay_by_own.go | 15 +++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/local_alipay/api.go b/lib/local_alipay/api.go index 2723e41..c2d56c0 100644 --- a/lib/local_alipay/api.go +++ b/lib/local_alipay/api.go @@ -65,6 +65,60 @@ func TradeAppPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS str } return payParam, nil } +func TradeAppPc(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (string, error) { + //初始化支付宝客户端 + // appID 是在支付宝申请的APPID + // priKey 是支付宝私钥 + // subject 是支付订单的主题 + // orderID 是智莺这边生成的订单id + // amount 是付费金额 + // notiURL 通知地址url + // passback_params 回调通知参数 + + client := alipay.NewClient(appID, priKey, true) + client.DebugSwitch = gopay.DebugOn + //判断密钥的类型 + rsa_type := alipay.RSA2 + pkcs_type := alipay.PKCS1 + if RSA == "1" { + rsa_type = alipay.RSA + } + if PKCS == "1" { + pkcs_type = alipay.PKCS8 + } + if paySet.PayAliUseType == "1" { + rsa_type = alipay.RSA2 + pkcs_type = alipay.PKCS8 + } + //配置公共参数 + client.SetCharset("utf-8"). + SetSignType(rsa_type). + SetPrivateKeyType(pkcs_type) + if notiURL != "" { + client.SetNotifyUrl(notiURL) + } + //新支付宝支付 + if paySet.PayAliUseType == "1" { + client.SetAppCertSN(paySet.PayAppCertSn) + aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6" + client.SetAliPayRootCertSN(aliPayRootCertSN) + client.SetAliPayPublicCertSN(paySet.PayAlipayrsaPublicKey) + } + fmt.Println(client) + //请求参数 + body := make(gopay.BodyMap) + body.Set("subject", subject) + body.Set("out_trade_no", orderID) + body.Set("total_amount", amount) + body.Set("timeout_express", "30m") + body.Set("product_code", "FAST_INSTANT_TRADE_PAY") + // body.Set("passback_params", orderID) + payParam, err := client.TradePagePay(body) + if err != nil { + return "", err + } + return payParam, nil +} // TradeAppPay is 支付宝H5支付 func TradeWapPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS, page_url string, paySet *md.PayData) (string, error) { diff --git a/pay/pay_by_own.go b/pay/pay_by_own.go index 7f6b174..7e9af1b 100644 --- a/pay/pay_by_own.go +++ b/pay/pay_by_own.go @@ -51,7 +51,20 @@ func AlipayApp(args map[string]string) (string, error) { } return zhios_pay_utils.AnyToString(param), nil } - +func AlipayPc(args map[string]string) (string, error) { + paySet := AlipayCofing(args) + if args["private_key"] == "" || args["app_id"] == "" { + return "", errors.New("请在后台正确配置支付宝") + } + param, err := local_alipay.TradeAppPc(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) { paySet := AlipayCofing(args) if args["private_key"] == "" || args["app_id"] == "" { From d047cded6ba9ba07326929eb3fa5076826d062d1 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 17 May 2023 15:28:07 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/local_wechat/api.go | 32 ++++++++++++++++++++++++++++++++ pay/pay_by_own.go | 5 +++++ 2 files changed, 37 insertions(+) diff --git a/lib/local_wechat/api.go b/lib/local_wechat/api.go index b89835a..a37486f 100644 --- a/lib/local_wechat/api.go +++ b/lib/local_wechat/api.go @@ -124,6 +124,38 @@ func TradeH5Pay(client *wechat.Client, subject, orderID, amount, notifyUrl strin } return r, nil } +func TradePcPay(client *wechat.Client, subject, orderID, amount, notifyUrl, productId 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("product_id", productId). + Set("trade_type", wechat.TradeType_Native). + Set("sign_type", wechat.SignType_MD5) + // 预下单 + 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) { diff --git a/pay/pay_by_own.go b/pay/pay_by_own.go index 7e9af1b..7d7b391 100644 --- a/pay/pay_by_own.go +++ b/pay/pay_by_own.go @@ -108,6 +108,11 @@ func WxH5Pay(params map[string]string) (map[string]string, error) { r, err := local_wxpay.TradeH5Pay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"]) return r, err } +func WxPcPay(params map[string]string) (map[string]string, error) { + client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true) + r, err := local_wxpay.TradePcPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["product_id"]) + return r, err +} // 微信小程序v2 func WxMiniProgPay(params map[string]string) (map[string]string, error) { From 7d2146b6c2e526893115060bbaf579e89a6587f3 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 17 May 2023 17:39:18 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/local_wechat/api.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/local_wechat/api.go b/lib/local_wechat/api.go index a37486f..1075271 100644 --- a/lib/local_wechat/api.go +++ b/lib/local_wechat/api.go @@ -142,17 +142,8 @@ func TradePcPay(client *wechat.Client, subject, orderID, amount, notifyUrl, prod _ = 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, + "code_url": wxRsp.CodeUrl, } return r, nil } From d7fc1bd06983ec02afa0d637249cbcdb280db8d9 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 17 May 2023 17:42:42 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/local_wechat/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/local_wechat/api.go b/lib/local_wechat/api.go index 1075271..432fb56 100644 --- a/lib/local_wechat/api.go +++ b/lib/local_wechat/api.go @@ -143,7 +143,7 @@ func TradePcPay(client *wechat.Client, subject, orderID, amount, notifyUrl, prod return nil, err } r := map[string]string{ - "code_url": wxRsp.CodeUrl, + "payQrcode": wxRsp.CodeUrl, } return r, nil } From 80a0e5836b2ad1fc97103452a977054374b9d9cd Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 17 May 2023 18:23:04 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 + lib/local_wechat/api.go | 9 +++- utils/qrcode/decodeFile.go | 33 +++++++++++++++ utils/qrcode/getBase64.go | 43 +++++++++++++++++++ utils/qrcode/saveFile.go | 85 ++++++++++++++++++++++++++++++++++++++ utils/qrcode/writeWeb.go | 39 +++++++++++++++++ 6 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 utils/qrcode/decodeFile.go create mode 100644 utils/qrcode/getBase64.go create mode 100644 utils/qrcode/saveFile.go create mode 100644 utils/qrcode/writeWeb.go diff --git a/go.mod b/go.mod index 76a8640..9d10b9b 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,14 @@ module code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git go 1.15 require ( + github.com/boombuler/barcode v1.0.1 github.com/forgoer/openssl v1.2.1 github.com/gin-gonic/gin v1.8.0 github.com/go-redis/redis v6.15.9+incompatible github.com/go-sql-driver/mysql v1.6.0 github.com/gomodule/redigo v1.8.8 github.com/iGoogle-ink/gopay v1.5.36 + github.com/makiuchi-d/gozxing v0.1.1 github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.2.0 github.com/syyongx/php2go v0.9.7 diff --git a/lib/local_wechat/api.go b/lib/local_wechat/api.go index 432fb56..54a95f1 100644 --- a/lib/local_wechat/api.go +++ b/lib/local_wechat/api.go @@ -3,12 +3,14 @@ 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" + "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils/qrcode" "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" + "strings" "time" ) @@ -142,8 +144,13 @@ func TradePcPay(client *wechat.Client, subject, orderID, amount, notifyUrl, prod _ = zhios_pay_logx.Warn(err) return nil, err } + QRcode := "" + if wxRsp.CodeUrl != "" { + QRcode = qrcode.GetPNGBase64(wxRsp.CodeUrl) + } + QRcode = strings.ReplaceAll(QRcode, "\u0000", "") r := map[string]string{ - "payQrcode": wxRsp.CodeUrl, + "payQrcode": QRcode, } return r, nil } diff --git a/utils/qrcode/decodeFile.go b/utils/qrcode/decodeFile.go new file mode 100644 index 0000000..f50fb28 --- /dev/null +++ b/utils/qrcode/decodeFile.go @@ -0,0 +1,33 @@ +package qrcode + +import ( + "image" + _ "image/jpeg" + _ "image/png" + "os" + + "github.com/makiuchi-d/gozxing" + "github.com/makiuchi-d/gozxing/qrcode" +) + +func DecodeFile(fi string) (string, error) { + file, err := os.Open(fi) + if err != nil { + return "", err + } + img, _, err := image.Decode(file) + if err != nil { + return "", err + } + // prepare BinaryBitmap + bmp, err := gozxing.NewBinaryBitmapFromImage(img) + if err != nil { + return "", err + } + // decode image + result, err := qrcode.NewQRCodeReader().Decode(bmp, nil) + if err != nil { + return "", err + } + return result.String(), nil +} diff --git a/utils/qrcode/getBase64.go b/utils/qrcode/getBase64.go new file mode 100644 index 0000000..11d149c --- /dev/null +++ b/utils/qrcode/getBase64.go @@ -0,0 +1,43 @@ +package qrcode + +// 生成登录二维码图片, 方便在网页上显示 + +import ( + "bytes" + "encoding/base64" + "image/jpeg" + "image/png" + + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/qr" +) + +func GetJPGBase64(content string, edges ...int) string { + edgeLen := 300 + if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 { + edgeLen = edges[0] + } + img, _ := qr.Encode(content, qr.L, qr.Unicode) + img, _ = barcode.Scale(img, edgeLen, edgeLen) + + emptyBuff := bytes.NewBuffer(nil) // 开辟一个新的空buff缓冲区 + jpeg.Encode(emptyBuff, img, nil) + dist := make([]byte, 50000) // 开辟存储空间 + base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) // buff转成base64 + return "data:image/png;base64," + string(dist) // 输出图片base64(type = []byte) +} + +func GetPNGBase64(content string, edges ...int) string { + edgeLen := 300 + if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 { + edgeLen = edges[0] + } + img, _ := qr.Encode(content, qr.L, qr.Unicode) + img, _ = barcode.Scale(img, edgeLen, edgeLen) + + emptyBuff := bytes.NewBuffer(nil) // 开辟一个新的空buff缓冲区 + png.Encode(emptyBuff, img) + dist := make([]byte, 50000) // 开辟存储空间 + base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) // buff转成base64 + return string(dist) // 输出图片base64(type = []byte) +} diff --git a/utils/qrcode/saveFile.go b/utils/qrcode/saveFile.go new file mode 100644 index 0000000..4854783 --- /dev/null +++ b/utils/qrcode/saveFile.go @@ -0,0 +1,85 @@ +package qrcode + +// 生成登录二维码图片 + +import ( + "errors" + "image" + "image/jpeg" + "image/png" + "os" + "path/filepath" + "strings" + + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/qr" +) + +func SaveJpegFile(filePath, content string, edges ...int) error { + edgeLen := 300 + if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 { + edgeLen = edges[0] + } + img, _ := qr.Encode(content, qr.L, qr.Unicode) + img, _ = barcode.Scale(img, edgeLen, edgeLen) + + return writeFile(filePath, img, "jpg") +} + +func SavePngFile(filePath, content string, edges ...int) error { + edgeLen := 300 + if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 { + edgeLen = edges[0] + } + img, _ := qr.Encode(content, qr.L, qr.Unicode) + img, _ = barcode.Scale(img, edgeLen, edgeLen) + + return writeFile(filePath, img, "png") +} + +func writeFile(filePath string, img image.Image, format string) error { + if err := createDir(filePath); err != nil { + return err + } + file, err := os.Create(filePath) + defer file.Close() + if err != nil { + return err + } + switch strings.ToLower(format) { + case "png": + err = png.Encode(file, img) + break + case "jpg": + err = jpeg.Encode(file, img, nil) + default: + return errors.New("format not accept") + } + if err != nil { + return err + } + return nil +} + +func createDir(filePath string) error { + var err error + // filePath, _ = filepath.Abs(filePath) + dirPath := filepath.Dir(filePath) + dirInfo, err := os.Stat(dirPath) + if err != nil { + if !os.IsExist(err) { + err = os.MkdirAll(dirPath, 0777) + if err != nil { + return err + } + } else { + return err + } + } else { + if dirInfo.IsDir() { + return nil + } + return errors.New("directory is a file") + } + return nil +} diff --git a/utils/qrcode/writeWeb.go b/utils/qrcode/writeWeb.go new file mode 100644 index 0000000..57e1e92 --- /dev/null +++ b/utils/qrcode/writeWeb.go @@ -0,0 +1,39 @@ +package qrcode + +import ( + "bytes" + "image/jpeg" + "image/png" + "net/http" + + "github.com/boombuler/barcode" + "github.com/boombuler/barcode/qr" +) + +func WritePng(w http.ResponseWriter, content string, edges ...int) error { + edgeLen := 300 + if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 { + edgeLen = edges[0] + } + img, _ := qr.Encode(content, qr.L, qr.Unicode) + img, _ = barcode.Scale(img, edgeLen, edgeLen) + buff := bytes.NewBuffer(nil) + png.Encode(buff, img) + w.Header().Set("Content-Type", "image/png") + _, err := w.Write(buff.Bytes()) + return err +} + +func WriteJpg(w http.ResponseWriter, content string, edges ...int) error { + edgeLen := 300 + if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 { + edgeLen = edges[0] + } + img, _ := qr.Encode(content, qr.L, qr.Unicode) + img, _ = barcode.Scale(img, edgeLen, edgeLen) + buff := bytes.NewBuffer(nil) + jpeg.Encode(buff, img, nil) + w.Header().Set("Content-Type", "image/jpg") + _, err := w.Write(buff.Bytes()) + return err +}