2017-01-21 9 views
1

私は自分のアプリにサブスクリプション製品をセットアップして購入することができます。私はGoogle開発者コンソールでその購入を見る。私がログアウトしてからアプリに戻ったとき、私は再び購読ページに連れて行きます。アプリはサブスクリプションが購入されたことを認識せず、ユーザーがアクセスできるようにする必要があります。購読ボタンをもう一度クリックすると、「あなたはすでに購読しています...購読を管理しています」と表示されます。私は、サブスクリプションを確認した後にユーザをサブスクライブした情報に送るか、サブスクリプションが見つからない場合は拒否し、ユーザをサブスクライブページに送信するために何をする必要があるかはわかりません。私はまだそれを構築する方法を知っている知識がまだないと思う。ここに私のコードがあります。私はそれを扱うコードの塊を見逃していますか?Google InAppBillingアプリログインの定期購読確認

登録/ログインのバックエンドとしてfirebaseを使用しています。トークンなどを保持するためにfirebaseを利用する必要があるかどうかはわかりません。私は初心者であり、学んでいることは分かっていますが、私のプロジェクトは98%完了しています。

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

import com.iliprivateequity.btfxforexalerts.Util.IabHelper; 
import com.iliprivateequity.btfxforexalerts.Util.IabResult; 
import com.iliprivateequity.btfxforexalerts.Util.Inventory; 
import com.iliprivateequity.btfxforexalerts.Util.Purchase; 

public class GoogleInAppBilling extends AppCompatActivity implements View.OnClickListener { 

    private String base64EncodedPublicKey = "*****"; 

    public Button buttonSubscribe; 
    public Button buttonSubscribed; 
    IabHelper mHelper; 
    static final String SKU_SUBSCRIBE_BTFX = "btfx_alerts_service_50"; 
    static final String TAG = "BTFX_Alerts"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_google_in_app_billing); 

     loadInAppPurchase(); 

     buttonSubscribe = (Button) findViewById(R.id.buttonsubscribe); 
     buttonSubscribed = (Button) findViewById(R.id.button_subscribed); 
     buttonSubscribed.setEnabled(false); //todo remember to set back to false. its true just for testing the build 

     buttonSubscribe.setOnClickListener(this); 
     buttonSubscribed.setOnClickListener(this); 

    } 

    private void loadInAppPurchase() { 
     mHelper = new IabHelper(this, base64EncodedPublicKey); 
     mHelper.enableDebugLogging(true); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 

      @Override 
      public void onIabSetupFinished(IabResult result) { 
       if (!result.isSuccess()) { 
        Log.d(TAG, "Problem setting up in-app billing: " + result); 
       } else { 
        Log.d(TAG, "Setup successful. Querying inventory."); 
       } 
       mHelper.queryInventoryAsync(mGotInventoryListener); 
      } 


     }); 
    } 

    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 

     @Override 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      Log.d(TAG, "Query inventory finished."); 
      if (result.isFailure()) { 
       Log.d(TAG, "Failed to query inventory: " + result); 
       return; 

      } 
      Log.d(TAG, "Query inventory was successful."); 

      Purchase sku_purchase = inventory.getPurchase(SKU_SUBSCRIBE_BTFX); 
      if (sku_purchase != null) { 
       mHelper.consumeAsync(inventory.getPurchase(SKU_SUBSCRIBE_BTFX), 
         mConsumeFinishedListener); 
       return; 

      } 
     } 
    }; 

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener(){ 
     public void onConsumeFinished(Purchase purchase, IabResult result) { 
      Log.d("Tag", "Consumption finished. Purchase: " + purchase + ", result: " + result); 

      if (mHelper == null) 
       return; 

      if (result.isSuccess()) { 
       Log.d("TAG", "Consumption successful. Provisioning."); 
      } else { 
       Log.d("TAG", "Error while consuming: " + result); 
      } 
      Log.d("Tag", "End consumption flow."); 
     } 
    }; 

    @Override 
    public void onClick(View v) { 
     if (v == buttonSubscribe) { 
      mHelper.launchPurchaseFlow(GoogleInAppBilling.this, SKU_SUBSCRIBE_BTFX, IabHelper.ITEM_TYPE_SUBS 
        , 10001, mPurchaseFinishedListener, "alertservicepurchased"); 
     } 
     if (v == buttonSubscribed){ 
      startActivity(new Intent(this, ProfileActivity.class)); 
     } 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (!mHelper.handleActivityResult(requestCode, resultCode, data)){ 
      super.onActivityResult(requestCode,resultCode,data); 
     }else{ 
      Log.d("TAG", "onActivityResult handled by IABUtil."); 
     } 
    } 

    // Callback for when a purchase is finished 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
      = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
      Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase); 

      if (mHelper == null) return; 

      if (result.isFailure()) { 
       Log.d(TAG, "Error purchasing: " + result); 
       return; 

      } 
      Log.d(TAG, "BTFX Alerts subscription purchased."); 
      if (purchase.getSku().equals(SKU_SUBSCRIBE_BTFX)) { 
       Log.d("TAG", "Thank you for subscribing!"); 
       mHelper.consumeAsync(purchase, mConsumeFinishedListener); 
       // bought the btfx alerts subscription 

       buttonSubscribe.setEnabled(false); 
       buttonSubscribed.setEnabled(true); 

      } 

     } 
    }; 
}; 

答えて

1

ユーザーが既にサブスクリプションを持っているかどうかを確認するだけで、支払い済みのアイテムの表示/非表示に依存します。

There is例その方法。また、libraryを使用して、アンドロイド上のIAPでの作業を簡素化することも検討できます。

関連する問題