use of com.amazon.purchase.model.Response in project zype-firebuilder by zype.
the class ProductsAction method onResponse.
/**
* Inform the user of the request completion.
*
* @param requestId The request id of this call.
*/
private void onResponse(String requestId) {
Log.d(TAG, "onResponse(): request=" + requestId);
Response response = mPurchaseManager.responseMap.get(requestId);
Map<String, Product> products = mPurchaseManager.productsDataMap.get(requestId);
if (mPurchaseManagerListener != null) {
mPurchaseManagerListener.onProductDataResponse(response, products);
}
cleanUp(requestId);
}
use of com.amazon.purchase.model.Response in project zype-firebuilder by zype.
the class PurchaseManager method createPurchaseListener.
/**
* Create a listener that is required to make calls to purchase system.
*
* @return The purchase listener.
*/
private IPurchase.PurchaseListener createPurchaseListener() {
return new IPurchase.PurchaseListener() {
/**
* {@inheritDoc}
*/
@Override
public void onProductDataResponse(Response response, Map<String, Product> productDetails, Set<String> invalidSkus) {
// Not required for this system.
/* Zype, Evgeny Cherkasov */
Log.d(TAG, "onProductDataResponse(): response= " + response.getRequestId());
// Saving the response and product data.
responseMap.put(response.getRequestId(), response);
productsDataMap.put(response.getRequestId(), productDetails);
if (productsObjectMap.containsKey(response.getRequestId())) {
Log.d(TAG, "onProductDataResponse(): productsObjectMap contains " + response.getRequestId());
// Inform user.
productsObjectMap.get(response.getRequestId()).informUser(response.getRequestId());
} else {
// Products request object not available, moving on.
Log.d(TAG, "onProductDataResponse(): productsObjectMap does not contain " + response.getRequestId());
}
}
/**
* {@inheritDoc}
*/
@Override
public void onGetUserDataResponse(Response response, UserData userData) {
// Not required for this system.
}
/**
* {@inheritDoc}
* This method first validates that the received receipts belong to the same user as
* the one registered in the system. If its not the case then it resets the current
* logged in user to the one received via the call and clears the cache receipts from
* the system.
* Then the method parses through the receipt list, validates each receipt is still
* valid and adds it to the receipts stored in the system. If the receipt is not
* valid it unregisters it from the system.
*/
@Override
public void onUserDataResponse(Response response, List<Receipt> receiptList, UserData userData, boolean hasMore) {
if (Response.Status.SUCCESSFUL == response.getStatus()) {
if (userData != null && !userData.equals(mUserData)) {
// Current user is not the same as registered user, update the registered
// user and reset the receipts
mUserData = userData;
mReceiptMap.clear();
}
Log.d(TAG, "user purchases count " + receiptList.size());
for (Receipt receipt : receiptList) {
Log.d(TAG, "Receipt received " + receipt);
// Received receipt for a valid SKU.
if (mSkuDataMap.containsKey(receipt.getSku())) {
// registering it in the system.
if (isLocalPurchaseDataValid(receipt)) {
registerReceiptSku(receipt);
} else {
unregisterSkuReceipt(receipt);
}
} else {
Log.e(TAG, "Received receipt for a non-supported sku " + receipt.getSku() + " receipt id is " + receipt.getReceiptId());
}
}
// There are more receipts available, poll for them.
if (hasMore) {
Log.d(TAG, "has more purchases");
new UpdatePurchasesAction(sInstance, false).execute();
} else // User purchase updates complete.
{
setPurchaseUpdateStatus(PurchaseUpdateStatus.COMPLETED);
completeUpdatePurchaseCall(response);
}
} else {
Log.e(TAG, "Failed to get user purchases ", response.getThrowable());
setPurchaseUpdateStatus(PurchaseUpdateStatus.FAILED);
// Inform the user that SKUs purchase update call is complete.
completeUpdatePurchaseCall(response);
}
}
/**
* {@inheritDoc}
*/
@Override
public void onPurchaseResponse(Response response, String sku, Receipt receipt, UserData userData) {
try {
Log.d(TAG, "onPurchaseResponse " + response.getRequestId());
// Saving the response and receipts.
purchaseReceiptMap.put(response.getRequestId(), receipt);
purchaseResponseMap.put(response.getRequestId(), response);
if (purchaseObjectMap.containsKey(response.getRequestId())) {
Log.d(TAG, "purchaseObjectMap contains " + response.getRequestId());
// Inform user.
purchaseObjectMap.get(response.getRequestId()).informUser(response.getRequestId());
} else {
// Purchase request object not available, moving on.
Log.d(TAG, "purchaseObjectMap does not contain " + response.getRequestId());
}
} catch (Exception e) {
Log.e(TAG, "onPurchaseResponse exception", e);
}
}
/**
* {@inheritDoc}
* This method registers or unregisters the receipts from the system based on the {link
* #purchaseValid} response.
*/
@Override
public void isPurchaseValidResponse(Response response, String sku, Receipt receipt, boolean purchaseValid, UserData userData) {
Response purchaseManagerResponse;
boolean result;
String requestId = response.getRequestId();
if (Response.Status.SUCCESSFUL.equals(response.getStatus())) {
purchaseManagerResponse = new Response(requestId, Response.Status.SUCCESSFUL, null);
if (!purchaseValid) {
// Purchase is not valid, unregister it from the system.
Log.d(TAG, "purchase not valid " + response);
unregisterSkuReceipt(receipt);
result = false;
} else {
Log.d(TAG, "purchase valid " + response);
// Purchase is valid, register it into the system.
registerReceiptSku(receipt);
result = true;
}
} else {
purchaseManagerResponse = new Response(requestId, Response.Status.FAILED, null);
result = false;
}
// Save the response.
purchaseResponseMap.put(requestId, purchaseManagerResponse);
purchaseValidResultMap.put(requestId, result);
// Inform user.
if (purchaseValidObjectMap.containsKey(requestId)) {
purchaseValidObjectMap.get(requestId).informUser(requestId);
}
}
};
}
use of com.amazon.purchase.model.Response in project zype-firebuilder by zype.
the class PurchaseManager method purchaseSku.
/**
* Purchases a SKU. This method first validates that the SKU is not already purchased using the
* {@link #isLocalPurchaseDataValid(Receipt)} method to avoid to duplicate purchases.
*
* @param sku The SKU to purchase.
* @param purchaseManagerListener The purchase manager listener.
*/
public void purchaseSku(String sku, PurchaseManagerListener purchaseManagerListener) {
validateSystemConfiguration();
PendingAction pendingAction = new PendingAction(sku, purchaseManagerListener, PendingAction.ACTION.PURCHASE);
// Action pending on other ongoing requests.
if (addPendingAction(pendingAction)) {
Log.d(TAG, "purchase action for sku " + sku + " added in pending list");
return;
}
if (isLocalPurchaseDataValid(mReceiptMap.get(sku))) {
purchaseManagerListener.onValidPurchaseResponse(new Response(null, Response.Status.SUCCESSFUL, null), true, sku);
return;
}
// Initiate purchase.
new PurchaseAction(this, sku, purchaseManagerListener).execute();
}
use of com.amazon.purchase.model.Response in project zype-firebuilder by zype.
the class PurchaseValidAction method runPurchaseValidFlow.
/**
* Starts the purchase validation flow.
*
* @param requestId The request id of this call.
*/
private void runPurchaseValidFlow(String requestId) {
Response response = mPurchaseManager.purchaseResponseMap.get(requestId);
Boolean result = mPurchaseManager.purchaseValidResultMap.get(requestId);
if (mPurchaseManagerListener != null) {
mPurchaseManagerListener.onValidPurchaseResponse(response, result, mSku);
}
cleanUp(requestId);
}
use of com.amazon.purchase.model.Response in project zype-firebuilder by zype.
the class PurchaseManagerTest method testIsPurchaseValidForExpiredRentPurchase.
/**
* tests isPurchaseValid for expired rent purchase
*/
@Test
public void testIsPurchaseValidForExpiredRentPurchase() throws Exception {
mPurchaseManager.init(purchaseSystem, null);
Thread.sleep(1000);
Receipt receipt = TestUtils.createReceipt("rentSku", "rentSku", new Date(), null);
mPurchaseManager.mReceiptMap.put("rentSku", receipt);
mPurchaseManager.isPurchaseValid("rentSku", new PurchaseManagerListener() {
@Override
public void onRegisterSkusResponse(Response response) {
}
@Override
public void onValidPurchaseResponse(Response response, boolean validity, String sku) {
assertEquals(Response.Status.SUCCESSFUL, response.getStatus());
assertFalse(validity);
assertFalse(mPurchaseManager.mReceiptMap.containsKey("rentSku"));
verify(purchaseSystem).notifyFulfillment(any(String.class), any(UserData.class), any(Receipt.class), any(Receipt.FulfillmentStatus.class));
verifyUtil.verified();
}
});
Thread.sleep(1000);
verify(verifyUtil).verified();
}
Aggregations