Skip to content

Commit 965853b

Browse files
author
Robot Media
committed
Added onRequestPurchaseResponse. Closes robotmedia#23
1 parent 73689f9 commit 965853b

File tree

8 files changed

+119
-48
lines changed

8 files changed

+119
-48
lines changed

AndroidBillingLibrary/src/net/robotmedia/billing/BillingController.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import net.robotmedia.billing.model.TransactionManager;
3030
import net.robotmedia.billing.request.BillingRequest;
3131
import net.robotmedia.billing.request.ResponseCode;
32-
import net.robotmedia.billing.request.RestoreTransactions;
3332
import net.robotmedia.billing.security.DefaultSignatureValidator;
3433
import net.robotmedia.billing.security.ISignatureValidator;
3534
import net.robotmedia.billing.utils.Compatibility;
@@ -471,20 +470,17 @@ protected static void onRequestSent(long requestId, BillingRequest request) {
471470
* @see net.robotmedia.billing.request.ResponseCode
472471
*/
473472
protected static void onResponseCode(Context context, long requestId, int responseCode) {
474-
debug("Request " + requestId + " received response " + ResponseCode.valueOf(responseCode));
473+
final ResponseCode response = ResponseCode.valueOf(responseCode);
474+
debug("Request " + requestId + " received response " + response);
475475

476476
final BillingRequest request = pendingRequests.get(requestId);
477477
if (request != null) {
478478
pendingRequests.remove(requestId);
479-
request.onResponseCode(responseCode);
479+
request.onResponseCode(response);
480480
}
481481
}
482482

483-
/**
484-
*
485-
* @param restoreTransactions
486-
*/
487-
public static void onTransactionsRestored(RestoreTransactions restoreTransactions) {
483+
public static void onTransactionsRestored() {
488484
for (IBillingObserver o : observers) {
489485
o.onTransactionsRestored();
490486
}
@@ -680,4 +676,10 @@ private static boolean verifyNonce(JSONObject data) {
680676
}
681677
}
682678

679+
public static void onRequestPurchaseResponse(String itemId, ResponseCode response) {
680+
for (IBillingObserver o : observers) {
681+
o.onRequestPurchaseResponse(itemId, response);
682+
}
683+
}
684+
683685
}

AndroidBillingLibrary/src/net/robotmedia/billing/IBillingObserver.java

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,91 @@
11
/* Copyright 2011 Robot Media SL (http://www.robotmedia.net)
2-
*
3-
* Licensed under the Apache License, Version 2.0 (the "License");
4-
* you may not use this file except in compliance with the License.
5-
* You may obtain a copy of the License at
6-
*
7-
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
9-
* Unless required by applicable law or agreed to in writing, software
10-
* distributed under the License is distributed on an "AS IS" BASIS,
11-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
* See the License for the specific language governing permissions and
13-
* limitations under the License.
14-
*/
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1515

1616
package net.robotmedia.billing;
1717

18+
import net.robotmedia.billing.request.ResponseCode;
1819
import android.app.PendingIntent;
1920

2021
public interface IBillingObserver {
2122

2223
/**
2324
* Called after checking if in-app billing is supported or not.
24-
* @param supported if true, in-app billing is supported. Otherwise, it isn't.
25+
*
26+
* @param supported
27+
* if true, in-app billing is supported. Otherwise, it isn't.
2528
* @see BillingController#checkBillingSupported(android.content.Context)
2629
*/
2730
public void onBillingChecked(boolean supported);
28-
29-
/**
30-
* Called after requesting the purchase of the specified item.
31-
* @param itemId id of the item whose purchase was requested.
32-
* @param purchaseIntent a purchase pending intent for the specified item.
33-
* @see BillingController#requestPurchase(android.content.Context, String, boolean)
34-
*/
35-
public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent);
36-
31+
3732
/**
3833
* Called after the purchase of the specified item was cancelled.
39-
* @param itemId id of the item whose purchase was cancelled.
40-
* @see BillingController#startPurchaseIntent(android.app.Activity, PendingIntent, android.content.Intent)
34+
*
35+
* @param itemId
36+
* id of the item whose purchase was cancelled.
37+
* @see BillingController#startPurchaseIntent(android.app.Activity,
38+
* PendingIntent, android.content.Intent)
4139
*/
4240
public void onPurchaseCancelled(String itemId);
4341

4442
/**
4543
* Called after the purchase of the specified item was executed.
46-
* @param itemId id of the item whose purchase was executed.
47-
* @see BillingController#startPurchaseIntent(android.app.Activity, PendingIntent, android.content.Intent)
44+
*
45+
* @param itemId
46+
* id of the item whose purchase was executed.
47+
* @see BillingController#startPurchaseIntent(android.app.Activity,
48+
* PendingIntent, android.content.Intent)
4849
*/
4950
public void onPurchaseExecuted(String itemId);
5051

52+
/**
53+
* Called after requesting the purchase of the specified item.
54+
*
55+
* @param itemId
56+
* id of the item whose purchase was requested.
57+
* @param purchaseIntent
58+
* a purchase pending intent for the specified item.
59+
* @see BillingController#requestPurchase(android.content.Context, String,
60+
* boolean)
61+
*/
62+
public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent);
63+
5164
/**
5265
* Called after the purchase of the specified item was refunded.
53-
* @param itemId id of the item whose purchase was refunded.
54-
* @see BillingController#startPurchaseIntent(android.app.Activity, PendingIntent, android.content.Intent)
66+
*
67+
* @param itemId
68+
* id of the item whose purchase was refunded.
69+
* @see BillingController#startPurchaseIntent(android.app.Activity,
70+
* PendingIntent, android.content.Intent)
5571
*/
5672
public void onPurchaseRefunded(String itemId);
5773

