4

我最近一直在研究 In-App-Billing v2 的代码。并发现了一些问题。我之前已经实施了应用内计费,但​​希望升级到订阅。在我的研究中,我发现购买令牌与成功购买订阅的 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 并且需要实际购买。我想知道的是这里解析这个令牌,或者提供的示例代码是否正确实现了这部分。

4

1 回答 1

5

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

这里是:

安全.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);

计费服务.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。

如果一切正常,请接受!

于 2012-09-12T17:01:58.377 回答