Search in sources :

Example 1 with Product

use of com.codename1.payment.Product in project CodenameOne by codenameone.

the class CodenameOneActivity method getProducts.

Product[] getProducts(String[] skus, boolean fromCacheOnly) {
    if (inventory != null) {
        ArrayList pList = new ArrayList<Product>();
        ArrayList moreskusList = new ArrayList<Product>();
        for (int i = 0; i < skus.length; i++) {
            String sku = skus[i];
            if (inventory.hasDetails(sku)) {
                SkuDetails details = inventory.getSkuDetails(sku);
                Product p = new Product();
                p.setSku(sku);
                p.setDescription(details.getDescription());
                p.setDisplayName(details.getTitle());
                p.setLocalizedPrice(details.getPrice());
                pList.add(p);
            } else {
                moreskusList.add(sku);
            }
        }
        // if the inventory does not all the requestes sku make an update.
        if (moreskusList.size() > 0 && !fromCacheOnly) {
            try {
                inventory = mHelper.queryInventory(true, moreskusList);
                return getProducts(skus, true);
            } catch (IabException ex) {
                Logger.getLogger(CodenameOneActivity.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        Product[] products = new Product[pList.size()];
        products = (Product[]) pList.toArray(products);
        return products;
    }
    return null;
}
Also used : SkuDetails(com.codename1.payments.v3.SkuDetails) ArrayList(java.util.ArrayList) Product(com.codename1.payment.Product) IabException(com.codename1.payments.v3.IabException)

Example 2 with Product

use of com.codename1.payment.Product in project CodenameOne by codenameone.

the class JavaSEPort method getInAppPurchase.

public Purchase getInAppPurchase() {
    return new Purchase() {

        private Vector purchases;

        @Override
        public Product[] getProducts(String[] skus) {
            return null;
        }

        @Override
        public boolean isItemListingSupported() {
            return false;
        }

        @Override
        public boolean isManagedPaymentSupported() {
            return managedPurchaseSupported;
        }

        @Override
        public boolean isManualPaymentSupported() {
            return manualPurchaseSupported;
        }

        @Override
        public boolean isRefundable(String sku) {
            if (!refundSupported) {
                return false;
            }
            int val = JOptionPane.showConfirmDialog(window, "Is " + sku + " refundable?", "Purchase", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            return val == JOptionPane.YES_OPTION;
        }

        private Vector getPurchases() {
            if (purchases == null) {
                purchases = (Vector) Storage.getInstance().readObject("CN1InAppPurchases");
                if (purchases == null) {
                    purchases = new Vector();
                }
            }
            return purchases;
        }

        private void savePurchases() {
            if (purchases != null) {
                Storage.getInstance().writeObject("CN1InAppPurchases", purchases);
            }
        }

        @Override
        public boolean isSubscriptionSupported() {
            return subscriptionSupported;
        }

        @Override
        public boolean isUnsubscribeSupported() {
            return subscriptionSupported;
        }

        @Override
        public String pay(final double amount, final String currency) {
            try {
                if (Display.getInstance().isEdt()) {
                    final String[] response = new String[1];
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            response[0] = pay(amount, currency);
                        }
                    });
                    return response[0];
                }
                if (!manualPurchaseSupported) {
                    throw new RuntimeException("Manual payment isn't supported check the isManualPaymentSupported() method!");
                }
                final String[] result = new String[1];
                final boolean[] completed = new boolean[] { false };
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        int res = JOptionPane.showConfirmDialog(window, "A payment of " + amount + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (res == JOptionPane.YES_OPTION) {
                            result[0] = UUID.randomUUID().toString();
                        }
                        completed[0] = true;
                    }
                });
                Display.getInstance().invokeAndBlock(new Runnable() {

                    public void run() {
                        while (!completed[0]) {
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException err) {
                            }
                        }
                    }
                });
                if (getPurchaseCallback() != null) {
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            if (result[0] != null) {
                                getPurchaseCallback().paymentSucceeded(result[0], amount, currency);
                            } else {
                                getPurchaseCallback().paymentFailed(UUID.randomUUID().toString(), null);
                            }
                        }
                    });
                }
                return result[0];
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }

        @Override
        public void purchase(final String sku) {
            if (managedPurchaseSupported) {
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        final int res = JOptionPane.showConfirmDialog(window, "An in-app purchase of " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        Display.getInstance().callSerially(new Runnable() {

                            public void run() {
                                if (res == JOptionPane.YES_OPTION) {
                                    com.codename1.payment.Purchase.postReceipt(Receipt.STORE_CODE_SIMULATOR, sku, "cn1-iap-sim-" + UUID.randomUUID().toString(), System.currentTimeMillis(), "");
                                    getPurchaseCallback().itemPurchased(sku);
                                    getPurchases().addElement(sku);
                                    savePurchases();
                                } else {
                                    getPurchaseCallback().itemPurchaseError(sku, "Purchase failed");
                                }
                            }
                        });
                    }
                });
            } else {
                throw new RuntimeException("In app purchase isn't supported on this platform!");
            }
        }

        @Override
        public void refund(final String sku) {
            if (refundSupported) {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        getPurchaseCallback().itemRefunded(sku);
                        getPurchases().removeElement(sku);
                        savePurchases();
                    }
                });
            }
        }

        @Override
        public void subscribe(final String sku) {
            if (subscriptionSupported) {
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        final int res = JOptionPane.showConfirmDialog(window, "An in-app subscription to " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        Display.getInstance().callSerially(new Runnable() {

                            public void run() {
                                if (res == JOptionPane.YES_OPTION) {
                                    getPurchaseCallback().subscriptionStarted(sku);
                                    getPurchases().addElement(sku);
                                    savePurchases();
                                } else {
                                    getPurchaseCallback().itemPurchaseError(sku, "Subscription failed");
                                }
                            }
                        });
                    }
                });
            }
        }

        @Override
        public void unsubscribe(final String sku) {
            if (subscriptionSupported) {
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        final int res = JOptionPane.showConfirmDialog(window, "In-app unsubscription request for " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        Display.getInstance().callSerially(new Runnable() {

                            public void run() {
                                if (res == JOptionPane.YES_OPTION) {
                                    getPurchaseCallback().subscriptionCanceled(sku);
                                    getPurchases().removeElement(sku);
                                    savePurchases();
                                } else {
                                    getPurchaseCallback().itemPurchaseError(sku, "Error in unsubscribe");
                                }
                            }
                        });
                    }
                });
            }
        }

        @Override
        public boolean wasPurchased(String sku) {
            return getPurchases().contains(sku);
        }
    };
}
Also used : Purchase(com.codename1.payment.Purchase) Product(com.codename1.payment.Product) Vector(java.util.Vector) Point(java.awt.Point) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) EOFException(java.io.EOFException) FontFormatException(java.awt.FontFormatException)

