android IAP unmaneged items 服务器校验

当成功IAP以后, 会在google服务器记录此次购买的状态. 可以通过Google Play Android Developer API去请求此状态, 从而完成校验和发给玩家相应的道具.

1> 客户端字串, orderId(订单ID), productId(购买道具名), packageName(APP包名), purchaseToken(token, 唯一值), 此4个串是校验需要用到的, 传给服务器.

2> 调用Google Play Android Developer API(https://developers.google.com/android-publisher/api_usage) 需要使用OAuth2.0, 可以采用Java, Python, .Net, Ruby, PHP等(https://developers.google.com/identity/protocols/OAuth2WebServer)

本文使用python实现.

3> 设置环境

    在console.developers.google.com启用Google Play Android Developer API接口

    在play.google.com中设置API权限, OAUTH客户端项目设置

4> 脚本实现

    

import httplib2
import pprint
import sys
import time
import os
import MySQLdb list_bill=[]
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials def main(argv):
# connect the db to get bill
db=MySQLdb.connect("localhost","root","pass",sys.argv[1])
cursor=db.cursor()
try:
# 查询客户端传来的字串
cursor.callproc('getbill',('2'))
results=cursor.fetchall()
while(cursor.nextset()):
print "111111111111"
for result in results:
# Load the key in PKCS 12 format that you downloaded from the Google API
# Console when you created your Service account.
f = file('console中的p12 key的路径', 'rb')
key = f.read()
f.close() # Create an httplib2.Http object to handle our HTTP requests and authorize it
# with the Credentials. Note that the first parameter, service_account_name,
# is the Email address created for the Service account. It must be the email
# address associated with the key that was created.
credentials = SignedJwtAssertionCredentials(
'play.google.com中OAUTH授权账号',
key,
scope='https://www.googleapis.com/auth/androidpublisher')
http = httplib2.Http()
http = credentials.authorize(http)
#service build
service = build("androidpublisher", "v2", http=http) #get bill
list_bill=result[4].split(' ')
transaction_id=list_bill[0]
product_id=list_bill[1]
packagename=list_bill[2]
token=list_bill[3]
try:
print "try to get"
lists = service.purchases().products().get(packageName=packagename,productId=product_id,token=token).execute(http=http)
except:
# bill is missing or invalid bill
cursor.callproc(# sql处理代码)
while(cursor.nextset()):
print "111111111111"
db.commit()
continue pprint.pprint(lists)
# 判断是否是合法且未消费
if(lists['purchaseState']==0 and lists['consumptionState']==0):
diff=time.time()-float(lists['purchaseTimeMillis'][0:10])
if(diff>2592000):
#over time bill, record the log
cursor.callproc(# sql处理代码)
while(cursor.nextset()):
print "111111111111"
db.commit()
continue
else:
#good receipt
num=product_id.split('.')[3]
cursor.callproc('check_bill',(result[0],1,num,time.time(),'',transaction_id))
while(cursor.nextset()):
print "111111111111"
db.commit()
continue
# 已消费
elif(lists['purchaseState']==0 and lists['consumptionState']==1):
cursor.callproc(# sql处理代码)
while(cursor.nextset()):
print "111111111111"
db.commit()
print "Already consumed"
continue
except:
# sql get is wrong
print "sql err"
finally:
cursor.close()
db.close() if __name__ == '__main__':
while(1):
if(os.path.exists("/tmp/stop_gp_iap_check-"+sys.argv[1]+".txt")):
print "stop"
break
else:
main(sys.argv)
time.sleep(2)

  

      

[Android] Google IAP unmaneged items服务器校验的更多相关文章

  1. SDK接入(2)之Android Google Play内支付(in-app Billing)接入

    SDK接入(2)之Android Google Play内支付(in-app Billing)接入 继上篇SDK接入(1)之Android Facebook SDK接入整理完Facebook接入流程之 ...

  2. Android Webview SSL 自签名安全校验解决方案

    服务器证书校验主要针对 WebView 的安全问题. 在 app 中需要通过 WebView 访问 url,因为服务器采用的自签名证书,而不是 ca 认证,使用 WebView 加载 url 的时候会 ...

  3. 转: android之虚拟机访问tomcat服务器资源

    最近在研究Android虚拟机访问tomcat服务器资源,所以找了个时间写下这篇博客和大家分享一下心得. 其实Android虚拟机访问tomcat服务器非常的简单,只要不要弄错IP地址就可以访问tom ...

  4. Android Google 地图 API for Android

    从健康类 app Runkeeper 到游戏 app 精灵宝可梦,位置服务对现代 app 来说越来越重要. 在本文中,我们将创建一个 app,名字就叫做 City Guide.这个 app 允许用户搜 ...

  5. Android操作HTTP实现与服务器通信(转)

    Android操作HTTP实现与服务器通信   本示例以Servlet为例,演示Android与Servlet的通信. 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方 ...

  6. Android提交数据到JavaWeb服务器实现登录

    之前学习Android提交数据到php服务器没有成功,在看了两三个星期的视频之后,现在终于实现了与服务器的交互.虽然完成的不是PHP端的,但是在这个过程还是学到了不少东西的.现在我先来展示一下我的成果 ...

  7. android文件上传到服务器

    package uploadDemo; import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStre ...

  8. android上传文件到服务器

    package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...

  9. Android google map 两点之间的距离

    在Android google map中,有时候会碰到计算两地的距离,下面的辅助类就可以帮助你计算距离: public class DistanceHelper { /** Names for the ...

随机推荐

  1. ubuntu启动tomcat出错解决方案

    从终端进入tomcat的bin目录,然后执行startup.sh,出现以下异常: Cannot find ./catalina.sh The file is absent or does not ha ...

  2. spring 配置bean

    Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...

  3. Coursera台大机器学习课程笔记6 -- The VC Dimension

    本章的思路在于揭示VC Dimension的意义,简单来说就是假设的自由度,或者假设包含的feature vector的个数(一般情况下),同时进一步说明了Dvc和,Eout,Ein以及Model C ...

  4. github 删除仓库 repository

    1.点开想要删除的仓库 2点击setting 3.拉到最下面 4.点击 Delete this repository 5.输入想删除仓库的名字 点击

  5. LigerUI 表单和表格中的combobox如何初始化值

    摘要: 在修改基础信息的时候,通常会遇到需要修改值为选择值的时候,这时候,数据库存的一般是value,而不是显示的text值,但页面显示的时候如果显示成数字型的值,通常会给人不够直观的感觉.因此,要求 ...

  6. 推荐!手把手教你使用Git

    推荐!手把手教你使用Git 原文出处: 涂根华的博客   http://blog.jobbole.com/78960/ 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与 ...

  7. windows下使用火狐浏览器插件AutoProxy+MyEnTunnel+SSH访问海外站点(转)

    windows下使用火狐浏览器插件AutoProxy+MyEnTunnel+SSH访问海外站点   平时需要查阅一些技术资料,光走VPN太浪费流量,所以这儿教大家一种使用火狐浏览器的插件 AutoPr ...

  8. T3500通过PXE克隆报“Unable to Control A20 Line XMS Driver not installed”

    问题:使用deepin_ghost1.6中的PXE网络GHOST时提示如下错误信息: ERROR:Unable to control A20 line!XMS Driver not installed ...

  9. AjaxUpload跨域上传问题

    1. 调用上传的html页面所在域名是 www.abc.com ,添加document.domain <script> document.domain = "abc.com&qu ...

  10. PRINCE2项目管理七大流程之指导流程

    PRINCE2项目管理七大流程之指导流程   今天学习七大流程之二--项目指导流程 项目指导流程的目的就是使项目管理委员会能够对项目的成功负责,具体来说,是由项目管理委员会作出关键决策,并进行总体控制 ...