Search in sources :

Example 1 with Receipt

use of com.amazon.purchase.model.Receipt 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 Receipt

use of com.amazon.purchase.model.Receipt 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)

Example 3 with Receipt

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

the class MockIPurchaseImpl method getUserPurchaseData.

@Override
public String getUserPurchaseData(boolean reset) {
    List<Receipt> list = new ArrayList<>();
    list.addAll(TestUtils.receiptMap.values());
    Response response = listenerResponse();
    listener.onUserDataResponse(response, list, TestUtils.userData, false);
    return "getUserPurchaseDataSuccess";
}
Also used : Response(com.amazon.purchase.model.Response) Receipt(com.amazon.purchase.model.Receipt) ArrayList(java.util.ArrayList)

Example 4 with Receipt

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

the class MockIPurchaseImpl method purchase.

@Override
public String purchase(String sku) {
    Receipt receipt = TestUtils.createReceipt(sku, "title", new Date(), null);
    listener.onPurchaseResponse(purchaseListenerResponse(), sku, receipt, TestUtils.userData);
    return "purchaseRequest";
}
Also used : Receipt(com.amazon.purchase.model.Receipt) Date(java.util.Date)

Example 5 with Receipt

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

the class AmazonInAppPurchase method createReceipt.

/**
 * Converts a single IAP receipt to a purchase receipt.
 *
 * @param iapReceipt The IAP receipt to be converted.
 * @return The purchase receipt.
 */
private Receipt createReceipt(com.amazon.device.iap.model.Receipt iapReceipt) {
    if (iapReceipt == null) {
        return null;
    }
    Receipt receipt = new Receipt();
    receipt.setSku(iapReceipt.getSku());
    receipt.setPurchasedDate(iapReceipt.getPurchaseDate());
    receipt.setExpiryDate(iapReceipt.getCancelDate());
    receipt.setReceiptId(iapReceipt.getReceiptId());
    receipt.setProductType(getProductType(iapReceipt.getProductType()));
    return receipt;
}
Also used : Receipt(com.amazon.purchase.model.Receipt)

Aggregations

Receipt (com.amazon.purchase.model.Receipt)17 Response (com.amazon.purchase.model.Response)11 Test (org.junit.Test)8 Date (java.util.Date)7 UserData (com.amazon.purchase.model.UserData)4 ArrayList (java.util.ArrayList)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 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1