From e5caf7cd728e1805d5f4ea241ed2af02cce7a5e7 Mon Sep 17 00:00:00 2001 From: jiaoboxiang Date: Thu, 8 Dec 2022 14:09:42 +0800 Subject: [PATCH] =?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/fb_pay/api.go | 92 +++++++++++++++++++++++++++++++++++++ lib/fb_pay/fb_pay.go | 105 +++++++++++++++++++++++++++++++++++++++++++ test/ada_pay_test.go | 19 ++++++++ test/fb_pay_test.go | 79 ++++++++++++++++++++++++++++++++ 4 files changed, 295 insertions(+) create mode 100644 lib/fb_pay/api.go create mode 100644 lib/fb_pay/fb_pay.go create mode 100644 test/fb_pay_test.go diff --git a/lib/fb_pay/api.go b/lib/fb_pay/api.go new file mode 100644 index 0000000..2249c9f --- /dev/null +++ b/lib/fb_pay/api.go @@ -0,0 +1,92 @@ +package fb_pay + +const ( + ReqHeader = "application/json; charset=utf-8" + TestUrl = "https://shq-api.51fubei.com/gateway/agent" + PrdUrl = "https://shq-api.51fubei.com/gateway/agent" + //Methods 接口名称 + + Unification = "fbpay.order.create" //统一支付 + OrderQrcode = "fbpay.order.qrcode" //支付宝跳转支付 + AggregateCode = "fbpay.fixed.qrcode.create" //聚合码支付 + PaymentCode = "fbpay.order.pay" //付款码支付 + OrderQuery = "fbpay.order.query" //订单查询 + Refund = "fbpay.order.refund" //退款 + RefundQuery = "fbpay.order.refund.query" //退款查询 + WechatPaymentAuth = "openapi.agent.merchant.wechat.payment.auth" //微信网页授权 + UnionPaymentAuth = "openapi.agent.merchant.union.payment.auth" //银联云闪付授权 + +) + +type BaseParameters struct { + VendorSn string `json:"vendor_sn" label:"服务商开放平台id(服务商后台生成),服务商级接入该字段必填,与app_id二选一,根据接口级别进行判定"` + AppId string `json:"app_id" label:"商户开放平台id(商户后台生成),商户级接入该字段必填,与vendor_sn二选一,根据接口级别进行判定"` + Method string `json:"method" label:"接口名称"` + Format string `json:"format" label:"接口格式,默认json"` + SignMethod string `json:"sign_method" label:"签名算法,默认md5"` + Nonce string `json:"nonce" label:"请求端随机生成数"` + Version string `json:"version" label:"接口版本,默认1.0"` + BizContent string `json:"biz_content" label:"请求参数的集合,除公共参数外所有请求参数都必须放在这个参数中"` +} + +type RequestParameters struct { + BaseParameters + Sign string `json:"sign" label:"签名"` +} + +type OrderCreateParameters struct { + OrderCreateMustParameters + PayWay string `json:"pay_way" label:"02:公众号,03:小程序"` + StoreId int64 `json:"store_id" label:"商户门店号(如果只有一家有效门店,可不传)"` + CashierId int64 `json:"cashier_id" label:"收银员ID"` + SubAppid string `json:"sub_appid" label:"公众号appid/小程序appid。当微信支付时,该字段必填(user_id需要保持一致,即为该公众号appid获取的)间联商户需提前配置对应公众号appid,对应配置接口"` + Royalty int `json:"royalty" label:"1 分账自定义打标参数,1:参与分账(需开白名单),不传则默认不参与分账"` + GoodsTag string `json:"goods_tag" label:"订单优惠标记,代金券或立减优惠功能的参数(使用单品券时必传)"` + Detail string `json:"detail" label:"订单包含的商品信息,Json格式。当当微信支付或者支付宝支付时时可选填此字段。对于使用单品优惠的商户,该字段必须按照规范上传,详见“单品优惠参数说明”"` + DeviceNo string `json:"device_no" label:"终端号"` + Body string `json:"body" label:"商品描述"` + Attach string `json:"attach" label:"附加数据,原样返回,该字段主要用于商户携带订单的自定义数据"` + TimeoutExpress string `json:"timeout_express" label:"订单失效时间,逾期将关闭交易。格式为yyyyMMddHHmmss,失效时间需大于1分钟。银联暂不支持"` + NotifyUrl string `json:"notify_url" label:"支付回调地址"` + AlipayExtendParams string `json:"alipay_extend_params" label:"支付宝业务拓展参数--花呗分期"` + PlatformStoreId string `json:"platform_store_id" label:"平台方门店号(即微信/支付宝的storeid)"` + DisablePayChannels string `json:"disable_pay_channels" label:"禁止使用优惠券标识,promotion-支付宝优惠(包含实时优惠+商户优惠)"` + Remark string `json:"remark" label:"备注"` +} + +type OrderCreateMustParameters struct { + MerchantOrderSn string `json:"merchant_order_sn" binding:"required" label:"外部系统订单号(确保唯一,前后不允许带空格)"` + PayType string `json:"pay_type" binding:"required" label:"支付方式,,wxpay 微信,,alipay 支付宝,unionpay 银联云闪付"` + TotalAmount float64 `json:"total_amount" binding:"订单总金额,单位为元,精确到0.01 ~ 10000000"` + UserId string `json:"user_id" binding:"required" label:"用户标识,微信openid.支付宝userid,银联云闪付upUserId"` + MerchantId int64 `json:"merchant_id" label:"付呗商户号,以服务商级接入时必传,以商户级接入时不传"` +} + +type OrderCreateDetailParameters struct { + CostPrice int64 `json:"cost_price" label:"1.商户侧一张小票订单可能被分多次支付,订单原价用于记录整张小票的交易金额。2.当订单原价与支付金额不相等,则不享受优惠。3.该字段主要用于防止同一张小票分多次支付,以享受多次优惠的情况,正常支付订单不必上传此参数。"` + ReceiptId string `json:"receipt_id" label:"商家小票ID"` + GoodsDetail string `json:"total_amount" binding:"单品信息,使用Json数组格式提交,见下文说明"` +} +type OrderCreateDetailGoodsDetailParameters struct { + GoodsId string `json:"goods_id" binding:"required" label:"商品编码"` + GoodsName string `json:"goods_name" binding:"required" label:"商品名称"` + Quantity int `json:"quantity" binding:"required" label:"商品数量"` + Price int64 `json:"price" binding:"required" label:"商品单价,单位为分"` +} + +type OrderQrcodeParameters struct { + OrderQrcodeMustParameters + //CashierId int64 `json:"cashier_id" label:"收银员ID"` + DeviceNo string `json:"device_no" label:"终端号"` + Body string `json:"body" label:"商品描述"` + Attach string `json:"attach" label:"附加数据,原样返回,该字段主要用于商户携带订单的自定义数据"` + NotifyUrl string `json:"notify_url" label:"支付回调地址"` + AlipayExtendParams string `json:"alipay_extend_params" label:"支付宝业务拓展参数--花呗分期"` +} + +type OrderQrcodeMustParameters struct { + MerchantOrderSn string `json:"merchant_order_sn" binding:"required" label:"外部系统订单号(确保唯一,前后不允许带空格)"` + PayType string `json:"pay_type" binding:"required" label:"支付方式,alipay 支付宝 (目前只支持支付宝扫码)"` + TotalAmount float64 `json:"total_amount" binding:"订单总金额,单位为元,精确到0.01 ~ 10000000"` + StoreId int64 `json:"store_id" binding:"required" label:"商户门店号"` +} diff --git a/lib/fb_pay/fb_pay.go b/lib/fb_pay/fb_pay.go new file mode 100644 index 0000000..d551c9d --- /dev/null +++ b/lib/fb_pay/fb_pay.go @@ -0,0 +1,105 @@ +package fb_pay + +import ( + zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils" + "errors" + "fmt" + "strings" +) + +func GetUnificationByVendorSn(vendorSn string) *RequestParameters { + var rp RequestParameters + rp.VendorSn = vendorSn + rp.Method = Unification + return &rp +} + +func GetOrderQrcodeByVendorSn(vendorSn string) *RequestParameters { + var rp RequestParameters + rp.VendorSn = vendorSn + rp.Method = OrderQrcode + return &rp +} + +func GetAggregateCodeByVendorSn(vendorSn string) *RequestParameters { + var rp RequestParameters + rp.VendorSn = vendorSn + rp.Method = AggregateCode + return &rp +} + +func GetRequestParametersByVendorSn(vendorSn string) *RequestParameters { + var rp RequestParameters + rp.VendorSn = vendorSn + return &rp +} + +func GetUnificationByAppId(appId string) *RequestParameters { + var rp RequestParameters + rp.AppId = appId + rp.Method = Unification + return &rp +} + +func GetOrderQrcodeByAppId(appId string) *RequestParameters { + var rp RequestParameters + rp.AppId = appId + rp.Method = OrderQrcode + return &rp +} + +func GetAggregateCodeByAppId(appId string) *RequestParameters { + var rp RequestParameters + rp.AppId = appId + rp.Method = AggregateCode + return &rp +} + +func GetRequestParametersByAppId(appId string) *RequestParameters { + var rp RequestParameters + rp.AppId = appId + return &rp +} + +func (bp *BaseParameters) SetBizContent(param interface{}) { + fmt.Println("参数:", zhios_pay_utils.SerializeStr(param)) + bp.BizContent = zhios_pay_utils.SerializeStr(param) +} + +func (bp *BaseParameters) GetSign(appSecret string) string { + struct2Map := make(map[string]string) + zhios_pay_utils.Unserialize(zhios_pay_utils.Serialize(bp), &struct2Map) + needSignStr := zhios_pay_utils.JoinStringsInASCII(struct2Map, "&", "=", false, false) + needSignStr = needSignStr + appSecret + return strings.ToUpper(zhios_pay_utils.Md5(needSignStr)) +} + +func (rp *RequestParameters) SetSign(appSecret string) { + rp.Sign = rp.BaseParameters.GetSign(appSecret) +} + +func (rp *RequestParameters) Send(prd bool) (string, error) { + if rp.Sign == "" { + return "", errors.New("请进行签名") + } + param := zhios_pay_utils.Struct2Map(rp) + for key, value := range param { + if value == "" { + delete(param, key) + } + } + url := TestUrl + if prd { + url = PrdUrl + } + headers := map[string]string{ + "Content-Type": ReqHeader, + } + zhios_pay_utils.CurlDebug = true + res, err := zhios_pay_utils.CurlPost(url, zhios_pay_utils.SerializeStr(param), headers) + if err != nil { + return "", err + } + fmt.Println(string(res)) + return string(res), nil +} diff --git a/test/ada_pay_test.go b/test/ada_pay_test.go index 41fcfa7..7872492 100644 --- a/test/ada_pay_test.go +++ b/test/ada_pay_test.go @@ -211,3 +211,22 @@ func TestCreateAliPayQr(t *testing.T) { } t.Log(zhios_pay_utils.SerializeStr(data)) } + +func TestPaymentQuery(t *testing.T) { + Adapay, err := adapay.GetAdapayByDBSysConfig("jiuhuicang", "app_a794b158-359b-4e5a-8464-5bd8f171bb6e", "api_live_419ec107-9854-45e8-af12-070efcef6ae9", "api_test_d6485653-c09e-405f-b386-8ef2936dfb97", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwN6xgd6Ad8v2hIIsQVnbt8a3JituR8o4Tc3B5WlcFR55bz4OMqrG/356Ur3cPbc2Fe8ArNd/0gZbC9q56Eb16JTkVNA/fye4SXznWxdyBPR7+guuJZHc/VW2fKH2lfZ2P3Tt0QkKZZoawYOGSMdIvO+WqK44updyax0ikK6JlNQIDAQAB", + "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMxVAwWM79+2Lx1TYA4muHy40AbwcBwXmvgNJGp2gq7bcDaNd5cmvXndwS6exV2hIGo8er0Qu3myGrwD/4BIoqCDSuqYfJkrlKzKM30+WAfwRulrFW+SOr6qCvGFF2bMdL884NMgd5PCpayASONq/blefQ3/Ic+c0gtZQxJzYzxLAgMBAAECgYEAyS+rGfv68zpeiXFeUpCtdaBCs5Jp0D8txq6p/GlTovdNNWl1Js5uIV/GpjCOA/JyCCgkcaPX86r5x8Xn/FeIRt/U1kkGs2zvKcxQntkOhmzMdDe7/FuwbRUUeM6LWGIc5AeQcBWYZeS8HAFMnlWho+knMqqgHC/fgUyon6YgMAECQQDyjJYSp+hdhTnvo88GPisQEPOoZRbrUnVFbwjD/OrQT7s6uSlbTAJRXGEYfdeaWsh2CoFl986pOqc/84E/Lr3LAkEA16nd1J3stk9XtC4i+iiXY0codiK24kU+pMcEm8NNyMV1nQT5iTcDA1ZeJCgamJZFeqcfyusimBNbCXdBKk2rgQJBAIymodAntkOlIjepEkBYhLhIXENme6fypTaicL7WR4SM99HR1f2vUhjELTn6n7BOvLhW1zq+PQU9kgcvud9dx4kCQFSMXtBFHZEXp/2WfNXv5fHg6sbtsx8gIH//GhpqxerpJsPpOF8H9yFu0beBFXQurYx5SqiF6GkQZYdffme0TYECQQDgOdQxysI1FEM/l8BDnQgmnO7sJqUtP9HsVcKElGdkJMZGqb8B5rt12y4dxpyVfyaTMZZZjqIx8zgQBhn+l0NJ") + if err != nil { + t.Error(err) + } + createPaymentParams := map[string]interface{}{ + "payment_id": "002112022102209243910429704781642039296", + } + + data, apiError, err := Adapay.Payment().Query(createPaymentParams) + if err != nil || apiError != nil { // 网络或本应用异常 + t.Error(err) + t.Error(apiError) + return + } + t.Log(zhios_pay_utils.SerializeStr(data)) +} diff --git a/test/fb_pay_test.go b/test/fb_pay_test.go new file mode 100644 index 0000000..3f1d984 --- /dev/null +++ b/test/fb_pay_test.go @@ -0,0 +1,79 @@ +package test + +import ( + fb "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/fb_pay" + zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils" + "testing" + "time" +) + +func TestFBPayUnification(t *testing.T) { + unification := fb.GetUnificationByVendorSn("2022120518415543840a") + var ordCreat fb.OrderCreateParameters + ordCreat.OrderCreateMustParameters.MerchantOrderSn = "202212061833-01" + ordCreat.OrderCreateMustParameters.TotalAmount = 0.1 + ordCreat.OrderCreateMustParameters.PayType = "alipay" + ordCreat.OrderCreateMustParameters.UserId = "userid" + ordCreat.OrderCreateMustParameters.MerchantId = 1979251 + unification.Nonce = ordCreat.OrderCreateMustParameters.MerchantOrderSn + time.Now().Format("20060102150405") + param := zhios_pay_utils.Struct2Map(ordCreat) + for key, value := range param { + if value == "" { + delete(param, key) + } + } + unification.SetBizContent(param) + unification.SetSign("c5d47b9e515594313d80fb9903ece2d8") + send, err := unification.Send(false) + if err != nil { + t.Error(err) + } + t.Logf("回调数据:%s\n", send) +} + +func TestFBPayOrderQrcode(t *testing.T) { + unification := fb.GetOrderQrcodeByVendorSn("2022120518415543840a") + var orderQrcode fb.OrderQrcodeParameters + orderQrcode.OrderQrcodeMustParameters.MerchantOrderSn = "20221208-02" + orderQrcode.OrderQrcodeMustParameters.TotalAmount = 0.1 + orderQrcode.OrderQrcodeMustParameters.PayType = "alipay" + orderQrcode.OrderQrcodeMustParameters.StoreId = 1331462 + unification.Nonce = orderQrcode.OrderQrcodeMustParameters.MerchantOrderSn + time.Now().Format("20060102150405") + param := zhios_pay_utils.Struct2Map(orderQrcode) + for key, value := range param { + if value == "" { + delete(param, key) + } + } + unification.SetBizContent(param) + unification.SetSign("c5d47b9e515594313d80fb9903ece2d8") + send, err := unification.Send(false) + if err != nil { + t.Error(err) + } + t.Logf("回调数据:%s\n", send) +} + +func TestFBPayOrderWxconfig(t *testing.T) { + requestParameters := fb.GetRequestParametersByVendorSn("2022120518415543840a") + requestParameters.Method = "fbpay.order.wxconfig" + requestParameters.SignMethod = "md5" + requestParameters.Nonce = time.Now().Format("20060102150405") + param := make(map[string]interface{}) + param["merchant_id"] = 1979251 + param["store_id"] = 1331462 + param["sub_appid"] = "wx43207c79f84a14ca" + param["account_type"] = "01" + 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) +}