Skip to content

Commit

Permalink
proper purchase flow with screen orientation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
serggl committed Sep 9, 2014
1 parent 3a25045 commit 209dc08
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion library/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anjlab.android.iab.v3"
android:versionCode="1"
android:versionName="1.0.11">
android:versionName="1.0.12">

<uses-sdk
android:minSdkVersion="8"
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'maven'
apply plugin: 'signing'

group 'com.anjlab.android.iab.v3'
version '1.0.11'
version '1.0.12'

android {
compileSdkVersion 8
Expand Down
2 changes: 1 addition & 1 deletion library/library.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.anjlab.android.iab.v3" external.system.module.version="1.0.11" type="JAVA_MODULE" version="4">
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.anjlab.android.iab.v3" external.system.module.version="1.0.12" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down
22 changes: 17 additions & 5 deletions library/src/com/anjlab/android/iab/v3/BillingProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public static interface IBillingHandler {
private static final String RESTORE_KEY = ".products.restored" + SETTINGS_VERSION;
private static final String MANAGED_PRODUCTS_CACHE_KEY = ".products.cache" + SETTINGS_VERSION;
private static final String SUBSCRIPTIONS_CACHE_KEY = ".subscriptions.cache" + SETTINGS_VERSION;
private static final String PURCHASE_PAYLOAD_CACHE_KEY = ".purchase.last" + SETTINGS_VERSION;

private IInAppBillingService billingService;
private String contextPackageName;
private String purchasePayload;
private String signatureBase64;
private BillingCache cachedProducts;
private BillingCache cachedSubscriptions;
Expand Down Expand Up @@ -148,7 +148,8 @@ private boolean loadPurchasesByType(String type, BillingCache cacheStorage) {
for (int i=0; i<purchaseList.size(); i++) {
String jsonData = purchaseList.get(i);
JSONObject purchase = new JSONObject(jsonData);
cacheStorage.put(purchase.getString(Constants.RESPONSE_PRODUCT_ID), jsonData, signatureList.get(i));
String signature = signatureList != null && signatureList.size() > i ? signatureList.get(i) : null;
cacheStorage.put(purchase.getString(Constants.RESPONSE_PRODUCT_ID), jsonData, signature);
}
}
return true;
Expand Down Expand Up @@ -179,7 +180,8 @@ private boolean purchase(String productId, String purchaseType) {
if (!isInitialized() || TextUtils.isEmpty(productId) || TextUtils.isEmpty(purchaseType))
return false;
try {
purchasePayload = purchaseType + ":" + UUID.randomUUID().toString();
String purchasePayload = purchaseType + ":" + UUID.randomUUID().toString();
savePurchasePayload(purchasePayload);
Bundle bundle = billingService.getBuyIntent(Constants.GOOGLE_API_VERSION, contextPackageName, productId, purchaseType, purchasePayload);
if (bundle != null) {
int response = bundle.getInt(Constants.RESPONSE_CODE);
Expand Down Expand Up @@ -297,8 +299,10 @@ public TransactionDetails getSubscriptionTransactionDetails(String productId) {
public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PURCHASE_FLOW_REQUEST_CODE)
return false;
int responseCode = data.getIntExtra(Constants.RESPONSE_CODE, Constants.BILLING_RESPONSE_RESULT_OK);
if (resultCode == Activity.RESULT_OK && responseCode == Constants.BILLING_RESPONSE_RESULT_OK) {
int responseCode = data.getIntExtra(Constants.RESPONSE_CODE, Constants.BILLING_RESPONSE_RESULT_OK);
Log.d(LOG_TAG, String.format("resultCode = %d, responseCode = %d", resultCode, responseCode));
String purchasePayload = getPurchasePayload();
if (resultCode == Activity.RESULT_OK && responseCode == Constants.BILLING_RESPONSE_RESULT_OK && !TextUtils.isEmpty(purchasePayload)) {
String purchaseData = data.getStringExtra(Constants.INAPP_PURCHASE_DATA);
String dataSignature = data.getStringExtra(Constants.RESPONSE_INAPP_SIGNATURE);
try {
Expand Down Expand Up @@ -360,4 +364,12 @@ public void setPurchaseHistoryRestored() {
saveBoolean(getPreferencesBaseKey() + RESTORE_KEY, true);
}

private void savePurchasePayload(String value) {
saveString(getPreferencesBaseKey() + PURCHASE_PAYLOAD_CACHE_KEY, value);
}

private String getPurchasePayload() {
return loadString(getPreferencesBaseKey() + PURCHASE_PAYLOAD_CACHE_KEY, null);
}

}
Binary file modified sample/libs/anjlab-iabv3-current.jar
Binary file not shown.

0 comments on commit 209dc08

Please sign in to comment.