<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <!-- permissions to download files and billing support --> <uses-permission android:name="android.permission.INTERNET" /> ... </manifest>
public class DictActivity extends Activity { // API . private OpenIabHelper mIABHelper; private boolean isIABHelperSetup = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dict); // API. OpenIabHelper.Options.Builder builder = new OpenIabHelper.Options.Builder() .setCheckInventory(true) .addPreferredStoreName(OpenIabHelper.NAME_YANDEX) .setVerifyMode(OpenIabHelper.Options.VERIFY_EVERYTHING) .addStoreKeys(SettingsManager.STORE_KEYS_MAP); // . mIABHelper = new OpenIabHelper(this, builder.build()); mIABHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if(!result.isSuccess()) { isIABHelperSetup = false; //TODO: . return; } if(mIABHelper != null) { isIABHelperSetup = true; //API . } } }); } @Override public void onDestroy() { super.onDestroy(); if(mIABHelper != null) mIABHelper.dispose(); mIABHelper = null; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... <!-- permissions to download files and billing support --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="org.onepf.openiab.permission.BILLING" /> ... </manifest>
... if((mIABHelper != null) && isIABHelperSetup) { String sku = "duct_1"; mIABHelper.launchPurchaseFlow(DictActivity.this, sku, 10001, mPurchaseFinishedListener); } ...
public class DictActivity extends Activity { ... private void makePurchase() { if((mIABHelper != null) && isIABHelperSetup) { String sku = "duct_1"; mIABHelper.launchPurchaseFlow(DictActivity.this, sku, 10001, mPurchaseFinishedListener); } } ... @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if((mIABHelper == null) || !mIABHelper.handleActivityResult(requestCode, resultCode, data)) super.onActivityResult(requestCode, resultCode, data); } ... private OnIabPurchaseFinishedListener mPurchaseFinishedListener = new OnIabPurchaseFinishedListener() { public void onIabPurchaseFinished(IabResult result, Purchase purchase) { if(result.isFailure()) { //TODO: - , . return; } // String purchaseName = purchase.getSku(); markPurchaseAsConfirmed(purchaseName); } }; private void markPurchaseAsConfirmed(String purchase) { //TODO: // UI . } }
public class DictActivity extends Activity { ... @Override public void onIabSetupFinished(IabResult result) { if(!result.isSuccess()) { isIABHelperSetup = false; //TODO: . return; } if(mIABHelper != null) { isIABHelperSetup = true; //API . // . mIABHelper.queryInventoryAsync(mGotInventoryListener); } } ... private QueryInventoryFinishedListener mGotInventoryListener = new QueryInventoryFinishedListener() { @Override public void onQueryInventoryFinished(IabResult result, Inventory inventory) { if (result.isFailure()) { //TODO: . return; } // . List<Purchase> purchases = inventory.getAllPurchases(); for(Purchase p : purchases) { String sku = p.getSku(); if(!isPurchaseConfirmed(sku)) markPurchaseAsConfirmed(sku); } } }; private boolean isPurchaseConfirmed(String purchase) { //TODO: true, , // false. } private void markPurchaseAsConfirmed(String purchase) { //TODO: , , // UI . } }
public class DictActivity extends BaseActivity { // API . private OpenIabHelper mIABHelper; private boolean isIABHelperSetup = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dict); // API. OpenIabHelper.Options.Builder builder = new OpenIabHelper.Options.Builder() .setCheckInventory(true) .addPreferredStoreName(OpenIabHelper.NAME_YANDEX) .setVerifyMode(OpenIabHelper.Options.VERIFY_EVERYTHING) .addStoreKeys(SettingsManager.STORE_KEYS_MAP); // . mIABHelper = new OpenIabHelper(this, builder.build()); mIABHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if(!result.isSuccess()) { isIABHelperSetup = false; //TODO: . return; } if(mIABHelper != null) { isIABHelperSetup = true; //API . // . mIABHelper.queryInventoryAsync(mGotInventoryListener); } } }); final Button buy = (Button)findViewById(R.id.buy); buy.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if((mIABHelper != null) && isIABHelperSetup) { String payload = "payload"; String sku = "duct_1"; mIABHelper.launchPurchaseFlow(DictActivity.this, sku, 10001, mPurchaseFinishedListener, payload); } } }); } @Override public void onDestroy() { super.onDestroy(); if(mIABHelper != null) mIABHelper.dispose(); mIABHelper = null; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if((mIABHelper == null) || !mIABHelper.handleActivityResult(requestCode, resultCode, data)) super.onActivityResult(requestCode, resultCode, data); } private OnIabPurchaseFinishedListener mPurchaseFinishedListener = new OnIabPurchaseFinishedListener() { public void onIabPurchaseFinished(IabResult result, Purchase purchase) { if (result.isFailure()) { showMessage("Error purchasing: " + result); return; } String purchaseName = purchase.getSku(); markPurchaseAsConfirmed(purchaseName); } }; private QueryInventoryFinishedListener mGotInventoryListener = new QueryInventoryFinishedListener() { @Override public void onQueryInventoryFinished(IabResult result, Inventory inventory) { if (result.isFailure()) { //TODO: . return; } // . List<Purchase> purchases = inventory.getAllPurchases(); for(Purchase p : purchases) { String sku = p.getSku(); if(!isPurchaseConfirmed(sku)) markPurchaseAsConfirmed(sku); } } }; private void markPurchaseAsConfirmed(String purchase) { //TODO: , , // UI . } private boolean isPurchaseConfirmed(String purchase) { //TODO: true, , // false. } }
Source: https://habr.com/ru/post/238531/
All Articles