Example 3 with Product

use of com.codename1.payment.Product in project CodenameOne by codenameone.

the class ZoozPurchase method getProducts.

public synchronized Product[] getProducts(String[] skus) {
    int numSkus = skus.length;
    final Product[] p = new Product[numSkus];
    for (int iter = 0; iter < numSkus; iter++) {
        p[iter] = new Product();
    }
    fetchProductsFailed = false;
    fetchProductsComplete = false;
    nativeInstance.fetchProducts(skus, p);
    // wait for request to complete
    Display.getInstance().invokeAndBlock(new Runnable() {

        @Override
        public void run() {
            while (!fetchProductsFailed && !fetchProductsComplete) {
                try {
                    Thread.currentThread().sleep(10);
                } catch (InterruptedException ex) {
                }
            }
        }
    });
    if (fetchProductsFailed) {
        Log.e(new IOException("Failed to fetch products: " + fetchProductsFailedMessage));
        return null;
    }
    return p;
}
Also used : Product(com.codename1.payment.Product) IOException(java.io.IOException)

Aggregations

Product (com.codename1.payment.Product)3 Purchase (com.codename1.payment.Purchase)1 IabException (com.codename1.payments.v3.IabException)1 SkuDetails (com.codename1.payments.v3.SkuDetails)1 FontFormatException (java.awt.FontFormatException)1 Point (java.awt.Point)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1