Android-consume in app payment - 结果:IabResult:类型' subs'无法消费

时间:2014-10-14 15:19:25

标签: android

我正在以应用付款方式撰写耗材。这是我的代码:

static final String SKU_PREMIUM = "goldvip1";
boolean mIsPremium = false;
static final int RC_REQUEST = 12345;
IabHelper mHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.payit);
    initilize();
    actionbar();
    String base64EncodedPublicKey = "MIHNMA0GCSqGSIb3DQEBAQUAA4G7ADCBtwKBrwDS6t/zu28hM3uSAn93f+S0Nlvk1sajcmWiULyeURQs8ebf1eB8jDm6fQXIBp0q3KDIjPgdla5ideXybxpkVcQCF9EKugyu8MizjwgtmRjTlPO+/XHgJXpdvrGVtKsdsu4GtOvl5MIV6jVvUgltmS68qxct+rEA6GtXvJPrnhzeI0LxND6A6qUbdZn/bXY6qw2Hq0NmUgM/+GzL1cfFDxlOOKLlnJ/oqnwOsq4ILbWsocCAwEAAQ==";
    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.v("this", "Problem setting up In-app Billing: " + result);
            }else if(result.isSuccess()) 
                mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });

    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mHelper.launchPurchaseFlow(Payit.this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener);
        }
    });

}

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            //Log.v("this", "Failed to query inventory: " + result);
            tv.setText(DariGlyphUtils.reshapeText(Z_Farsi.Convert(getString(R.string.problempaying))));
            return;
        }else {
            mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
            Log.v("this", "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
            b.setEnabled(true);

            Purchase gasPurchase = inventory.getPurchase(SKU_PREMIUM);
            if (gasPurchase != null ) 
             mHelper.consumeAsync(inventory.getPurchase(SKU_PREMIUM), mConsumeFinishedListener);
        }
        //Log.v("this", "Initial inventory query finished; enabling main UI.");
    }
};

public void consumeItem() {
    mHelper.queryInventoryAsync(mGotInventoryListener);
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.v("this", "Error purchasing: " + result);
            return;
        } else if (purchase.getSku().equals(SKU_PREMIUM)) {
             consumeItem();              
        }

        if (result.isSuccess()) {// Successful - the item has been payed for
            // The flag "CLEAR_TASK" is important, so the user is not sent
            // back to this buy activity when he presses the back button.
            //Toast.makeText(getBaseContext(), "thanks for your payment",Toast.LENGTH_SHORT).show();
            /*Intent intentz = new Intent(Payit.this,page_full.class);
            intentz.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET| Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intentz);*/
        }
    }
};

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

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        // We know this is the "gas" sku because it's the only one we consume,
        // so we don't check which sku was consumed. If you have more than one
        // sku, you probably should check...
        if (result.isSuccess()) {
            // successfully consumed, so we apply the effects of the item in our
            // game world's logic, which in our case means filling the gas tank a bit
            Log.v("this", "Consumption successful. Provisioning.");
            //alert("You filled 1/4 tank. Your tank is now 4 full!");
        }
        else {
            Log.v("this","Error while consuming: " + result);
        }

        Log.v("this", "End consumption flow.");
    }
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.v("this", "onActivityResult(" + requestCode + "," + resultCode + "," + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    } else {
        Log.v("this", "onActivityResult handled by IABUtil.");
    }
}

请告知,SKU_PREMIUM是我的消耗品。 这是错误:

Consumption finished. Purchase: PurchaseInfo(type:subs):{"orderId": "-vmhoD_kwxMs6Ksb", "purchaseToken": "-vmhoD_kwxMs6Ksb", "developerPayload": "", "packageName": "com.example.package", "purchaseState": 0, "purchaseTime": 1413292833534, "productId": "goldvip1"}, result: IabResult: Items of type 'subs' can't be consumed. (response: -1010:Invalid consumption attempt)

我认为这是重要的部分: 结果:IabResult:类型' subs'无法消费。

为什么我收到此错误?你可以帮帮我吗 ?

谢谢

1 个答案:

答案 0 :(得分:0)

因为您没有覆盖下面的功能:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
      if (!mHelper.handleActivityResult(requestCode, 
              resultCode, data)) {     
        super.onActivityResult(requestCode, resultCode, data);
      }
}

然后这是您的BuyClick功能

   public void OnBuyClickButtonClicking(View arg0)
    {
        mHelper.launchPurchaseFlow(this, ITEM_SKU, 50002, mPurchaseFinishedListener, "mypurchasetoken");
    }

这是购买完成的听众:

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {

    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

       if (result.isFailure()) {
          // Handle error
          return;
       }      
       else if (purchase.getSku().equals(ITEM_SKU)) {
          mHelper.consumeAsync(purchase, mConsumeFinishedListener);

       }

    }
};

然后这是你的消费完成听众:

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

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

         if (result.isSuccess()) {      


         } else {
             // handle error
        }

    }
};