main
parent
96cf907894
commit
6e62d1a84a
@ -0,0 +1 @@
|
||||
qdSewHxAIHz4O5SU
|
@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func StructToURLValues(s interface{}) (url.Values, error) {
|
||||
values := url.Values{}
|
||||
structValue := reflect.ValueOf(s)
|
||||
structFieldValue := structValue.FieldByName
|
||||
|
||||
structType := reflect.TypeOf(s)
|
||||
for i := 0; i < structType.NumField(); i++ {
|
||||
fieldName := structType.Field(i).Name
|
||||
tagName := structType.Field(i).Tag.Get("form")
|
||||
valueField := fieldName
|
||||
if tagName != "" {
|
||||
valueField = tagName
|
||||
}
|
||||
fieldValue := structFieldValue(fieldName)
|
||||
switch fieldValue.Kind() {
|
||||
case reflect.String:
|
||||
values.Set(valueField, fieldValue.String())
|
||||
case reflect.Int, reflect.Int64:
|
||||
str := fmt.Sprintf("%v", fieldValue.Int())
|
||||
values.Set(valueField, str)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported field type: %v", fieldValue.Kind())
|
||||
}
|
||||
}
|
||||
fmt.Println(values)
|
||||
return values, nil
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package tlpay
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gold-shop/errors"
|
||||
"gold-shop/global"
|
||||
"gold-shop/utils"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var TLPay tlPay = tlPay{}
|
||||
|
||||
type tlPay struct {
|
||||
}
|
||||
|
||||
func (api *tlPay) Pay(param PayParam) (string, error) {
|
||||
payment := global.Config.Payment
|
||||
data, err := utils.StructToURLValues(param)
|
||||
sign, err := api.sign(data)
|
||||
data.Set("sign", sign)
|
||||
res, err := api.post(payment.BaseUrl+"/apiweb/gateway/pay", data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(res), nil
|
||||
}
|
||||
|
||||
func (api *tlPay) sign(params url.Values) (string, error) {
|
||||
params.Del("sign")
|
||||
fmt.Println(params.Encode())
|
||||
return utils.RSASign([]byte(params.Encode()), "")
|
||||
}
|
||||
|
||||
func (api *tlPay) post(url string, data url.Values) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
body := strings.NewReader(data.Encode())
|
||||
request, err := http.NewRequest("POST", url, body)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (api *tlPay) buildHtml(param PayParam) string {
|
||||
htmlBegin := `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>充值</title>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
document.getElementById("auto-submit-form").submit();
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<body>`
|
||||
|
||||
htmlEnd :=
|
||||
`</body>
|
||||
</html>
|
||||
`
|
||||
return htmlBegin + api.buildFormHtml(param) + htmlEnd
|
||||
}
|
||||
|
||||
func (api *tlPay) buildFormHtml(param PayParam) string {
|
||||
data, _ := utils.StructToURLValues(param)
|
||||
formHtml := "<form id=\"auto-submit-form\" action=\"{{.Action}}\" method=\"{{.Method}}\">"
|
||||
for key, value := range data {
|
||||
formHtml += "<input type=\"hidden\" name=\"" + key + "\" value=\"" + value[0] + "\">"
|
||||
}
|
||||
formHtml += "</form>"
|
||||
return formHtml
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package tlpay
|
||||
|
||||
type PayParam struct {
|
||||
OrgID string `json:"orgid" form:"orgid"`
|
||||
CusID string `json:"cusid" form:"cusid"`
|
||||
AppID string `json:"appid" form:"appid"`
|
||||
RetUrl string `json:"returl" form:"returl"`
|
||||
NotifyUrl string `json:"notifyurl" form:"notifyurl"`
|
||||
GoodsID string `json:"goodsid" form:"goodsid"`
|
||||
GoodsInf string `json:"goodsinf" form:"goodsinf"`
|
||||
TrxAmt string `json:"trxamt" form:"trxamt"`
|
||||
OrderID string `json:"orderid" form:"orderid"`
|
||||
RandomStr string `json:"randomstr" form:"randomstr"`
|
||||
GateID string `json:"gateid" form:"gateid"`
|
||||
PayType string `json:"paytype" form:"paytype"`
|
||||
LimitPay string `json:"limitpay" form:"limitpay"`
|
||||
ValidTime string `json:"validtime" form:"validtime"`
|
||||
SignType string `json:"signtype" form:"signtype"`
|
||||
Sign string `json:"sign" form:"sign"`
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>充值</title>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
document.getElementById("auto-submit-form").submit();
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<form id="auto-submit-form" action="{{.Action}}" method="{{.Method}}">
|
||||
<!-- Your form fields go here -->
|
||||
<input type="hidden" name="paytype" value="{{.data.PayType}}">
|
||||
<input type="hidden" name="field2" value="value2">
|
||||
<input type="hidden" name="field2" value="value2">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue