Search in sources :

Example 11 with Receipt

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

the class PurchaseManager method isPurchaseValid.

/**
 * This tests that a purchase is still valid. Receipts can become obsolete at any point of
 * time via external systems, such as cancelling a subscription. Its important to check that
 * they are still valid before using them.
 *
 * @param sku                     The SKU of the receipt that needs to be validated.
 * @param purchaseManagerListener The purchase manager listener.
 */
public void isPurchaseValid(String sku, PurchaseManagerListener purchaseManagerListener) {
    validateSystemConfiguration();
    if (addPendingAction(new PendingAction(sku, purchaseManagerListener, PendingAction.ACTION.IS_PURCHASE_VALID))) {
        Log.i(TAG, "isPurchaseValid call for sku " + sku + " stored for future execution");
        return;
    }
    if (sku == null) {
        purchaseManagerListener.onValidPurchaseResponse(new Response(null, Response.Status.SUCCESSFUL, null), false, sku);
    }
    Receipt receipt = mReceiptMap.get(sku);
    // Check if receipt stored with purchaseManager is expired.
    if (!isLocalPurchaseDataValid(receipt)) {
        Log.d(TAG, "local purchase not valid for " + receipt);
        purchaseManagerListener.onValidPurchaseResponse(new Response(null, Response.Status.SUCCESSFUL, null), false, sku);
    } else {
        // Local receipt is still valid, now check the purchase system for receipt validity
        Log.d(TAG, "local purchase is valid for " + receipt);
        new PurchaseValidAction(this, sku, purchaseManagerListener, receipt).execute();
    }
}
Also used : Response(com.amazon.purchase.model.Response) Receipt(com.amazon.purchase.model.Receipt)

Example 12 with Receipt

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

the class PurchaseManagerTest method testPurchaseForAlreadyPurchased.

/**
 * Tests purchase for already purchased product
 */
@Test
public void testPurchaseForAlreadyPurchased() throws Exception {
    Receipt receipt = TestUtils.createReceipt("subSku", "subSku", new Date(), null);
    mPurchaseManager.mReceiptMap.put("subSku", receipt);
    mPurchaseManager.init(purchaseSystem, null);
    Thread.sleep(1000);
    mPurchaseManager.purchaseSku("subSku", new PurchaseManagerListener() {

        @Override
        public void onRegisterSkusResponse(Response response) {
        }

        @Override
        public void onValidPurchaseResponse(Response response, boolean validity, String sku) {
            assertEquals(Response.Status.SUCCESSFUL, response.getStatus());
            assertTrue(validity);
            assertTrue(mPurchaseManager.mReceiptMap.containsKey("subSku"));
            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 13 with Receipt

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

the class PurchaseManagerTest method testIsPurchaseValidForExpiredExistentPurchase.

/**
 * tests isPurchaseValid for expired existent purchase
 */
@Test
public void testIsPurchaseValidForExpiredExistentPurchase() throws Exception {
    mPurchaseManager.init(purchaseSystem, null);
    Thread.sleep(1000);
    Receipt receipt = TestUtils.createReceipt("subSku", "subSku", new Date(), new Date());
    mPurchaseManager.mReceiptMap.put("subSku", receipt);
    mPurchaseManager.isPurchaseValid("subSku", 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);
            verify(purchaseSystem).notifyFulfillment(any(String.class), any(UserData.class), any(Receipt.class), any(Receipt.FulfillmentStatus.class));
            assertFalse(mPurchaseManager.mReceiptMap.containsKey("subSku"));
            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 14 with Receipt

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

the class PurchaseManagerTest method testIsPurchaseValidForReceiptValidSystemFailure.

/**
 * tests isPurchaseValid for receipt validation system failure
 */
@Test
public void testIsPurchaseValidForReceiptValidSystemFailure() throws Exception {
    Response response = new Response("isPurchaseValidRequest", Response.Status.FAILED, null);
    doReturn(response).when(mockPurchaseSystem).listenerResponse();
    doReturn(false).when(mockPurchaseSystem).receiptValidResponse();
    mPurchaseManager.init(purchaseSystem, null);
    Thread.sleep(1000);
    Receipt receipt = TestUtils.createReceipt("rentSku", "rentSku", DateAndTimeHelper.addSeconds(new Date(), 25), 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.FAILED, response.getStatus());
            assertFalse(validity);
            assertTrue(mPurchaseManager.mReceiptMap.containsKey("rentSku"));
            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 15 with Receipt

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

the class PurchaseManagerTest method testIsPurchaseValidForValidPurchase.

/**
 * tests isPurchaseValid for valid purchase
 */
@Test
public void testIsPurchaseValidForValidPurchase() throws Exception {
    Response response = new Response("isPurchaseValidRequest", Response.Status.SUCCESSFUL, null);
    doReturn(response).when(mockPurchaseSystem).listenerResponse();
    doReturn(true).when(mockPurchaseSystem).receiptValidResponse();
    mPurchaseManager.init(purchaseSystem, null);
    Thread.sleep(1000);
    Receipt receipt = TestUtils.createReceipt("rentSku", "rentSku", DateAndTimeHelper.addSeconds(new Date(), 25), 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());
            assertTrue(validity);
            assertTrue(mPurchaseManager.mReceiptMap.containsKey("rentSku"));
            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

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