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. Linux环境数据备份Python脚本

    #!/usr/bin/python#Filename:backupscript.pyimport osimport time # The files and directories to be bac ...

  2. oracle归档模式和非归档模式的切换

    Oracle从未归档日志改成归档日志: SQL> shutdown immediate; 数据库已经关闭. 已经卸载数据库. Oracle 例程已经关闭. SQL> startup mou ...

  3. linux--------------今天又遇到一个奇葩的问题,就是linux文件的权限已经是777了但是还是没有写入权限,按照下面的命令就解决了

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  4. Intellij IDE 常用设置

    1· 去除代码提示的Case sensitive(比如Sprite,键入sprite不进行任何提示) Editor->Code Completion->Case sensitive com ...

  5. c语言的学习秘籍之链表

    刚翻出来的作品,有点低级,但希望能起到作用: #include<stdio.h>#include<stdlib.h>#include<time.h>#include ...

  6. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  7. Apache部署django项目

    在此之前,我们一直使用django的manage.py 的runserver 命令来运行django应用,但这只是我们的开发环境,当项目真正部署上线的时候这做就不可行了,必须将我们的项目部署到特定的w ...

  8. 【转】PowerShell入门(十一):编写脚本模块

    转至:http://www.cnblogs.com/ceachy/archive/2013/03/08/PowerShell_Script_Module.html 现在通过编写模块就可以在PowerS ...

  9. asp.net 事件加载顺序

    下面是母版页与内容页合并后事件的发生顺序: 母版页控件 Init 事件. 内容控件 Init 事件. 母版页 Init 事件. 内容页 Init 事件. 内容页 Load 事件. 母版页 Load 事 ...

  10. [转] 经典SQL练习题

    原题目来自qaz13177_58_CSDN博客 http://blog.csdn.net/qaz13177_58_/article/details/5575711/#sql 只是更新个人答案供参考 表 ...