5874
/**
59-
* Called after a restore transactions request has been successfully received by the server.
75+
* Called with the response for the purchase request of the specified item.
76+
* This is used for reporting various errors, or if the user backed out and
77+
* didn't purchase the item.
78+
*
79+
* @param itemId
80+
* id of the item whose purchase was requested
81+
* @param response
82+
* response of the purchase request
83+
*/
84+
public void onRequestPurchaseResponse(String itemId, ResponseCode response);
85+
86+
/**
87+
* Called after a restore transactions request has been successfully
88+
* received by the server.
6089
*/
6190
public void onTransactionsRestored();
6291

AndroidBillingLibrary/src/net/robotmedia/billing/request/BillingRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected Bundle makeRequestBundle() {
6565
return request;
6666
}
6767

68-
public void onResponseCode(int responseCode) {
68+
public void onResponseCode(ResponseCode responde) {
6969
// Do nothing by default
7070
}
7171

AndroidBillingLibrary/src/net/robotmedia/billing/request/RequestPurchase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ protected void processOkResponse(Bundle response) {
5353
BillingController.onPurchaseIntent(itemId, purchaseIntent);
5454
}
5555

56+
@Override
57+
public void onResponseCode(ResponseCode response) {
58+
super.onResponseCode(response);
59+
BillingController.onRequestPurchaseResponse(itemId, response);
60+
}
61+
62+
5663
}

AndroidBillingLibrary/src/net/robotmedia/billing/request/RestoreTransactions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public String getRequestType() {
3131
}
3232

