Search in sources :

Example 1 with Response

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);
}
Also used : Response(com.amazon.purchase.model.Response) Product(com.amazon.purchase.model.Product)

Example 2 with Response

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);
            }
        }
    };
}
Also used : Response(com.amazon.purchase.model.Response) Set(java.util.Set) Receipt(com.amazon.purchase.model.Receipt) UserData(com.amazon.purchase.model.UserData) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Response

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();
}
Also used : Response(com.amazon.purchase.model.Response)

Example 4 with Response

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);
}
Also used : Response(com.amazon.purchase.model.Response)

Example 5 with Response

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();
}
Also used : Response(com.amazon.purchase.model.Response) Receipt(com.amazon.purchase.model.Receipt) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Response (com.amazon.purchase.model.Response)21 Receipt (com.amazon.purchase.model.Receipt)11 Test (org.junit.Test)10 Date (java.util.Date)6 Product (com.amazon.purchase.model.Product)3 UserData (com.amazon.purchase.model.UserData)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Bundle (android.os.Bundle)1 PurchasingListener (com.amazon.device.iap.PurchasingListener)1 ProductDataResponse (com.amazon.device.iap.model.ProductDataResponse)1 PurchaseResponse (com.amazon.device.iap.model.PurchaseResponse)1 PurchaseUpdatesResponse (com.amazon.device.iap.model.PurchaseUpdatesResponse)1 UserDataResponse (com.amazon.device.iap.model.UserDataResponse)1 IPurchase (com.amazon.purchase.IPurchase)1 PurchaseManagerListener (com.amazon.purchase.PurchaseManagerListener)1 Gson (com.google.gson.Gson)1 MarketplaceConnectBody (com.zype.fire.api.Model.MarketplaceConnectBody)1 MarketplaceConnectBodyData (com.zype.fire.api.Model.MarketplaceConnectBodyData)1 HashMap (java.util.HashMap)1