-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
388 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package rest | ||
|
||
import ( | ||
"net/http" | ||
"go-wxpay-gateway/conf" | ||
"go-wxpay-gateway/wx-pay-api" | ||
) | ||
|
||
// POST /query-transfer | ||
/* | ||
{ | ||
"appId": "appId of mp/mini-prog", | ||
"payApp": "name-of-app-in-wxpay-gateway", | ||
"tradeNo": "unique-trade-no", | ||
} | ||
*/ | ||
func QueryTransfer(w http.ResponseWriter, r *http.Request) { | ||
var queryParam struct { | ||
AppId string | ||
PayApp string | ||
TradeNo string | ||
} | ||
if code, err := _ReadJson(r, &queryParam); err != nil { | ||
_WriteError(w, code, err.Error()) | ||
return | ||
} | ||
|
||
isSandbox := _IsSandbox(queryParam.PayApp) | ||
mchConf, _, ok := conf.GetAppAttrs(queryParam.PayApp) | ||
if !ok { | ||
_WriteError(w, http.StatusBadRequest, "Unknown pay-app name") | ||
return | ||
} | ||
res, err := wxpay.QueryTransfer( | ||
queryParam.AppId, | ||
mchConf.MchId, | ||
mchConf.MchAppKey, | ||
queryParam.TradeNo, | ||
mchConf.MchCertPemFile, | ||
mchConf.MchKeyPemFile, | ||
isSandbox, | ||
) | ||
if err != nil { | ||
_WriteError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
_WriteJson(w, http.StatusOK, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package rest | ||
|
||
import ( | ||
"net/http" | ||
"go-wxpay-gateway/conf" | ||
"go-wxpay-gateway/wx-pay-api" | ||
) | ||
|
||
// POST /transfer | ||
/* | ||
{ | ||
"appId": "appId of mp/mini-prog", | ||
"payApp": "name-of-app-in-wxpay-gateway", | ||
"tradeNo": "unique-trade-no", | ||
"openId": "openid to be transfered", | ||
"userName": "real user name", | ||
"amount": xxx-in-fen, | ||
"desc": "description", | ||
"ip": "ip to create order" | ||
} | ||
*/ | ||
func Transfer(w http.ResponseWriter, r *http.Request) { | ||
var transferParam struct { | ||
AppId string | ||
PayApp string | ||
TradeNo string | ||
OpenId string | ||
UserName string | ||
Amount int | ||
Desc string | ||
Ip string | ||
} | ||
|
||
if code, err := _ReadJson(r, &transferParam); err != nil { | ||
_WriteError(w, code, err.Error()) | ||
return | ||
} | ||
|
||
isSandbox := _IsSandbox(transferParam.PayApp) | ||
mchConf, _, ok := conf.GetAppAttrs(transferParam.PayApp) | ||
if !ok { | ||
_WriteError(w, http.StatusBadRequest, "Unknown pay-app name") | ||
return | ||
} | ||
|
||
res, err := wxpay.Transfer( | ||
transferParam.AppId, | ||
mchConf.MchId, | ||
mchConf.MchAppKey, | ||
transferParam.TradeNo, | ||
transferParam.OpenId, | ||
transferParam.UserName, | ||
transferParam.Amount, | ||
transferParam.Desc, | ||
transferParam.Ip, | ||
mchConf.MchCertPemFile, | ||
mchConf.MchKeyPemFile, | ||
isSandbox, | ||
) | ||
if err != nil { | ||
_WriteError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
_WriteJson(w, http.StatusOK, &res) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// +build gateway | ||
|
||
package wxpay | ||
|
||
func postQueryTransfer(tradeNo, appKey, certFile, keyFile string, xml []byte, isSandbox bool) (*QueryTransferResult, error) { | ||
_paymentLog.Printf("[query-transfer] 1. ### Before Querying tradeNo #%s: %s\n", tradeNo, string(xml)) | ||
transfer_query_url := _GetApiUrl(UT_TRANSFER_QUERY, isSandbox) | ||
content, err := _CallSecureWxAPI(transfer_query_url, "POST", xml, certFile, keyFile) | ||
if err != nil { | ||
_paymentLog.Printf("[query-transfer] 2. --- Query tradeNo #%s failed: %v\n", tradeNo, err) | ||
return nil, err | ||
} | ||
_paymentLog.Printf("[query-transfer] 2. +++ Result of querying tradeNo #%s: %s\n", tradeNo, string(content)) | ||
|
||
return ParseQueryTransferResult("query-transfer-result", content, appKey) | ||
} | ||
|
||
func QueryTransfer( | ||
appId string, | ||
mchId string, | ||
mchAppKey string, | ||
tradeNo string, | ||
certFile string, | ||
keyFile string, | ||
isSandbox bool, | ||
) (*QueryTransferResult, error) { | ||
/* | ||
if isSandbox { | ||
var err error | ||
if mchAppKey, err = GetSandbox(appId, mchId, mchAppKey); err != nil { | ||
return nil, err | ||
} | ||
}*/ | ||
xml := newXmlGenerator("xml") | ||
params := make(map[string]string) | ||
|
||
addTag(xml, params, "appid", appId, false) | ||
addTag(xml, params, "mch_id", mchId, false) | ||
addTag(xml, params, "partner_trade_no", tradeNo, false) | ||
addTag(xml, params, "nonce_str", string(_GetRandomBytes(32)), false) | ||
addTag(xml, params, "sign_type", "MD5", false) | ||
|
||
// sign | ||
signature := createMd5Signature(params, mchAppKey) | ||
addTag(xml, params, "sign", signature, false) | ||
|
||
xmlstr := xml.toXML() | ||
|
||
return postQueryTransfer(tradeNo, mchAppKey, certFile, keyFile, xmlstr, isSandbox) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// +build gateway | ||
|
||
package wxpay | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Transfer( | ||
appId string, | ||
mchId string, | ||
mchAppKey string, | ||
tradeNo string, | ||
openId string, | ||
userName string, | ||
amount int, | ||
desc string, | ||
ip string, | ||
certFile string, | ||
keyFile string, | ||
isSandbox bool, | ||
) (*TransferResult, error) { | ||
tags := make(map[string]string) | ||
xml := newXmlGenerator("xml") | ||
addTag(xml, tags, "mch_appid", appId, false) | ||
addTag(xml, tags, "mchid", mchId, false) | ||
addTag(xml, tags, "nonce_str", string(_GetRandomBytes(32)), false) | ||
addTag(xml, tags, "partner_trade_no", tradeNo, false) | ||
addTag(xml, tags, "openid", openId, false) | ||
if userName == "" { | ||
addTag(xml, tags, "check_name", "NO_CHECK", false) | ||
} else { | ||
addTag(xml, tags, "check_name", "FORCE_CHECK", false) | ||
addTag(xml, tags, "re_user_name", userName, false) | ||
} | ||
addTag(xml, tags, "amount", fmt.Sprintf("%d", amount), false) | ||
addTag(xml, tags, "desc", desc, false) | ||
addTag(xml, tags, "spbill_create_ip", ip, false) | ||
// sign | ||
signature := createMd5Signature(tags, mchAppKey) | ||
addTag(xml, tags, "sign", signature, false) | ||
|
||
xmlstr := xml.toXML() | ||
// fmt.Printf("xml: %s\n", string(xmlstr)) | ||
|
||
return postTransfer(tradeNo, mchAppKey, certFile, keyFile, xmlstr, isSandbox) | ||
} | ||
|
||
func postTransfer(tradeNo, appKey, certFile, keyFile string, xml []byte, isSandbox bool) (*TransferResult, error) { | ||
_paymentLog.Printf("[transfer] 1. ### Before POSTing transfer #%s: %s\n", tradeNo, string(xml)) | ||
transfer_url := _GetApiUrl(UT_TRANSFER, isSandbox) | ||
content, err := _CallSecureWxAPI(transfer_url, "POST", xml, certFile, keyFile) | ||
if err != nil { | ||
_paymentLog.Printf("[transfer] 2. --- POST transfer #%s failed: %v\n", tradeNo, err) | ||
return nil, err | ||
} | ||
_paymentLog.Printf("[transfer] 2. +++ Result of POSTing transfer #%s: %s\n", tradeNo, string(content)) | ||
|
||
return ParseTransferResultBody("transfer result", content, appKey) | ||
} |
Oops, something went wrong.