内购

  • 先检查签名是否正确
  • 再查询google该订单信息是否正确
  • 具体细节请联系我的QQ
func VerifyGoogleSign(data, id string, sign string) (bool, error) {
    decodePublic, err := base64.StdEncoding.DecodeString(publicKey)
	if err != nil {
		return false, err
	}
	pubInterface, err := x509.ParsePKIXPublicKey(decodePublic)
	if err != nil {
		return false, err
	}
	pub := pubInterface.(*rsa.PublicKey)
	decodeSign, err := base64.StdEncoding.DecodeString(sign)
	if err != nil {
		return false, err
	}
	sh1 := sha1.New()
	sh1.Write([]byte(data))
	hashData := sh1.Sum(nil)
	result := rsa.VerifyPKCS1v15(pub, crypto.SHA1, hashData, decodeSign)
	if result != nil {
		return false, err
	}
	info := make(map[string]interface{})
	if err = json.Unmarshal([]byte(data), &info); err != nil {
		return false, err
	}
	v, ok := info["purchaseToken"]
	if !ok {
		return false, errors.New("info not find")
	}
	if v == "" {
		return false, errors.New("v==")
	}
	server, err := androidpublisher.NewService(context.Background(), option.WithCredentialsJSON([]byte(`{
    "type": "service_account",
    "project_id": "",
    "private_key_id": "",
    "private_key": "",
    "client_email": "",
    "client_id": "",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": ""
    }`)))
	if err != nil {
		return false, err
	}
	call, err := server.Purchases.Products.Get("android程序包名", id, fmt.Sprint(v)).Do()
	if err != nil {
		return false, err
	}
	if call.PurchaseState != 0 {
		return false, errors.New("call.PurchaseState != 0")
	}
	return true, nil

订阅

  • 具体细节请联系我的QQ
func VerifyGoogleSignV3(id string, token string) (string, int64, error) {
server, err := androidpublisher.NewService(context.Background(), option.WithCredentialsJSON([]byte(`{
    "type": "service_account",
    "project_id": "",
    "private_key_id": "",
    "private_key": "",
    "client_email": "",
    "client_id": "",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": ""
    }`)))
	if err != nil {
		return  "", 0, err
	}
	call, err := server.Purchases.Subscriptions.Get("android程序包名", id, token).Do()
	if err != nil {
		return "", 0, err
	}
	if *call.PurchaseType != 0 {
		return  "", 0, errors.New("call.PurchaseState != 0")
	}
	res, err := call.MarshalJSON()
	if err != nil {
		return  "", 0, err
	}
	return  string(res), call.ExpiryTimeMillis / 1000, nil
}

联系 QQ: 3355168235