go语言google pay支付验证订单
訪問google pay的接口,國內需要翻墻
1獲取code
需要在瀏覽器登錄谷歌賬號,填寫redirect_uri和client_id(在谷歌后臺里面獲取),然后在瀏覽器中訪問https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=…&client_id=…
然后跳轉到redirect_uri地址,redirect_uri亂寫可能會出現404,不用在意,直接在上方地址欄復制code,code類似于一長串4/uQH…g6Z3M
2獲取refresh_token
發送POST請求到https://accounts.google.com/o/oauth2/token
grant_type=authorization_code
code //上面獲取的code
client_id //客服端id,在谷歌后臺獲取
client_secret //客服端秘鑰,在谷歌后臺獲取,沒有秘鑰可不用client_secret參數
redirect_uri //谷歌后臺配置,填寫的重定向地址
注意:refresh_token是一個長久有效的,每個code只會生成一次refresh_token,需要及時保存
3.獲取token
grant_type : refresh_token
client_id //在谷歌后臺獲取
client_secret //沒有秘鑰可不用這個參數
refresh_toke //上方2獲取refresh_token
4驗證訂單
獲取token,查詢訂單,代碼如下:
package mainimport ("encoding/json""fmt""io/ioutil""net/http""net/url" ) var(PackageName=""//包名GrantType="refresh_token"ClientId=""//客戶端idClientSecret=""//客戶端秘鑰RefreshToken=""//RefreshToken,上面第二步獲取的 )type TokenInfo struct {AccessToken string `json:"access_token"`ExpiresIn int `json:"expires_in"`Scope string `json:"scope"`TokenType string `json:"token_type"` }type OrderInfo struct {Kind string `json:"kind"`PurchaseTimeMillis string `json:"purchaseTimeMillis"` // 支付時間, 毫秒PurchaseState int `json:"purchaseState"`// 是否付費: 0 已支付, 1 取消ConsumptionState int `json:"consumptionState"`// 是否被消費: 0 未消費, 1 已消費DeveloperPayload string `json:"developerPayload"`// 開發者透傳參數OrderId string `json:"orderId"`// 谷歌訂單號AcknowledgementState int `json:"acknowledgementState"`// 支付類型: 0 測試, 1 真實 }func main() {info:=PostRefreshToken()//獲取tokenproductId:=""//商品 ID,即內購 ID,客戶端傳遞過來當前購買的產品token:=""//充值的時候,前端支付獲取的token值GetOrder(productId,token,info.AccessToken)//查詢訂單 }//獲取token憑證 func PostRefreshToken() *TokenInfo{resp, err := http.PostForm("https://accounts.google.com/o/oauth2/token", url.Values{"grant_type": {GrantType},"client_id": {ClientId}, "client_secret":{ClientSecret},"refresh_token": {RefreshToken}})defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println("PostToken body err:",err)}fmt.Println("token:",string(body))info:=new(TokenInfo)error := json.Unmarshal(body, &info)if err != nil {fmt.Println("PostToken Unmarshal err:",error)}fmt.Println("token info:",info)return info }//獲取訂單信息 func GetOrder(productId,token,accessToken string) *OrderInfo{client := &http.Client{}resp, err := client.Get("https://www.googleapis.com/androidpublisher/v3/applications/"+PackageName+"/purchases/products/"+productId+"/tokens/"+token+"?access_token="+accessToken+"")defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println("GetOrder body err:",err)}fmt.Println("GetOrder:",string(body))info:=new(OrderInfo)error := json.Unmarshal(body, &info)if err != nil {fmt.Println("GetOrder Unmarshal err:",error)}fmt.Println("GetOrder info:",info)return info }5錯誤
開發中遇到的問題:
401:token授權到期
403:有兩個原因 ,當時測試我遇到了第一個問題,改了之后,第二天又報錯了,確定項目絕對是關聯上的,然后發現是第二個問題導致的。
1:項目沒有關聯,或者關聯到錯誤的API項目上了。
2:谷歌服務的 BUG,在該應用的商店內,隨意增加一個內購或者訂閱,再看看是不是這個問題就消失了。新增的內購或者訂閱可以刪除的。
總結
以上是生活随笔為你收集整理的go语言google pay支付验证订单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git中的revert和reset
- 下一篇: [Git] Git整理(四) git r