Skip to content

Commit

Permalink
query cached purchases. fix a bug that makes me wonder how any of thi…
Browse files Browse the repository at this point in the history
…s even ever worked.
  • Loading branch information
koush committed Feb 1, 2012
1 parent 6d7435d commit 99f5533
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/com/clockworkmod/billing/ClockworkModBillingClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,7 @@
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpMessage;
Expand Down Expand Up @@ -57,7 58,6 @@

import com.android.vending.billing.IMarketBillingService;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalAdvancedPayment;
import com.paypal.android.MEP.PayPalInvoiceData;
import com.paypal.android.MEP.PayPalInvoiceItem;
import com.paypal.android.MEP.PayPalPayment;
Expand Down Expand Up @@ -534,7 534,41 @@ private static boolean checkNonce(Context context, long nonce, long cacheDuratio
}

public static final long CACHE_DURATION_FOREVER = Long.MAX_VALUE;

public List<ClockworkOrder> getCachedClockworkPurchases(String buyerId) {
ArrayList<ClockworkOrder> ret = new ArrayList<ClockworkOrder>();
SharedPreferences orderData = getOrderData();

try {
String proofString = orderData.getString("server-purchases", null);
if (proofString == null)
throw new Exception();

JSONObject proof = new JSONObject(proofString);
Log.i(LOGTAG, proof.toString(4));
String signedData = proof.getString("signed_data");
String signature = proof.getString("signature");
if (!checkSignature(mClockworkPublicKey, signedData, signature))
throw new Exception();

proof = new JSONObject(signedData);
if (proof.optBoolean("sandbox", true) != mSandbox)
throw new Exception();
String sellerId = proof.optString("seller_id", null);
if (!mSellerId.equals(sellerId))
throw new Exception();
if (!buyerId.equals(proof.getString("buyer_id")))
throw new Exception();
JSONArray orders = proof.getJSONArray("orders");
for (int i = 0; i < orders.length(); i ) {
JSONObject order = orders.getJSONObject(i);
ret.add(new ClockworkOrder(order));
} }
catch (Exception ex) {
}
return ret;
}

private CheckPurchaseResult[] checkCachedPurchases(Context context, String productId, String buyerId, long marketCacheDuration, long billingCacheDuration, SharedPreferences orderData) {
Editor edit = orderData.edit();
CheckPurchaseResult[] result = new CheckPurchaseResult[3];
Expand Down Expand Up @@ -739,23 773,22 @@ public void onReceive(Context context, Intent intent) {
catch (Exception ex) {
}

state.refreshedServer = true;
state.restoredMarket = true;
if (BillingReceiver.CANCELLED.equals(intent.getAction())) {
state.marketResult = CheckPurchaseResult.notPurchased();
}
else if (BillingReceiver.SUCCEEDED.equals(intent.getAction())) {
CheckPurchaseResult[] cachedResults = checkCachedPurchases(context, productId, buyerId, CACHE_DURATION_FOREVER, 0, orderData);
CheckPurchaseResult cachedResult = cachedResults[0];
if (cachedResult.isPurchased())
state.serverResult = cachedResult;
state.marketResult = cachedResult;
else
state.serverResult = CheckPurchaseResult.notPurchased();
state.marketResult = CheckPurchaseResult.notPurchased();
}
else {
state.marketResult = CheckPurchaseResult.notPurchased();
}
Log.i(LOGTAG, "In app billing result: " state.marketResult);
state.restoredMarket = true;
reportPurchase.run();
}
};
Expand Down

0 comments on commit 99f5533

Please sign in to comment.