標籤:
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 httplib2import pprintimport sysimport timeimport osimport MySQLdblist_bill=[]from apiclient.discovery import buildfrom oauth2client.client import SignedJwtAssertionCredentialsdef main(argv):# connect the db to get billdb=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 buildservice = build("androidpublisher", "v2", http=http)#get billlist_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 billcursor.callproc(# sql處理代碼)while(cursor.nextset()):print "111111111111"db.commit()continuepprint.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 logcursor.callproc(# sql處理代碼)while(cursor.nextset()):print "111111111111"db.commit()continueelse:#good receiptnum=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"continueexcept:# sql get is wrongprint "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"breakelse:main(sys.argv)time.sleep(2)
[Android] Google IAP unmaneged items伺服器校正