我正在實作 Authorize.net 信用卡 API。無論交易是成功還是被拒絕,API 總是給我一個 200 回應代碼。但它為成功交易提供了一個回應主體,而為拒絕交易提供了不同的回應主體。
type AuthorizeApprovedResponse struct {
TransactionResponse struct {
ResponseCode string `json:"responseCode"`
AuthCode string `json:"authCode"`
AvsResultCode string `json:"avsResultCode"`
CvvResultCode string `json:"cvvResultCode"`
CavvResultCode string `json:"cavvResultCode"`
TransID string `json:"transId"`
RefTransID string `json:"refTransID"`
TransHash string `json:"transHash"`
TestRequest string `json:"testRequest"`
AccountNumber string `json:"accountNumber"`
AccountType string `json:"accountType"`
Messages []struct {
Code string `json:"code"`
Description string `json:"description"`
} `json:"messages"`
UserFields []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"userFields"`
TransHashSha2 string `json:"transHashSha2"`
SupplementalDataQualificationIndicator int `json:"SupplementalDataQualificationIndicator"`
NetworkTransID string `json:"networkTransId"`
} `json:"transactionResponse"`
RefID string `json:"refId"`
Messages struct {
ResultCode string `json:"resultCode"`
Message []struct {
Code string `json:"code"`
Text string `json:"text"`
} `json:"message"`
} `json:"messages"`
}
type AuthorizeDeclinedResponse struct {
TransactionResponse struct {
ResponseCode string `json:"responseCode"`
AuthCode string `json:"authCode"`
AvsResultCode string `json:"avsResultCode"`
CvvResultCode string `json:"cvvResultCode"`
CavvResultCode string `json:"cavvResultCode"`
TransID string `json:"transId"`
RefTransID string `json:"refTransID"`
TransHash string `json:"transHash"`
TestRequest string `json:"testRequest"`
AccountNumber string `json:"accountNumber"`
AccountType string `json:"accountType"`
Errors []struct {
ErrorCode string `json:"errorCode"`
ErrorText string `json:"errorText"`
} `json:"errors"`
UserFields []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"userFields"`
TransHashSha2 string `json:"transHashSha2"`
SupplementalDataQualificationIndicator int `json:"SupplementalDataQualificationIndicator"`
NetworkTransID string `json:"networkTransId"`
} `json:"transactionResponse"`
RefID string `json:"refId"`
Messages struct {
ResultCode string `json:"resultCode"`
Message []struct {
Code string `json:"code"`
Text string `json:"text"`
} `json:"message"`
} `json:"messages"`
}
這是我的問題,使用哪個結構。我正在考慮嘗試一個 interface{} 然后嘗試將其轉換為結構?
err := json.Unmarshal(b, &whichStructToUse)
if err != nil {
panic(err.Error())
}
當我不知道要使用哪個結構時,有關如何解組回應的任何建議?
uj5u.com熱心網友回復:
無論交易是成功還是被拒絕,API 總是給我一個 200 回應代碼。
我感覺到你的痛苦。
兩種回應之間只有一個區別,成功有Messages和失敗有Errors。將它們結合起來。
type CommonResponse struct {
TransactionResponse struct {
ResponseCode string `json:"responseCode"`
AuthCode string `json:"authCode"`
AvsResultCode string `json:"avsResultCode"`
CvvResultCode string `json:"cvvResultCode"`
CavvResultCode string `json:"cavvResultCode"`
TransID string `json:"transId"`
RefTransID string `json:"refTransID"`
TransHash string `json:"transHash"`
TestRequest string `json:"testRequest"`
AccountNumber string `json:"accountNumber"`
AccountType string `json:"accountType"`
Messages []struct {
Code string `json:"code"`
Description string `json:"description"`
} `json:"messages"`
Errors []struct {
ErrorCode string `json:"errorCode"`
ErrorText string `json:"errorText"`
} `json:"errors"`
UserFields []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"userFields"`
TransHashSha2 string `json:"transHashSha2"`
SupplementalDataQualificationIndicator int `json:"SupplementalDataQualificationIndicator"`
NetworkTransID string `json:"networkTransId"`
} `json:"transactionResponse"`
RefID string `json:"refId"`
Messages struct {
ResultCode string `json:"resultCode"`
Message []struct {
Code string `json:"code"`
Text string `json:"text"`
} `json:"message"`
} `json:"messages"`
}
然后使用它來解組并檢查錯誤。
var response CommonResponse;
json.Unmarshal([]byte(jsonString), &response)
if len(response.Error) == 0 {
fmt.Println("Success!")
} else {
fmt.Println("Error!")
}
對于更一般的情況,您可以解組為map[string]interface{}.
var result map[string]interface{}
json.Unmarshal([]byte(jsonString), &result)
示范。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521014.html
標籤:json去解组授权网
上一篇:如果父行程終止,則終止exec
