Advertisement
progrmor

Untitled

Jun 24th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. public interface purchasesInterface() {
  2. public void purchaseItem();
  3. }
  4. ________________
  5.  
  6. AndroidLauncher:
  7. public class AndroidLauncher extends AndroidApplication implements purchaseInterface{
  8.  
  9. @Override
  10.     protected void onCreate (Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  13.         initialize(new MainClass(this), config);
  14.         mHelper = new IabHelper(this, base64EncodedPublicKey);
  15.  
  16.         mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
  17.             public void onIabSetupFinished(IabResult result) {
  18.                 if (!result.isSuccess()) {
  19.                     // IF any problem occurs, log that.
  20.                     Log.d("IAB", "Problem setting up In-app Billing: " + result);
  21.                 }
  22.                 // IAB is success
  23.                 Log.d("IAB", "Billing Success: " + result);
  24.  
  25.                 processPurchases();
  26.  
  27.  
  28.             }
  29.         });
  30.    
  31.     }
  32.  
  33. @Override
  34.     public void purchaseItem() {
  35.         // TODO Auto-generated method stub
  36.         mHelper.launchPurchaseFlow(this, ITEM_1, RC_REQUEST, mPurchaseFinishedListener, "HANDLE_PAYLOADS");
  37.     }
  38.  
  39. __________________
  40. IOSLauncher:
  41.  
  42. public class IOSLauncher extends IOSApplication.Delegate implements purchaseInterface{
  43.  
  44. private InAppPurchaseManager iapManager;
  45. private Map<String, SKProduct> appStoreProducts;
  46.  
  47. @Override
  48.     protected IOSApplication createApplication() {
  49.         IOSApplicationConfiguration config = new IOSApplicationConfiguration();
  50.         config.orientationPortrait = true;
  51.         config.orientationLandscape = false;
  52.         return new IOSApplication(new MainClass(this), config);
  53.     }
  54.  
  55. @Override
  56.     public boolean didFinishLaunching(UIApplication application, UIApplicationLaunchOptions launchOptions) {
  57.         super.didFinishLaunching(application, launchOptions);
  58.         iapManager = new InAppPurchaseManager(new InAppPurchaseListener() {
  59.  
  60.             @Override
  61.             public void productsReceived(SKProduct[] skProducts) {
  62.                 // Store the products.
  63.                 appStoreProducts = new HashMap<String, SKProduct>();
  64.  
  65.                 for (int i = 0; i < skProducts.length; i++) {
  66.                     appStoreProducts.put(skProducts[i].getProductIdentifier().toString(), skProducts[i]);
  67.                 }
  68.  
  69.                 Gdx.app.debug("productsReceived", "Total products: " + appStoreProducts.size());
  70.                 // Fill your shop UI with products here.
  71.             }
  72.  
  73.             @Override
  74.             public void productsRequestFailed(SKRequest skRequest, NSError nsError) {
  75.  
  76.             }
  77.  
  78.             @Override
  79.             public void transactionCompleted(SKPaymentTransaction skPaymentTransaction) {
  80.                 // Purchase successfully completed.
  81.                 // Get the product identifier and award the product to the user.
  82.                 String productId = skPaymentTransaction.getPayment().getProductIdentifier().toString();
  83.                 Gdx.app.debug("IOSApplication transactionCompleted", "Retrieved id: " + productId);
  84.                 Gdx.app.debug("IOSApplication transactionCompleted", "Wanted id: " + IOSpurchaseInterface.TOMBSTONE_2);
  85.                 if (productId.equals(IOSpurchaseInterface.ITEM_1)) {
  86.                     item1 = true;
  87.                }
  88.             }
  89.  
  90.             @Override
  91.             public void transactionFailed(SKPaymentTransaction skPaymentTransaction, NSError nsError) {
  92.  
  93.             }
  94.  
  95.             @Override
  96.             public void transactionRestored(SKPaymentTransaction skPaymentTransaction) {
  97.                 // Purchase successfully restored.
  98.                 // Get the product identifier and award the product to the user. This is only useful for non-consumable products.
  99.                 String productId = skPaymentTransaction.getPayment().getProductIdentifier().toString();
  100.                 Gdx.app.debug("IOSApplication transactionRestored", "Retrieved id: " + productId);
  101.  
  102.                 if (productId.equals(purchaseInterface.ITEM_1)) {
  103.                     item1 = true;
  104.                 }
  105.             }
  106.  
  107.             @Override
  108.             public void transactionRestoreFailed(NSArray<SKPaymentTransaction> nsArray, NSError nsError) {
  109.  
  110.             }
  111.         });
  112.         // First request the available products from the app store.
  113.        
  114.          iapManager.restoreTransactions();
  115.         return true;
  116.     }
  117.  
  118.     public static void main(String[] argv) {
  119.         NSAutoreleasePool pool = new NSAutoreleasePool();
  120.         UIApplication.main(argv, null, IOSLauncher.class);
  121.         pool.close();
  122.  
  123.     }
  124.  
  125. @Override
  126.     public void purchaseItem() {
  127.         iapManager.requestProducts(IOSpurchaseInterface.ITEM_1);
  128.         // At any time you want to purchase a product.
  129.         if (iapManager.canMakePayments() && appStoreProducts != null) {
  130.             Gdx.app.debug("IOSApplication", "Buying products");
  131.             iapManager.purchaseProduct(appStoreProducts.get(IOSpurchaseInterface.ITEM_1));
  132.         }else{
  133.             Gdx.app.debug("IOSApplication payments?", ""+iapManager.canMakePayments());
  134.             //Gdx.app.debug("IOSApplication products", "" + appStoreProducts.size());
  135.         }
  136.     }
  137.  
  138. _____________________
  139.  
  140. Purchasing
  141. Screen Class:
  142.  
  143. public class FlowerScreen implements Screen, purchaseInterface {
  144. purchaseInterface pInt;
  145.  
  146.  public FlowerScreen(MainClass gam) {
  147.         game = gam;
  148.         this.pInt = game.pi;
  149. ...
  150. ...
  151.  
  152. buyBtn1.addListener(new ChangeListener() {
  153.  
  154.             @Override
  155.             public void changed(ChangeEvent event, Actor actor) {
  156.                 purchaseItem();
  157.             }
  158.  
  159.         });
  160.  
  161. @Override
  162.     public void purchaseItem() {
  163.         pInt.purchaseItem();
  164.  
  165.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement