You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.5 KiB
Go

8 months ago
package payment
import (
"encoding/json"
"fmt"
"gold-shop/errors"
"gold-shop/global"
"gold-shop/utils"
"io"
"net/http"
"net/url"
6 months ago
"strconv"
8 months ago
"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")
8 months ago
data.Set("apikey", payment.ApiKey)
8 months ago
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())
6 months ago
data.Set("notify_url", url.QueryEscape(param.NotifyUrl))
8 months ago
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
}
5 months ago
func (api *payApi) AliPay(param AliPayParam) (*AliPayResult, 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("buyer_id", param.OrderId)
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())
data.Set("notify_url", url.QueryEscape(param.NotifyUrl))
res, err := api.post(payment.BaseUrl+"/payapi/trade/alipay", data)
if err != nil {
return nil, err
}
result := AliPayResult{}
err = json.Unmarshal(res, &result)
if err != nil {
return nil, errors.NewBusinessError("结果解析错误")
}
return &result, nil
}
6 months ago
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
}
7 months ago
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
}
8 months ago
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
}