36 lines
767 B
Go
36 lines
767 B
Go
package pay
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type APIError struct {
|
|
StatusCode int
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
RawBody string
|
|
}
|
|
|
|
func (e *APIError) Error() string {
|
|
if e.Code != "" {
|
|
return e.Message
|
|
// return fmt.Sprintf("wechat pay api error: status=%d code=%s message=%s", e.StatusCode, e.Code, e.Message)
|
|
}
|
|
return e.RawBody
|
|
// return fmt.Sprintf("wechat pay api error: status=%d body=%s", e.StatusCode, e.RawBody)
|
|
}
|
|
|
|
func newAPIError(statusCode int, body []byte) error {
|
|
err := &APIError{
|
|
StatusCode: statusCode,
|
|
RawBody: string(body),
|
|
}
|
|
_ = json.Unmarshal(body, err)
|
|
return err
|
|
}
|
|
|
|
func isSuccessStatus(code int) bool {
|
|
return code >= http.StatusOK && code < http.StatusMultipleChoices
|
|
}
|