Android in-app billing, how to handle app reinstalls

我怕爱的太早我们不能终老 提交于 2019-12-04 08:07:32
Dawid Drozd

JSON

An example of a JSON order object that includes a subscription purchase token is shown below.

{ "nonce" : 1836535032137741465,

  "orders" :
    [{ "notificationId" : "android.test.purchased",
       "orderId" : "transactionId.android.test.purchased",
       "packageName" : "com.example.dungeons",
       "productId" : "android.test.purchased",
       "developerPayload" : "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ",
       "purchaseTime" : 1290114783411,
       "purchaseState" : 0,
       "purchaseToken" : "rojeslcdyyiapnqcynkjyyjh" }]
}

How to parse JSON in Java.

Android supports JSON library

Android Billing

If you have a remote server, we recommend that you store purchase information on your server instead of in a local database on a device. Look here.

Read about Managed per user account, Unmanaged, or Subscription

I use billing like this:

  1. In onCreate

    BillingHelper.setCompletedHandler(handlerTransaction);
    

    You have handler in your code.

  2. Next in onClick or something

    BillingHelper.requestPurchase(this, currentMarketProduct.getMarketId());
    

You are on the right track with the second update. The trick is to realise everything is done in async.

The typical flow is as follows:

  1. User installs your app.

  2. On first load of your app, you check if you need to restore purchases.

  3. If you do, send a RESTORE_TRANSACTION synchronous request to Google.

  4. Google will respond with a acknowlegment response to your RESTORE_TRANSACTION request. (This is only an acknowlegement that they received your request.)

  5. At this point, you should mark that you had already sent a restore request to Google and no further restores needs to be sent from the app.

  6. Now asynchronously Google will start sending a 'PURCHASE_STATE_CHANGED' event to your app for each in-app purchase the user has previously purchased. This call is the same as what Google would had sent if the user had made that purchase for the first time.

  7. Since it's the same call, your app would pick up the event and handled it normally as if the user has just purchased the in-app product (thereby "restoring" the purchased feature).

In regard to steps 2 and 5, what I've done for my app is to keep a SharedPreference value called 'APP_INITIALISED' that defaults to false. Everytime my app starts up, if 'APP_INITIALISED' is false, I tell Google to RESTORE_TRANSACTION (step 2) then I set APP_INITIALISED to true (step 5).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!