Google Pay支付验证订单的Go语言实现(google play支付验证)(google验证付款信息)

访客 342 0

访问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,它看起来像一长串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,请务必及时保存。

Google Pay支付验证订单的Go语言实现(google play支付验证)(google验证付款信息)-第1张图片-谷歌商店上架

3.获取token

grant_type : refresh_token
client_id //在谷歌后台获取
client_secret //没有秘钥可不用这个参数
refresh_toke //上方2获取refresh_token
Google Pay支付验证订单的Go语言实现(google play支付验证)(google验证付款信息)-第2张图片-谷歌商店上架

4验证订单

获取令牌,查询订单的代码如下:

package main

import (
    encoding/json
    fmt
    io/ioutil
    net/http
    net/url
)

var (
    PackageName   =  // 包名
    GrantType     = refresh_token // 刷新令牌类型
    ClientId      =  // 客户端ID
    ClientSecret  =  // 客户端秘钥
	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` 
	ConsumptionState	int `json:consumptionState`
	DeveloperPayload	string` json:developerPayload`
	OrderId             string` json:orderId`
	AcknowledgementState int` json:acknowledgementState`
}

func main() {
	info := PostRefreshToken()// 获取token凭证
	
	productID := // 商品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},
    })
    
    if err != nil {
        fmt.Println(Error:, err)
        return nil
    }
    
    defer resp.Body.Close()
    
    body, _ := ioutil.ReadAll(resp.Body)
    
    var tokenInfo TokenInfo
	err = json.Unmarshal(body, &tokenInfo)
	
	if err != nil {
	    fmt.Println(Error:, err)
	    return nil
	}
	
	return &tokenInfo
}

标签: 谷歌商店上架 在谷歌 登录谷歌账号

发表评论 (已有0条评论)

还木有评论哦,快来抢沙发吧~