[Android] Google IAP unmaneged items伺服器校正

來源:互聯網
上載者:User

標籤:

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伺服器校正

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.