Skip to content

Commit

Permalink
Merge pull request #12 from iGoogle-ink/gopay/feature
Browse files Browse the repository at this point in the history
update
  • Loading branch information
iGoogle-ink authored Nov 16, 2019
2 parents fcecf00 + bdbadb5 commit 3f1b42f
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 273 deletions.
12 changes: 6 additions & 6 deletions alipay_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ func (a *AliPayClient) AliPayTradePageRefund(body BodyMap) (aliRsp *AliPayTradeP

// alipay.trade.precreate(统一收单线下交易预创建)
// 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.precreate
func (a *AliPayClient) AliPayTradePrecreate(body BodyMap) (aliRsp *AlipayTradePrecreateResponse, err error) {
func (a *AliPayClient) AliPayTradePrecreate(body BodyMap) (aliRsp *AliPayTradePrecreateResponse, err error) {
var bs []byte
if body.Get("out_trade_no") == null {
return nil, errors.New("out_trade_no is not allowed to be null")
}
if bs, err = a.doAliPay(body, "alipay.trade.precreate"); err != nil {
return
}
aliRsp = new(AlipayTradePrecreateResponse)
aliRsp = new(AliPayTradePrecreateResponse)
if err = json.Unmarshal(bs, aliRsp); err != nil {
return nil, err
}
Expand Down Expand Up @@ -319,15 +319,15 @@ func (a *AliPayClient) AliPayTradePagePay(body BodyMap) (payUrl string, err erro

// alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)
// 文档地址:https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
func (a *AliPayClient) AlipayFundTransToaccountTransfer(body BodyMap) (aliRsp *AlipayFundTransToaccountTransferResponse, err error) {
func (a *AliPayClient) AliPayFundTransToaccountTransfer(body BodyMap) (aliRsp *AliPayFundTransToaccountTransferResponse, err error) {
var bs []byte
if body.Get("out_biz_no") == null {
return nil, errors.New("out_biz_no is not allowed to be null")
}
if bs, err = a.doAliPay(body, "alipay.fund.trans.toaccount.transfer"); err != nil {
return
}
aliRsp = new(AlipayFundTransToaccountTransferResponse)
aliRsp = new(AliPayFundTransToaccountTransferResponse)
if err = json.Unmarshal(bs, aliRsp); err != nil {
return nil, err
}
Expand Down Expand Up @@ -373,12 +373,12 @@ func (a *AliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySyste
// alipay.user.info.share(支付宝会员授权信息查询接口)
// body:此接口无需body参数
// 文档地址:https://docs.open.alipay.com/api_2/alipay.user.info.share
func (a *AliPayClient) AlipayUserInfoShare() (aliRsp *AlipayUserInfoShareResponse, err error) {
func (a *AliPayClient) AliPayUserInfoShare() (aliRsp *AliPayUserInfoShareResponse, err error) {
var bs []byte
if bs, err = a.doAliPay(nil, "alipay.user.info.share"); err != nil {
return nil, err
}
aliRsp = new(AlipayUserInfoShareResponse)
aliRsp = new(AliPayUserInfoShareResponse)
if err = json.Unmarshal(bs, aliRsp); err != nil {
return nil, err
}
Expand Down
42 changes: 39 additions & 3 deletions alipay_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ func TestJsonToString(t *testing.T) {
}

type People struct {
Name string `json:"name"`
Age int `json:"age"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
List []*struct {
Address string `json:"address,omitempty"`
Age int `json:"age,omitempty"`
Phone string `json:"phone,omitempty"`
} `json:"list,omitempty"`
}

func TestAliPayParams(t *testing.T) {
Expand All @@ -48,7 +53,7 @@ func TestAliPayParams(t *testing.T) {
fmt.Println("result:", bodyMap.Get("people"))
}

func TestVerifyAliPaySign(t *testing.T) {
func TestSyncVerifyAliPaySign(t *testing.T) {
signData := `{"code":"10000","msg":"Success","buyer_logon_id":"854***@qq.com","buyer_pay_amount":"0.01","buyer_user_id":"2088102363632794","fund_bill_list":[{"amount":"0.01","fund_channel":"PCREDIT"}],"gmt_payment":"2019-08-29 20:14:05","invoice_amount":"0.01","out_trade_no":"GZ201901301040361012","point_amount":"0.00","receipt_amount":"0.01","total_amount":"0.01","trade_no":"2019082922001432790585537960"}`
sign := "bk3SzX0CZRI811IJioS2XKQHcgMixUT8mYyGQj+vcOAQas7GIYi6LpykqqSc3m7+yvqoG0TdX/c2JjYnpw/J53JxtC2IC4vsLuIPIgghVo5qafsfSxEJ22w20RZDatI2dYqFVcj8Jp+4aesQ8zMMNw7cX9NLyk7kw3DecYeyQp+zrZMueZPqLh88Z+54G+e6QuSU++0ouqQVd4PkpPqy6YI+8MdMUX4Ve0jOQxMmYH8BC6n5ZsTH/uEaLEtzYVZdSw/xdSQ7K1SH73aEH8XbRYx6rL7RkKksrdvhezX+ThDjQ+fTWjvNFrGcg3fmqXRy2elvoalu+BQmqlkWWjEJYA=="
aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp8gueNlkbiDidz6FBQEBpqoRgH8h7JtsPtYW0nzAqy1MME4mFnDSMfSKlreUomS3a55gmBopL1eF4/Km/dEnaL5tCY9+24SKn1D4iyls+lvz/ZjvUjVwxoUYBh8kkcxMZSDeDz8//o+9qZTrICVP2a4sBB8T0XmU4gxfw8FsmtoomBH1nLk3AO7wgRN2a3+SRSAmxrhIGDmF1lljSlhY32eJpJ2TZQKaWNW+7yDBU/0Wt3kQVY84vr14yYagnSCiIfqyVFqePayRtmVJDr5qvSXr51tdqs2zKZCu+26X7JAF4BSsaq4gmY5DmDTm4TohCnBduI1+bPGD+igVmtl05wIDAQAB"
Expand All @@ -59,6 +64,37 @@ func TestVerifyAliPaySign(t *testing.T) {
}
}

func TestVerifyAliPaySign(t *testing.T) {
// 测试,假数据,无法验签通过
bm := make(BodyMap)
bm.Set("sign", "kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=")
bm.Set("sign_type", "RSA2")
bm.Set("total_amount", "2.00")
bm.Set("buyer_id", "2088102116773037")
bm.Set("body", "大乐透2.1")
bm.Set("trade_no", "2016071921001003030200089909")
bm.Set("refund_fee", "0.00")
bm.Set("notify_time", "2016-07-19 14:10:49")
bm.Set("subject", "大乐透2.1")
bm.Set("charset", "utf-8")
bm.Set("notify_type", "trade_status_sync")
bm.Set("out_trade_no", "0719141034-6418")
bm.Set("gmt_close", "2016-07-19 14:10:46")
bm.Set("gmt_payment", "2016-07-19 14:10:47")
bm.Set("trade_status", "TRADE_SUCCESS")
bm.Set("version", "1.0")
bm.Set("gmt_create", "2016-07-19 14:10:44")
bm.Set("app_id", "2015102700040153")
bm.Set("seller_id", "2088102119685838")
bm.Set("notify_id", "4a91b7a78a503640467525113fb7d8bg8e")

ok, err := VerifyAliPaySign("aliPayPublicKey", bm)
if err != nil {
fmt.Println("err:", err)
return
}
fmt.Println("OK:", ok)
}
func TestSubString(t *testing.T) {
str := `{"alipay_trade_pay_response":{"code":"10000","msg":"Success","buyer_logon_id":"854***@qq.com","buyer_pay_amount":"0.01","buyer_user_id":"2088102363632794","fund_bill_list":[{"amount":"0.01","fund_channel":"PCREDIT"}],"gmt_payment":"2019-08-29 20:22:02","invoice_amount":"0.01","out_trade_no":"GZ201901301040361013","point_amount":"0.00","receipt_amount":"0.01","total_amount":"0.01","trade_no":"2019082922001432790585666965"},"sign":"DSX/wmE0nnuxQrWfJZtq0fNntcx5UYtVV35P2VZpoTC2KlIWr4eGNiXcetbb7AkI/1Tyd0+cNtcGMgB7SYzTB15/wDE0vJ+eT5ucqhNkER1kcuCC0k9OkZzU5w8wCJzOgAy52Wso9KnrwkY86mJWt3dC8DNCCi1rlf1a8bTGIBG/diJaKAgP1lGT3aW8jeGGM98zLabqDUNvck2qkgctGR49kBb0ZYmIzmY0x5goVyKnaCkcC/d1VTIIMz81mJbeqU8UZk6TqEplCC8J+dYEUj04pAO4/lwIg/YZdKj3Pz1136/+uy669Pew88+74J/u/zPsehC44PxcUk9YKmkNyw=="}`

Expand Down
18 changes: 9 additions & 9 deletions alipay_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ func (a *AliPayClient) SetAppCertSN(appCertSN string) (client *AliPayClient) {
return a
}

//设置 支付宝根证书SN
// 设置 支付宝根证书SN
// alipayRootCertSN:支付宝根证书SN,通过 gopay.GetCertSN() 获取
func (a *AliPayClient) SetAliPayRootCertSN(alipayRootCertSN string) (client *AliPayClient) {
a.AlipayRootCertSN = alipayRootCertSN
return a
}

//设置支付后的ReturnUrl
// 设置支付后的ReturnUrl
func (a *AliPayClient) SetReturnUrl(url string) (client *AliPayClient) {
a.ReturnUrl = url
return a
}

//设置支付宝服务器主动通知商户服务器里指定的页面http/https路径。
// 设置支付宝服务器主动通知商户服务器里指定的页面http/https路径。
func (a *AliPayClient) SetNotifyUrl(url string) (client *AliPayClient) {
a.NotifyUrl = url
return a
}

//设置编码格式,如utf-8,gbk,gb2312等,默认推荐使用 utf-8
// 设置编码格式,如utf-8,gbk,gb2312等,默认推荐使用 utf-8
func (a *AliPayClient) SetCharset(charset string) (client *AliPayClient) {
if charset == null {
a.Charset = "utf-8"
Expand All @@ -72,7 +72,7 @@ func (a *AliPayClient) SetCharset(charset string) (client *AliPayClient) {
return a
}

//设置签名算法类型,目前支持RSA2和RSA,默认推荐使用 RSA2
// 设置签名算法类型,目前支持RSA2和RSA,默认推荐使用 RSA2
func (a *AliPayClient) SetSignType(signType string) (client *AliPayClient) {
if signType == null {
a.SignType = "RSA2"
Expand All @@ -82,19 +82,19 @@ func (a *AliPayClient) SetSignType(signType string) (client *AliPayClient) {
return a
}

//设置应用授权
// 设置应用授权
func (a *AliPayClient) SetAppAuthToken(appAuthToken string) (client *AliPayClient) {
a.AppAuthToken = appAuthToken
return a
}

//设置用户信息授权
// 设置用户信息授权
func (a *AliPayClient) SetAuthToken(authToken string) (client *AliPayClient) {
a.AuthToken = authToken
return a
}

//获取参数签名
// 获取参数签名
func getRsaSign(bm BodyMap, signType, privateKey string) (sign string, err error) {
var (
block *pem.Block
Expand Down Expand Up @@ -131,7 +131,7 @@ func getRsaSign(bm BodyMap, signType, privateKey string) (sign string, err error
return
}

//格式化请求URL参数
// 格式化请求URL参数
func FormatAliPayURLParam(body BodyMap) (urlParam string) {
v := url.Values{}
for key, value := range body {
Expand Down
Loading

0 comments on commit 3f1b42f

Please sign in to comment.