Search in sources :

Example 11 with Response

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

the class DefaultReceiptVerificationService method validateReceipt.

/**
 * {@inheritDoc}
 */
@Override
public String validateReceipt(Context context, String requestId, String sku, UserData userData, Receipt receipt, IPurchase.PurchaseListener listener) {
    Response purchaseResponse = new Response(requestId, Response.Status.SUCCESSFUL, null);
    listener.isPurchaseValidResponse(purchaseResponse, sku, receipt, true, userData);
    return requestId;
}
Also used : Response(com.amazon.purchase.model.Response)

Example 12 with Response

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

the class PurchaseAction method purchased.

/**
 * Inform the user of the request completion.
 *
 * @param requestId The request id of this call.
 */
private void purchased(String requestId) {
    Log.d(TAG, "purchased mSku " + mSku + " with request " + requestId);
    Response response = mPurchaseManager.purchaseResponseMap.get(requestId);
    Receipt receipt = mPurchaseManager.purchaseReceiptMap.get(requestId);
    /* Zype, Evgeny Cherkasov
         * begin */
    if (receipt != null) {
        // Bundle receiptExtras = new Bundle();
        // receiptExtras.putString("VideoId", mPurchaseManagerListener.getVideoId());
        // receipt.setExtras(receiptExtras);
        receipt.setExtras(mPurchaseManagerListener.getPurchaseExtras());
    }
    if (Response.Status.SUCCESSFUL.equals(response.getStatus())) {
        if (receipt == null) {
            // Hack for AmazonInAppPurchase, if the product was already purchased they do not
            // return receipt, but the purchase is valid so we need to show the content. We
            // also trigger an update purchases call here to fetch any latest purchases and
            // update them in our system.
            Log.i(TAG, "mSku already purchased, but is not stored in our system, " + "update inventory");
            new UpdatePurchasesAction(mPurchaseManager, false).execute();
            if (mPurchaseManagerListener != null) {
                mPurchaseManagerListener.onValidPurchaseResponse(new Response(response.getRequestId(), Response.Status.SUCCESSFUL, null), true, mSku);
            }
        } else {
            // Validate that the receipt received is not fake.
            new PurchaseValidAction(mPurchaseManager, mSku, mPurchaseManagerListener, receipt).execute();
        }
    } else {
        if (mPurchaseManagerListener != null) {
            mPurchaseManagerListener.onValidPurchaseResponse(new Response(response.getRequestId(), Response.Status.FAILED, null), false, mSku);
        }
    }
    cleanUp(requestId);
}
Also used : Response(com.amazon.purchase.model.Response) Receipt(com.amazon.purchase.model.Receipt)

Example 13 with Response

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

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

the class PurchaseHelper method initializePurchaseSystem.

/**
 * Method to initialize purchase system
 */
private void initializePurchaseSystem() {
    // The purchase system should be initialized by the module initializer, if there is no
    // initializer available that means the purchase system is not needed.
    IPurchase purchaseSystem = (IPurchase) ModuleManager.getInstance().getModule(IPurchase.class.getSimpleName()).getImpl(true);
    if (purchaseSystem == null) {
        Log.i(TAG, "Purchase system not registered.");
        return;
    }
    try {
        registerSkuForActions();
    } catch (Exception e) {
        Log.e(TAG, "Could not register actions!!", e);
    }
    // Register the purchase system received via ModuleManager and configure the purchase
    // listener.
    this.mPurchaseManager = PurchaseManager.getInstance(mContext.getApplicationContext());
    try {
        mPurchaseManager.init(purchaseSystem, new PurchaseManagerListener() {

            @Override
            public void onRegisterSkusResponse(Response response) {
                if (response == null || !Response.Status.SUCCESSFUL.equals(response.getStatus())) {
                    Log.e(TAG, "Register products failed " + response);
                } else {
                    // If there is a valid receipt available in the system, set content browser
                    // variable as true.
                    String sku = mPurchaseManager.getPurchasedSku();
                    if (sku == null) {
                        setSubscription(false, null);
                    } else {
                        setSubscription(true, sku);
                    }
                    Log.d(TAG, "Register products complete.");
                }
                mContentBrowser.updateContentActions();
            }

            @Override
            public void onValidPurchaseResponse(Response response, boolean validity, String sku) {
                Log.e(TAG, "You should not hit here!!!");
            }

            /* Zype, Evgeny Cherkasov
                 * begin */
            @Override
            public void onProductDataResponse(Response response, Map<String, Product> products) {
                Log.e(TAG, "You should not hit here!!!");
            }

            // @Override
            // public String getVideoId() {
            // Log.e(TAG, "You should not hit here!!!");
            // return null;
            // }
            @Override
            public Bundle getPurchaseExtras() {
                Log.e(TAG, "You should not hit here!!!");
                return null;
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Could not configure the purchase system. ", e);
    }
}
Also used : Response(com.amazon.purchase.model.Response) Bundle(android.os.Bundle) Product(com.amazon.purchase.model.Product) IPurchase(com.amazon.purchase.IPurchase) IOException(java.io.IOException) PurchaseManagerListener(com.amazon.purchase.PurchaseManagerListener)

Example 15 with Response

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

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