Skip to content

Commit

Permalink
rename appKey to apiKey
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbit committed May 9, 2020
1 parent 04e0b8e commit e97c02a
Show file tree
Hide file tree
Showing 37 changed files with 127 additions and 125 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
3. 编译成功,会得到3个可执行程序
- `go-wxpay-gateway`: 微信支付网关程序,可以执行`./go-wxpay-gateway -v`显示程序信息。
- `go-wxpay-notify`: 支付结果通知服务程序,可以执行`./go-wxpay-notify -v`显示程序信息。
- `go-wxpay-getsandbox`: 获取沙箱测试的appKey工具,可以执行`./go-wxpay-getsandbox -v`显示程序信息。
- `go-wxpay-getsandbox`: 获取沙箱测试的apiKey工具,可以执行`./go-wxpay-getsandbox -v`显示程序信息。
4. Linux的二进制版本可以直接进入[releases](https://github.com/rosbit/go-wxpay-gateway/releases)下载

## 运行方法
Expand Down Expand Up @@ -69,11 +69,11 @@
- `$ CONF_FILE=./wxpay-notify-conf.json ./go-wxpay-notify`

### go-wxpay-getsandbox运行方法
- 运行`./go-wxpay-getsandbox <appId> <mchId> <mchAppKey>`
- 运行`./go-wxpay-getsandbox <appId> <mchId> <mchApiKey>`
- 其中&lt;appId&gt;是指公众号、小程序、网页应用等id,请根据应用类型选用正确的appId
- &lt;mchId&gt;是指商户号,申请商户号的时可以拿到
- &lt;mchAppKey&gt;是商户号的appKey,可以进入商户号管理界面设定
- 运行成功后把沙箱mchAppKey输出到屏幕
- &lt;mchApiKey&gt;是商户号的apiKey,可以进入商户号管理界面设定
- 运行成功后把沙箱mchApiKey输出到屏幕

## go-wxpay-gateway服务的接口参数
1. 创建订单
Expand Down
10 changes: 5 additions & 5 deletions conf/conf-gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{
"name": "mch1",
"mch-id": "",
"mch-app-key": "",
"mch-api-key": "",
"mch-cert-pem-file": "your-cert-pem-file-name, only used when refunding",
"mch-key-pem-file": "your-key-pem-file-name, only used when refunding"
}
Expand Down Expand Up @@ -59,7 +59,6 @@ import (
"fmt"
"os"
"time"
"io/ioutil"
"encoding/json"
)

Expand All @@ -84,7 +83,7 @@ type EndpointConf struct {
type MerchantConf struct {
Name string
MchId string `json:"mch-id"`
MchAppKey string `json:"mch-app-key"`
MchApiKey string `json:"mch-api-key"`
MchCertPemFile string `json:"mch-cert-pem-file"`
MchKeyPemFile string `json:"mch-key-pem-file"`
}
Expand Down Expand Up @@ -142,11 +141,12 @@ func CheckGlobalConf() error {
return err
}

b, err := ioutil.ReadFile(confFile)
fp, err := os.Open(confFile)
if err != nil {
return err
}
if err := json.Unmarshal(b, &ServiceConf); err != nil {
defer fp.Close()
if err = json.NewDecoder(fp).Decode(&ServiceConf); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions conf/conf-getsandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
var (
AppId string
MchId string
MchAppKey string
MchApiKey string
)

func CheckGlobalConf() error {
if len(os.Args) < 4 {
return fmt.Errorf("Usage: %s <appId> <mchId> <mchAppKey>", os.Args[0])
return fmt.Errorf("Usage: %s <appId> <mchId> <mchApiKey>", os.Args[0])
}

AppId, MchId, MchAppKey = os.Args[1], os.Args[2], os.Args[3]
AppId, MchId, MchApiKey = os.Args[1], os.Args[2], os.Args[3]
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions conf/conf-notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"os"
"time"
"io/ioutil"
"encoding/json"
)

Expand Down Expand Up @@ -71,11 +70,12 @@ func CheckGlobalConf() error {
return err
}

b, err := ioutil.ReadFile(confFile)
fp, err := os.Open(confFile)
if err != nil {
return err
}
if err := json.Unmarshal(b, &NotifyConf); err != nil {
defer fp.Close()
if err = json.NewDecoder(fp).Decode(&NotifyConf); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion getsandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func StartService() error {
sandbox_signkey, err := wxpay.GetSandbox(conf.AppId, conf.MchId, conf.MchAppKey)
sandbox_signkey, err := wxpay.GetSandbox(conf.AppId, conf.MchId, conf.MchApiKey)
if err != nil {
fmt.Printf("failed: %v\n", err)
} else {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require (
github.com/rosbit/go-wget v1.0.3
github.com/rosbit/logmerger v0.0.1
github.com/urfave/negroni v1.0.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ github.com/rosbit/logmerger v0.0.1 h1:mNebRSwis/pewR0fUHwDGx6zpxXbroVioljUjQia+p
github.com/rosbit/logmerger v0.0.1/go.mod h1:wzp/9K8Zu6+onB3moySMgBgXHNlXDj81vHWixaUHes0=
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
8 changes: 1 addition & 7 deletions rest/http-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/gernest/alien"
"net/http"
"encoding/json"
"io/ioutil"
"fmt"
)

Expand Down Expand Up @@ -37,12 +36,7 @@ func _ReadJson(r *http.Request, res interface{}) (int, error) {
return http.StatusBadRequest, fmt.Errorf("bad request")
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return http.StatusInternalServerError, err
}
// fmt.Printf("body: %s\n", string(b))
if err = json.Unmarshal(b, res); err != nil {
if err := json.NewDecoder(r.Body).Decode(res); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
Expand Down
2 changes: 1 addition & 1 deletion rest/rest-close-order.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func CloseOrder(w http.ResponseWriter, r *http.Request) {
if err := wxpay.CloseOrder(
closeParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
closeParam.OrderId,
isSandbox,
); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rest/rest-notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func notifyFromWx(w http.ResponseWriter, r *http.Request, fnParse wxpay.FnParseN
}

// parse xml params
params := fnParse(prompt, body, mchConf.MchAppKey)
params := fnParse(prompt, body, mchConf.MchApiKey)
params.AppName, params.CbUrl = appName, cbUrls[cbUrlIdx]
utils.SaveResult(params)
}
Expand Down
8 changes: 4 additions & 4 deletions rest/rest-pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func createWxPay(w http.ResponseWriter, r *http.Request) {
prepayId, reqJSAPI, err := wxpay.JSAPIPay(
jsapiParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
jsapiParam.Goods,
jsapiParam.Udd,
jsapiParam.OrderId,
Expand Down Expand Up @@ -139,7 +139,7 @@ func createNativePay(w http.ResponseWriter, r *http.Request) {
prepayId, codeUrl, err := wxpay.NativePay(
nativeParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
nativeParam.Goods,
nativeParam.Udd,
nativeParam.OrderId,
Expand Down Expand Up @@ -202,7 +202,7 @@ func createAppPay(w http.ResponseWriter, r *http.Request) {
prepayId, reqAppPay, err := wxpay.AppPay(
appParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
appParam.Goods,
appParam.Udd,
appParam.OrderId,
Expand Down Expand Up @@ -295,7 +295,7 @@ func createH5Pay(w http.ResponseWriter, r *http.Request) {
prepayId, payUrl, err := wxpay.H5Pay(
h5Param.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
h5Param.Goods,
h5Param.Udd,
h5Param.OrderId,
Expand Down
2 changes: 1 addition & 1 deletion rest/rest-query-order.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func QueryOrder(w http.ResponseWriter, r *http.Request) {
res, err := queryFunc(
queryParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
id,
isSandbox,
)
Expand Down
2 changes: 1 addition & 1 deletion rest/rest-query-transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func QueryTransfer(w http.ResponseWriter, r *http.Request) {
res, err := wxpay.QueryTransfer(
queryParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
queryParam.TradeNo,
mchConf.MchCertPemFile,
mchConf.MchKeyPemFile,
Expand Down
2 changes: 1 addition & 1 deletion rest/rest-refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func CreateRefundment(w http.ResponseWriter, r *http.Request) {
refundNotifyParams, err := refundFn(
refundParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
id,
refundParam.RefundId,
refundParam.TotalFee,
Expand Down
2 changes: 1 addition & 1 deletion rest/rest-transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Transfer(w http.ResponseWriter, r *http.Request) {
res, err := wxpay.Transfer(
transferParam.AppId,
mchConf.MchId,
mchConf.MchAppKey,
mchConf.MchApiKey,
transferParam.TradeNo,
transferParam.OpenId,
transferParam.UserName,
Expand Down
10 changes: 7 additions & 3 deletions utils/notify-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package utils

import (
"gopkg.in/natefinch/lumberjack.v2"
"github.com/rosbit/go-wget"
"os"
"io"
Expand Down Expand Up @@ -43,9 +44,12 @@ var (
)

func InitNotifyLog(logFile string) error {
_notifyLogFile, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
_notifyLogFile := &lumberjack.Logger{
Filename: logFile,
MaxSize: 100, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: false, // disabled by default
}
_notifyLog = log.New(_notifyLogFile, "", log.LstdFlags)
return nil
Expand Down
12 changes: 6 additions & 6 deletions wx-pay-api/close_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package wxpay

func postClose(orderId string, xml []byte, isSandbox bool, appKey string) error {
func postClose(orderId string, xml []byte, isSandbox bool, apiKey string) error {
_paymentLog.Printf("[close-order] 1. ### Before Closing order %s: %s\n", orderId, string(xml))
orderclose_url := _GetApiUrl(UT_ORDER_CLOSE, isSandbox)
content, err := _CallWxAPI(orderclose_url, "POST", xml)
Expand All @@ -12,21 +12,21 @@ func postClose(orderId string, xml []byte, isSandbox bool, appKey string) error
}
_paymentLog.Printf("[close-order] 2. +++ Result of closing order %s: %s\n", orderId, string(content))

_, err = parseXmlResult(content, appKey)
_, err = parseXmlResult(content, apiKey)
return err
}

func CloseOrder(
appId string,
mchId string,
mchAppKey string,
mchApiKey string,
orderId string,
isSandbox bool,
) error {
/*
if isSandbox {
var err error
if mchAppKey, err = GetSandbox(appId, mchId, mchAppKey); err != nil {
if mchApiKey, err = GetSandbox(appId, mchId, mchApiKey); err != nil {
return nil, err
}
}*/
Expand All @@ -40,10 +40,10 @@ func CloseOrder(
addTag(xml, params, "sign_type", "MD5", false)

// sign
signature := createMd5Signature(params, mchAppKey)
signature := createMd5Signature(params, mchApiKey)
addTag(xml, params, "sign", signature, false)

xmlstr := xml.toXML()

return postClose(orderId, xmlstr, isSandbox, mchAppKey)
return postClose(orderId, xmlstr, isSandbox, mchApiKey)
}
8 changes: 4 additions & 4 deletions wx-pay-api/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func addTag(xml *xmlGenerator, tags map[string]string, tagName string, value str
}
}

func createMd5Signature(params map[string]string, appKey string) string {
func createMd5Signature(params map[string]string, apiKey string) string {
// sort keys
keys := make([]string, len(params))
i := 0
Expand Down Expand Up @@ -71,7 +71,7 @@ func createMd5Signature(params map[string]string, appKey string) string {
}

io.WriteString(stringA, "&key=")
io.WriteString(stringA, appKey)
io.WriteString(stringA, apiKey)

// MD5
sign := stringA.Sum(nil)
Expand Down Expand Up @@ -119,7 +119,7 @@ func xml2map(body []byte) (map[string]string, error) {
return xml2mapWithRoot(body, "xml")
}

func parseXmlResult(body []byte, appKey string) (map[string]string, error) {
func parseXmlResult(body []byte, apiKey string) (map[string]string, error) {
res, err := xml2map(body)
if err != nil {
return nil, err
Expand All @@ -130,7 +130,7 @@ func parseXmlResult(body []byte, appKey string) (map[string]string, error) {
return nil, fmt.Errorf("no signature in result")
} else {
delete(res, "sign")
createdSign := createMd5Signature(res, appKey)
createdSign := createMd5Signature(res, apiKey)
if sign != createdSign {
return nil, fmt.Errorf("signature not matched: %s != %s", sign, createdSign)
}
Expand Down
2 changes: 1 addition & 1 deletion wx-pay-api/notify_params_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ func _NewNotifyError(err error) *NotifyParams {
return &NotifyParams{INotifyParams:e}
}

type FnParseNotifyBody func(prompt string, body []byte, appKey string) *NotifyParams
type FnParseNotifyBody func(prompt string, body []byte, apiKey string) *NotifyParams

10 changes: 5 additions & 5 deletions wx-pay-api/pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
SANDBOX_FEE = 101
)

func postOrder(orderId string, appKey string, xml []byte, isSandbox bool) (map[string]string, error) {
func postOrder(orderId string, apiKey string, xml []byte, isSandbox bool) (map[string]string, error) {
_paymentLog.Printf("[payment] 1. ### Before POSTing order #%s: %s\n", orderId, string(xml))
unifiedorder_url := _GetApiUrl(UT_UNIFIED_ORDER, isSandbox)
content, err := _CallWxAPI(unifiedorder_url, "POST", xml)
Expand All @@ -20,13 +20,13 @@ func postOrder(orderId string, appKey string, xml []byte, isSandbox bool) (map[s
}
_paymentLog.Printf("[payment] 2. +++ Result of POSTing order #%s: %s\n", orderId, string(content))

return parseXmlResult(content, appKey)
return parseXmlResult(content, apiKey)
}

func payOrder(
appId string,
mchId string,
mchAppKey string,
mchApiKey string,
deviceInfo string,
payBody string,
cbParams string,
Expand Down Expand Up @@ -59,13 +59,13 @@ func payOrder(
addTag(xml, tags, "scene_info", string(sceneInfo), false)
}
// sign
signature := createMd5Signature(tags, mchAppKey)
signature := createMd5Signature(tags, mchApiKey)
addTag(xml, tags, "sign", signature, false)

xmlstr := xml.toXML()
// fmt.Printf("xml: %s\n", string(xmlstr))

if res, err = postOrder(orderId, mchAppKey, xmlstr, isSandbox); err != nil {
if res, err = postOrder(orderId, mchApiKey, xmlstr, isSandbox); err != nil {
return "", nil, err
}

Expand Down
Loading

0 comments on commit e97c02a

Please sign in to comment.