3333
@Override
34-
public void onResponseCode(int responseCode) {
35-
super.onResponseCode(responseCode);
36-
if (ResponseCode.isResponseOk(responseCode)) {
37-
BillingController.onTransactionsRestored(this);
34+
public void onResponseCode(ResponseCode response) {
35+
super.onResponseCode(response);
36+
if (response == ResponseCode.RESULT_OK) {
37+
BillingController.onTransactionsRestored();
3838
}
3939
}
4040

AndroidBillingLibraryTest/src/net/robotmedia/billing/BillingControllerTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import net.robotmedia.billing.model.BillingDBTest;
2424
import net.robotmedia.billing.model.Transaction;
2525
import net.robotmedia.billing.model.TransactionTest;
26-
import net.robotmedia.billing.request.RestoreTransactions;
26+
import net.robotmedia.billing.request.ResponseCode;
2727
import android.app.PendingIntent;
2828
import android.test.AndroidTestCase;
2929
import android.test.suitebuilder.annotation.MediumTest;
@@ -115,21 +115,43 @@ public void testGetTransactionsString() throws Exception {
115115
public void testOnTransactionRestored() throws Exception {
116116
final Set<Boolean> flags = new HashSet<Boolean>();
117117
final IBillingObserver observer = new IBillingObserver() {
118-
119-
@Override
120118
public void onTransactionsRestored() {
121119
flags.add(true);
122120
}
121+
public void onPurchaseRefunded(String itemId) {}
122+
public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {}
123+
public void onPurchaseExecuted(String itemId) {}
124+
public void onPurchaseCancelled(String itemId) {}
125+
public void onBillingChecked(boolean supported) {}
126+
public void onRequestPurchaseResponse(String itemId, ResponseCode response) {}
127+
};
128+
BillingController.registerObserver(observer);
129+
BillingController.onTransactionsRestored();
130+
assertEquals(flags.size(), 1);
131+
BillingController.unregisterObserver(observer);
132+
}
133+
134+
@SmallTest
135+
public void testOnRequestPurchaseResponse() throws Exception {
136+
final String testItemId = TransactionTest.TRANSACTION_1.productId;
137+
final ResponseCode testResponse = ResponseCode.RESULT_OK;
138+
final Set<Boolean> flags = new HashSet<Boolean>();
139+
final IBillingObserver observer = new IBillingObserver() {
123140

141+
public void onTransactionsRestored() {}
124142
public void onPurchaseRefunded(String itemId) {}
125143
public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {}
126144
public void onPurchaseExecuted(String itemId) {}
127145
public void onPurchaseCancelled(String itemId) {}
128146
public void onBillingChecked(boolean supported) {}
147+
public void onRequestPurchaseResponse(String itemId, ResponseCode response) {
148+
flags.add(true);
149+
assertEquals(testItemId, itemId);
150+
assertEquals(testResponse, response);
151+
}
129152
};
130153
BillingController.registerObserver(observer);
131-
final RestoreTransactions request = new RestoreTransactions(getContext().getPackageName());
132-
BillingController.onTransactionsRestored(request);
154+
BillingController.onRequestPurchaseResponse(testItemId, testResponse);
133155
assertEquals(flags.size(), 1);
134156
BillingController.unregisterObserver(observer);
135157
}

AndroidBillingLibraryTest/src/net/robotmedia/billing/MockBillingActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package net.robotmedia.billing;
1717

18+
import net.robotmedia.billing.request.ResponseCode;
19+
1820
public class MockBillingActivity extends AbstractBillingActivity {
1921

2022
@Override
@@ -58,4 +60,9 @@ public void onTransactionsRestored() {
5860
// TODO Auto-generated method stub
5961
}
6062

63+
@Override
64+
public void onRequestPurchaseResponse(String itemId, ResponseCode response) {
65+
// TODO Auto-generated method stub
66+
}
67+
6168
}

DungeonsRedux/src/net/robotmedia/billing/example/Dungeons.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.robotmedia.billing.example.aux.CatalogEntry;
3030
import net.robotmedia.billing.model.Transaction;
3131
import net.robotmedia.billing.model.Transaction.PurchaseState;
32+
import net.robotmedia.billing.request.ResponseCode;
3233

3334
/**
3435
* A sample application based on the original Dungeons to demonstrate how to use
@@ -186,4 +187,7 @@ public void onNothingSelected(AdapterView<?> arg0) {
186187

187188
mOwnedItemsTable = (ListView) findViewById(R.id.owned_items);
188189
}
190+
191+
@Override
192+
public void onRequestPurchaseResponse(String itemId, ResponseCode response) {}
189193
}

0 commit comments

Comments
 (0)