订阅样本缺少对purchaseToken的JSON解析

时间:2012-08-17 09:00:08

标签: android in-app-billing subscription

我最近一直在研究In-App-Billing v2的代码。并发现了一些问题。 我之前已实施In-App-Billing但希望升级到订阅。在我的研究中,我发现 Purchase token 与成功购买订阅的JSON(签名数据)一起收到。

成功验证签名的演示 Security.java 解析Json但是此处缺少元素Purchase-token的解析。

            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", "");

我还没有运行代码,因为订阅没有测试产品-id并且需要实际购买。我想知道的是这个令牌在这里被解析,或者提供的示例代码是否正确实现了这部分。 / p>

1 个答案:

答案 0 :(得分:5)

文森特,这是正确的。如果需要验证purhcaseToken,则必须修改Security.java以及BillingService.java和ResponseHandler.java。

这是:

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;
    }
}

现在在VerifyPurchase:

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

BillingService.java:

在purchaseStateChanged中,修改:

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

ResponseHandler.java:

最后,将purchaseResponse的定义更改为:

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)

在这里,您可以进行与应用相关的逻辑更改,因为您将拥有purchaseToken。

如果一切正常,请接受!