|
|
|
package payment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"gold-shop/errors"
|
|
|
|
"gold-shop/global"
|
|
|
|
"gold-shop/utils"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var PayApi payApi = payApi{}
|
|
|
|
|
|
|
|
type payApi struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *payApi) WxPay(param WxPayParam) (*WxPayResult, error) {
|
|
|
|
payment := global.Config.Payment
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("service", "comm.js.pay")
|
|
|
|
data.Set("apikey", payment.ApiKey)
|
|
|
|
data.Set("money", fmt.Sprintf("%.2f", param.Amount))
|
|
|
|
data.Set("sub_appid", payment.WxPay.SubAppId)
|
|
|
|
data.Set("sub_openid", param.OpenId)
|
|
|
|
data.Set("nonce_str", utils.GenerateRandomString(32))
|
|
|
|
data.Set("mch_orderid", param.OrderId)
|
|
|
|
data.Set("notify_url", param.NotifyUrl)
|
|
|
|
data.Set("remarks", param.Remark)
|
|
|
|
sign := api.sign(data, payment.SignKey)
|
|
|
|
data.Set("sign", sign)
|
|
|
|
fmt.Println(data.Encode())
|
|
|
|
res, err := api.post(payment.BaseUrl+"/payapi/mini/wxpay", data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := WxPayResult{}
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.NewBusinessError("结果解析错误")
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *payApi) QueryOrder(param QueryOrderParam) (*QueryOrderResult, error) {
|
|
|
|
payment := global.Config.Payment
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("service", "pay.comm.query_order")
|
|
|
|
data.Set("apikey", payment.ApiKey)
|
|
|
|
data.Set("orderid", param.OrderID)
|
|
|
|
data.Set("mch_orderid", param.MchOrderID)
|
|
|
|
data.Set("nonce_str", utils.GenerateRandomString(32))
|
|
|
|
data.Set("order_time", strconv.Itoa(int(param.OrderTime)))
|
|
|
|
data.Set("dis_name", param.DisName)
|
|
|
|
sign := api.sign(data, payment.SignKey)
|
|
|
|
data.Set("sign", sign)
|
|
|
|
fmt.Println(data.Encode())
|
|
|
|
res, err := api.post(payment.BaseUrl+"/payapi/pay/query_order", data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := QueryOrderResult{}
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.NewBusinessError("结果解析错误")
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *payApi) TransferPay(param TransferPayParam) (*TransferPayResult, error) {
|
|
|
|
payment := global.Config.Payment
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("service", "pay.heli.large_query")
|
|
|
|
data.Set("apikey", payment.ApiKey)
|
|
|
|
data.Set("order_money", fmt.Sprintf("%.2f", param.Amount))
|
|
|
|
data.Set("nonce_str", utils.GenerateRandomString(32))
|
|
|
|
data.Set("out_orderid", param.OrderId)
|
|
|
|
//data.Set("notify_url", param.NotifyUrl)
|
|
|
|
//data.Set("remark", param.Remark)
|
|
|
|
sign := api.sign(data, payment.SignKey)
|
|
|
|
data.Set("sign", sign)
|
|
|
|
fmt.Println(data.Encode())
|
|
|
|
fmt.Println("url: " + payment.BaseUrl + "/payapi/pay/large_create")
|
|
|
|
res, err := api.post(payment.BaseUrl+"/payapi/pay/large_create", data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := TransferPayResult{}
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.NewBusinessError("结果解析错误")
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payApi) sign(params url.Values, signKey string) string {
|
|
|
|
fmt.Println(params.Encode() + "&signkey=" + signKey)
|
|
|
|
return utils.Md5(params.Encode() + "&signkey=" + signKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payApi) post(url string, data url.Values) ([]byte, error) {
|
|
|
|
client := &http.Client{}
|
|
|
|
body := strings.NewReader(data.Encode())
|
|
|
|
request, err := http.NewRequest("POST", url, body)
|
|
|
|
request.Header.Set("content-type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.NewBusinessError("请求错误")
|
|
|
|
}
|
|
|
|
response, err := client.Do(request)
|
|
|
|
|
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err := Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
}(response.Body)
|
|
|
|
|
|
|
|
bodyBytes, err := io.ReadAll(response.Body)
|
|
|
|
|
|
|
|
fmt.Println(string(bodyBytes))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.NewBusinessError("返回内容错误")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bodyBytes, nil
|
|
|
|
}
|