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();
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations