簡體   English   中英

設置v3應用內結算

[英]Setting up v3 of in-app billing

我在市場上有一個應用程序。 我的下一個版本想實現應用內購買。 我一直遵循( v設置應用內結算v3的指南。 我完成了該鏈接的前3課,但是現在我的代碼中有很多錯誤,所有錯誤都在同一個變量上。

在下面的代碼中, spreadsheet是我在開發人員控制台中購買的產品的PRODUCT_ID。 每次使用可變spreadsheet ,錯誤為:“電子表格無法解析為變量。”

    public class BibleStudy extends SwarmActivity {
        Context c;
        IabHelper mHelper;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.biblestudy);

            c = this;

        String base64EncodedPublicKey = a + b + d + e + f + g + h + i + j + k;

        // Create the helper, passing it our context and the public key to verify signatures with
        mHelper = new IabHelper(this, base64EncodedPublicKey);

        // Start setup. This is asynchronous and the specified listener will be called once setup completes.
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {

                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    complain("Problem setting up in-app billing: " + result);
                    return;
                }

                // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        });
    }

    // Listener that's called when we finish querying the items and subscriptions we own.
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
            if (result.isFailure()) {
                complain("Failed to query inventory: " + result);
                return;
            }

            /*
             * Check for items we own. Notice that for each purchase, we check
             * the developer payload to see if it's correct! See
             * verifyDeveloperPayload().
             */

            // Do we have the premium upgrade?
            Purchase spreadsheetPurchase = inventory.getPurchase(spreadsheet);

            updateUi();
        }
    };

    // User clicked the "Download Spreadsheet!" button.
    public void onBuyButtonClick(View v) {

        // launch the gas purchase UI flow.
        // We will be notified of completion via mPurchaseFinishedListener

        /* TODO: for security, generate your payload here for verification. See the comments on 
         *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use 
         *        an empty string, but on a production app you should carefully generate this. */
        String payload = ""; 

        mHelper.launchPurchaseFlow(this, spreadsheet, 10001, mPurchaseFinishedListener, payload);
    }

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

        // Pass on the activity result to the helper for handling
        if(!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            // not handled, so handle it ourselves (here's where you'd
            // perform any handling of activity results not related to in-app
            // billing...
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /** Verifies the developer payload of a purchase. */
    boolean verifyDeveloperPayload(Purchase p) {
        String payload = p.getDeveloperPayload();

        /*
         * TODO: verify that the developer payload of the purchase is correct. It will be
         * the same one that you sent when initiating the purchase.
         * 
         * WARNING: Locally generating a random string when starting a purchase and 
         * verifying it here might seem like a good approach, but this will fail in the 
         * case where the user purchases an item on one device and then uses your app on 
         * a different device, because on the other device you will not have access to the
         * random string you originally generated.
         *
         * So a good developer payload has these characteristics:
         * 
         * 1. If two different users purchase an item, the payload is different between them,
         *    so that one user's purchase can't be replayed to another user.
         * 
         * 2. The payload must be such that you can verify it even when the app wasn't the
         *    one who initiated the purchase flow (so that items purchased by the user on 
         *    one device work on other devices owned by the user).
         * 
         * Using your own server to store and verify developer payloads across app
         * installations is recommended.
         */

        return true;
    }

    // Callback for when a purchase is finished
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if(result.isFailure()) {
                complain("Error purchasing: " + result);
                return;
            }

            if(!verifyDeveloperPayload(purchase)) {
                complain("Error purchasing. Authenticity verification failed.");
                return;
            }

            else if(purchase.getSku().equals(spreadsheet)) {
                // bought the premium upgrade!
                alert("Thank you for upgrading to premium!");
//                mIsPremium = true;
                updateUi();
            }
        }
    };

    // updates UI to reflect model
    public void updateUi() {

        // MATT: update a TextView to show link after purchase is done.

//        // update the car color to reflect premium status or lack thereof
//        ((ImageView)findViewById(R.id.free_or_premium)).setImageResource(mIsPremium ? R.drawable.premium : R.drawable.free);
//
//        // "Upgrade" button is only visible if the user is not premium
//        findViewById(R.id.upgrade_button).setVisibility(mIsPremium ? View.GONE : View.VISIBLE);
//
//        // "Get infinite gas" button is only visible if the user is not subscribed yet
//        findViewById(R.id.infinite_gas_button).setVisibility(mSubscribedToInfiniteGas ? 
//                View.GONE : View.VISIBLE);
//
//        // update gas gauge to reflect tank status
//        if (mSubscribedToInfiniteGas) {
//            ((ImageView)findViewById(R.id.gas_gauge)).setImageResource(R.drawable.gas_inf);
//        }
//        else {
//            int index = mTank >= TANK_RES_IDS.length ? TANK_RES_IDS.length - 1 : mTank;
//            ((ImageView)findViewById(R.id.gas_gauge)).setImageResource(TANK_RES_IDS[index]);
//        }        
    }

    void complain(String message) {
        alert("Error: " + message);
    }

    void alert(String message) {
        AlertDialog.Builder bld = new AlertDialog.Builder(this);
        bld.setMessage(message);
        bld.setNeutralButton("OK", null);
        bld.create().show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mHelper != null) mHelper.dispose();
        mHelper = null;
    }
}

購買成功完成后,我想為用戶提供一個鏈接。 因此,我想我需要的是可以驗證購買是否完成的東西,然后可以顯示以前不可見的TextView,為他們提供鏈接。

我的問題是本教程中我做錯了什么?如何消除使用可變spreadsheet每個人身上出現的錯誤?

弄清楚了。 我必須聲明spreadsheet PRODUCT_ID。 我以為是某種方式將其從服務器中拉出。

我集成來解決問題的代碼:

static final String SKU_SPREADSHEET = "spreadsheet";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM