Search in sources :

Example 1 with UserData

use of com.amazon.purchase.model.UserData 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 2 with UserData

use of com.amazon.purchase.model.UserData in project zype-firebuilder by zype.

the class AmazonInAppPurchase method createIapPurchasingListener.

/**
 * Creates IAP purchase listener from PurchaseListener.
 */
/*package private*/
PurchasingListener createIapPurchasingListener(final PurchaseListener listener) {
    Log.d(TAG, "PurchasingListener registered");
    return new PurchasingListener() {

        @Override
        public void onUserDataResponse(UserDataResponse userDataResponse) {
            Log.d(TAG, "UserDataResponse received " + userDataResponse.toString());
            Response response = createResponse(isSuccessful(userDataResponse), userDataResponse.getRequestId().toString());
            UserData userData = createUserDataFromIapUserData(userDataResponse.getUserData());
            listener.onGetUserDataResponse(response, userData);
        }

        @Override
        public void onProductDataResponse(ProductDataResponse productDataResponse) {
            Log.d(TAG, "ProductDataResponse received " + productDataResponse.toString());
            Response response = createResponse(isSuccessful(productDataResponse), productDataResponse.getRequestId().toString());
            Map<String, Product> productMap = createProductMapFromProductDataResponse(productDataResponse.getProductData());
            listener.onProductDataResponse(response, productMap, productDataResponse.getUnavailableSkus());
        }

        @Override
        public void onPurchaseResponse(PurchaseResponse purchaseResponse) {
            Log.d(TAG, "purchaseResponse received " + purchaseResponse.toString());
            Response response = createResponse(isSuccessful(purchaseResponse), purchaseResponse.getRequestId().toString());
            com.amazon.device.iap.model.Receipt iapReceipt = purchaseResponse.getReceipt();
            String sku = null;
            if (iapReceipt != null) {
                sku = iapReceipt.getSku();
            }
            listener.onPurchaseResponse(response, sku, createReceipt(iapReceipt), createUserDataFromIapUserData(purchaseResponse.getUserData()));
        }

        @Override
        public void onPurchaseUpdatesResponse(PurchaseUpdatesResponse purchaseUpdatesResponse) {
            Log.d(TAG, "purchaseUpdatesResponse received " + purchaseUpdatesResponse.toString());
            Response response = createResponse(isSuccessful(purchaseUpdatesResponse), purchaseUpdatesResponse.getRequestId().toString());
            List<Receipt> receipts = createReceiptList(purchaseUpdatesResponse.getReceipts());
            UserData userData = createUserDataFromIapUserData(purchaseUpdatesResponse.getUserData());
            listener.onUserDataResponse(response, receipts, userData, purchaseUpdatesResponse.hasMore());
        }
    };
}
Also used : UserDataResponse(com.amazon.device.iap.model.UserDataResponse) PurchasingListener(com.amazon.device.iap.PurchasingListener) Receipt(com.amazon.purchase.model.Receipt) UserData(com.amazon.purchase.model.UserData) Product(com.amazon.purchase.model.Product) PurchaseResponse(com.amazon.device.iap.model.PurchaseResponse) ProductDataResponse(com.amazon.device.iap.model.ProductDataResponse) Response(com.amazon.purchase.model.Response) PurchaseUpdatesResponse(com.amazon.device.iap.model.PurchaseUpdatesResponse) PurchaseResponse(com.amazon.device.iap.model.PurchaseResponse) UserDataResponse(com.amazon.device.iap.model.UserDataResponse) ProductDataResponse(com.amazon.device.iap.model.ProductDataResponse) PurchaseUpdatesResponse(com.amazon.device.iap.model.PurchaseUpdatesResponse)

Example 3 with UserData

use of com.amazon.purchase.model.UserData in project zype-firebuilder by zype.

the class AmazonInAppPurchaseTest method testNotifyFulfillment.

@Test
public void testNotifyFulfillment() {
    AmazonInAppPurchase purchase = new AmazonInAppPurchase();
    purchase.notifyFulfillment("", new UserData("", ""), new Receipt(), Receipt.FulfillmentStatus.FULFILLED);
    verifyStatic(times(1));
}
Also used : Receipt(com.amazon.purchase.model.Receipt) UserData(com.amazon.purchase.model.UserData) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with UserData

use of com.amazon.purchase.model.UserData in project zype-firebuilder by zype.

the class AmazonInAppPurchaseTest method testisPurchaseValid.

@Test
public void testisPurchaseValid() {
    AmazonInAppPurchase purchase = new AmazonInAppPurchase();
    AReceiptVerifier verifier = mock(AReceiptVerifier.class);
    purchase.registerReceiptVerification(verifier);
    purchase.isPurchaseValid("", new UserData("", ""), new Receipt());
    verify(verifier).validateReceipt(any(Context.class), any(String.class), any(String.class), any(UserData.class), any(Receipt.class), any(IPurchase.PurchaseListener.class));
}
Also used : Context(android.content.Context) Receipt(com.amazon.purchase.model.Receipt) UserData(com.amazon.purchase.model.UserData) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Receipt (com.amazon.purchase.model.Receipt)4 UserData (com.amazon.purchase.model.UserData)4 Response (com.amazon.purchase.model.Response)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Context (android.content.Context)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 Product (com.amazon.purchase.model.Product)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1