From 83c385822b0d6a50df96fc8170217c2d58d22aed Mon Sep 17 00:00:00 2001 From: Jerry <85411418@qq.com> Date: Fri, 29 Jan 2021 15:30:28 +0800 Subject: [PATCH] update --- README.md | 21 +++++++++++++++++---- body_map.go | 38 -------------------------------------- body_map_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index a9ea0a7e..6e10b901 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -
# GoPay @@ -57,7 +56,7 @@ func main() { * APP纯签约-预签约接口-获取预签约ID(正式):client.EntrustAppPre() * H5纯签约(正式):client.EntrustH5() * 支付中签约(正式):client.EntrustPaying() -* 请求单次分账(正式):client.ProfitSharing() +* 请求单次分账(正式):client.ProfitSharing() * 请求多次分账(正式):client.MultiProfitSharing() * 查询分账结果(正式):client.ProfitSharingQuery() * 添加分账接收方(正式):client.ProfitSharingAddReceiver() @@ -126,7 +125,9 @@ func main() { --- ### 支付宝支付API + > #### 因支付宝接口太多,如没实现的接口,还请开发者自行调用client.PostAliPayAPISelf()方法实现! + * 支付宝接口自行实现方法:client.PostAliPayAPISelf() * 手机网站支付接口2.0(手机网站支付):client.TradeWapPay() * 统一收单下单并支付页面接口(电脑网站支付):client.TradePagePay() @@ -277,7 +278,7 @@ client.AddCertFileContent() ```go import ( - "github.com/iGoogle-ink/gopay/alipay" +"github.com/iGoogle-ink/gopay/alipay" ) // 初始化支付宝客户端 @@ -309,8 +310,10 @@ err := client.SetCertSnByPath("appCertPublicKey.crt", "alipayRootCert.crt", "ali ## 2、初始化并赋值BodyMap(client的方法所需的入参) * #### 微信请求参数 +* #### 微信接口通用参数(mch_id、appid、sign)无需传入,client 请求时会默认处理 具体参数请根据不同接口查看:[微信支付接口文档](https://pay.weixin.qq.com/wiki/doc/api/index.html) + ```go import ( "github.com/iGoogle-ink/gopay/wechat" @@ -345,6 +348,7 @@ bm.Set("sign", sign) * #### 支付宝请求参数 具体参数请根据不同接口查看:[支付宝支付API接口文档](https://opendocs.alipay.com/apis/api_1/alipay.trade.wap.pay) + ```go // 初始化 BodyMap bm := make(gopay.BodyMap) @@ -357,7 +361,8 @@ bm.Set("subject", "手机网站测试支付"). ## 3、client 方法调用 -* #### 微信 client +* #### 微信 client + ```go wxRsp, err := client.UnifiedOrder(bm) wxRsp, err := client.Micropay(bm) @@ -373,6 +378,7 @@ wxRsp, err := client.Transfer(bm, "apiclient_cert.pem", "apiclient_key.pem", "ap ``` * #### 支付宝 client + ```go // 手机网站支付是通过服务端获取支付URL后,然后返回给客户端,请求URL地址即可打开支付页面 payUrl, err := client.TradeWapPay(bm) @@ -412,11 +418,13 @@ aliRsp, err := client.UserCertifyOpenQuery(bm) ## 4、微信统一下单后,获取微信小程序支付、APP支付、微信内H5支付所需要的 paySign * #### 微信(只有微信需要此操作) + 微信小程序支付官方文档:[微信小程序支付API](https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html) APP支付官方文档:[APP端调起支付的参数列表文档](https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12) 微信内H5支付官方文档:[微信内H5支付文档](https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=7_7&index=6) + ```go import ( "github.com/iGoogle-ink/gopay/wechat" @@ -471,6 +479,7 @@ paySign := wechat.GetH5PaySign(AppID, wxRsp.NonceStr, packages, wechat.SignType_ 异步通知处理完后,需回复平台固定数据 * #### 微信 + ```go import ( "github.com/iGoogle-ink/gopay" @@ -528,6 +537,7 @@ c.String(http.StatusOK, "%s", rsp.ToXmlString()) // 此写法是 gin 框架 注意:APP支付、手机网站支付、电脑网站支付 暂不支持同步返回验签 支付宝支付后的同步/异步通知验签文档:[支付结果通知](https://opendocs.alipay.com/open/200/106120) + ```go import ( "github.com/iGoogle-ink/gopay/alipay" @@ -574,6 +584,7 @@ c.String(http.StatusOK, "%s", "success") // 此写法是 gin 框架返回支 button按钮获取手机号码:[button组件文档](https://developers.weixin.qq.com/miniprogram/dev/component/button.html) 微信解密算法文档:[解密算法文档](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html) + ```go import ( "github.com/iGoogle-ink/gopay/wechat" @@ -656,12 +667,14 @@ xlog.Debug(*phone) ``` ## 讲究的朋友可以给个赞赏 + 微信: 支付宝: ## License + ``` Copyright 2019 Jerry diff --git a/body_map.go b/body_map.go index c4db66b8..96bcfc1d 100644 --- a/body_map.go +++ b/body_map.go @@ -57,44 +57,6 @@ func (bm BodyMap) Reset() { } } -// GetArrayMap 获取获取的key对应的值,并尝试转换为 []BodyMap -// 在请求单次分账接口输入参数有用到 https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_1&index=1 -func (bm BodyMap) GetArrayBodyMap(key string) (array []BodyMap, err error) { - if bm == nil { - return nil, errors.New("param is nil") - } - mu.RLock() - defer mu.RUnlock() - value, ok := bm[key] - if !ok { - return nil, errors.New("param is not exist") - } - array, ok = value.([]BodyMap) - if !ok { - return nil, errors.New("can not transfer " + key + " to []BodyMap") - } - return -} - -// GetBodyMap 获取获取的key对应的值,并尝试转换为 BodyMap -// 在添加分账接收方会用到 https://pay.weixin.qq.com/wiki/doc/api/allocation.php?chapter=27_3&index=4 -func (bm BodyMap) GetBodyMap(key string) (b BodyMap, err error) { - if bm == nil { - return nil, errors.New("param is nil") - } - mu.RLock() - defer mu.RUnlock() - value, ok := bm[key] - if !ok { - return nil, errors.New("param is not exist") - } - b, ok = value.(BodyMap) - if !ok { - return nil, errors.New("can not transfer " + key + " to BodyMap") - } - return -} - func (bm BodyMap) JsonBody() (jb string) { bs, err := json.Marshal(bm) if err != nil { diff --git a/body_map_test.go b/body_map_test.go index dcdefe11..28ded71d 100644 --- a/body_map_test.go +++ b/body_map_test.go @@ -1,8 +1,11 @@ package gopay import ( + "encoding/json" + "encoding/xml" "testing" + "github.com/iGoogle-ink/gotil" "github.com/iGoogle-ink/gotil/xlog" ) @@ -72,3 +75,42 @@ func TestBodyMapMarshal(t *testing.T) { jb2 := bm.JsonBody() xlog.Debug("jb2:", jb2) } + +func TestBodyMapMarshalSlice(t *testing.T) { + type Receiver struct { + Type string `json:"type"` + Account string `json:"account"` + Amount int `json:"amount"` + Description string `json:"description"` + } + var rs []*Receiver + item := &Receiver{ + Type: "MERCHANT_ID", + Account: "190001001", + Amount: 100, + Description: "分到商户", + } + rs = append(rs, item) + item2 := &Receiver{ + Type: "PERSONAL_OPENID", + Account: "86693952", + Amount: 888, + Description: "分到个人", + } + rs = append(rs, item2) + bs, _ := json.Marshal(rs) + + bm := make(BodyMap) + bm.Set("nonce_str", gotil.GetRandomString(32)). + Set("transaction_id", "4208450740201411110007820472"). + Set("out_order_no", "P20150806125346") + + bm.Set("receivers", string(bs)) + + //receiver := make(BodyMap) + //receiver.Set("receiver", string(bs)) + // + //body := receiver.JsonBody() + bss, _ := xml.Marshal(bm) + xlog.Debug("body:", string(bss)) +}