Subscription sample missing JSON parsing of purchaseToken

喜夏-厌秋 提交于 2019-12-21 17:54:26

问题


I have been recently studying code for In-App-Billing v2. And have found some issues with it. I have earlier implemented In-App-Billing but wish to upgrade to subscription. On my research I found that Purchase token is recieved along with the JSON(Signed data) on successful purchase for subscription.

The demo's Security.java on successful verification of signature parses Json however the parsing of element Purchase-token is missing here.

            JSONObject jElement = jTransactionsArray.getJSONObject(i);
            int response = jElement.getInt("purchaseState");
            PurchaseState purchaseState = PurchaseState.valueOf(response);
            String productId = jElement.getString("productId");
            String packageName = jElement.getString("packageName");
            long purchaseTime = jElement.getLong("purchaseTime");
            String orderId = jElement.optString("orderId", "");
            String notifyId = null;
//          purchaseToken part that I have added
            String purchaseToken = jElement.optString("purchaseToken", "");

I haven't yet run the code since subscription doesn't have test product-ids and requires actual purchase.What I want to know is this token be parsed here or is the sample code provided has this part correctly implemented.


回答1:


Vincent, that is correct. You have to modify Security.java as well as BillingService.java and ResponseHandler.java if you need the purhcaseToken to be validated.

Here it is:

Security.java:

public PurchaseState    purchaseState;
public String           notificationId;
public String           productId;
public String           orderId;
public long             purchaseTime;
public String           developerPayload;
public String           purchaseToken;
public String           packageName;

public VerifiedPurchase(PurchaseState purchaseState, String notificationId, String productId, String orderId, long purchaseTime,
        String developerPayload, String purchaseToken, String packageName) {
    this.purchaseState = purchaseState;
    this.notificationId = notificationId;
    this.productId = productId;
    this.orderId = orderId;
    this.purchaseTime = purchaseTime;
    this.developerPayload = developerPayload;
    this.purchaseToken = purchaseToken;
    this.packageName = packageName;
    }
}

Now in VerifyPurchase:

String developerPayload = jElement.optString("developerPayload", null);
// VK: Changes to parse the purchaseToken
String purchaseToken = jElement.optString("purchaseToken", null);

BillingService.java:

In purchaseStateChanged, modify:

ResponseHandler.purchaseResponse(this, vp.purchaseState, vp.productId, vp.orderId, vp.purchaseTime, vp.developerPayload, vp.purchaseToken, vp.packageName);

ResponseHandler.java:

Finally, change the definition of purchaseResponse to:

public static void purchaseResponse(final Context context, final PurchaseState purchaseState, final String productId, final String orderId, final long purchaseTime, final String developerPayload, final String purchaseToken,final String packageName)

Here you can make your app related logic changes as you here will have the purchaseToken.

If everything works, please accept!



来源:https://stackoverflow.com/questions/12002788/subscription-sample-missing-json-parsing-of-purchasetoken

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