【问题标题】:Android - how to verify an in-app purchase?Android - 如何验证应用内购买?
【发布时间】:2012-11-12 04:27:13
【问题描述】:

我正在尝试按照本教程http://blog.blundell-apps.com/simple-inapp-billing-payment/了解应用内购买

到目前为止,这是我的代码:

public class main extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("BillingService", "Starting");
        setContentView(R.layout.main);

        startService(new Intent(this, BillingService.class));
        BillingHelper.setCompletedHandler(mTransactionHandler);

    }

    ////////////////////////////////

    public Handler mTransactionHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {

                Log.e("IN APP", "Transaction complete");
                Log.e("IN APP", "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
                Log.e("IN APP", "Item purchased is: "+BillingHelper.latestPurchase.productId);

                if(BillingHelper.latestPurchase.isPurchased())
                {
                    showItem();
                }

            };

    };

    ////////////////////////////////

    public void BuyButtonClick(View v) {

        if(BillingHelper.isBillingSupported()){
            Log.e("IN APP","Trying to buy...");
            BillingHelper.requestPurchase(this, "android.test.purchased"); 
        } else {
            Log.e("IN APP","Can't purchase on this device");
        }

    }

    ////////////////////////////////////////////////

    private void showItem() {

        TextView tv1 = (TextView)findViewById(R.id.tv1);
        tv1.setText("PAID!");

    }

    ////////////////////////////////////////////////

    @Override
    protected void onDestroy() {
        BillingHelper.stopService();
        super.onDestroy();
    }

    ////////////////////////////////

}

一切似乎都很好,但我还想要一些方法来检查应用程序启动时是否购买了该项目。我认为它可能会使用 BillingHelper.verifyPurchase(signedData, signature) ,但是我应该在其中放入哪些数据和签名?或者也许还有其他方法?

谢谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用 restoreTransactions 检查应用程序何时启动。如果您使用过托管产品或订阅,那么只有您才能获得用户的所有详细信息。

    对于非托管产品,谷歌没有维护任何细节。

    所以在你的主要活动中调用它

    mBillingService = new BillingService();
    mBillingService.setContext(this);
    mBillingService.restoreTransactions();
    

    一旦你在 ResponseHandler 类中调用它,就会有一个方法 purchaseResponse

    purchaseResponse(final Context context,
            final PurchaseState purchaseState, final String productId,
            final String orderId, final long purchaseTime,
            final String developerPayload, final String purchaseToken) {
     }
    

    这将返回所有详细信息。

    你可以在之后查看purchaseState

               if (purchaseState == PurchaseState.PURCHASED) {
    
                } else if (purchaseState == PurchaseState.REFUNDED) {
    
                } else if (purchaseState == PurchaseState.CANCELED) {
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2013-01-14
      • 2021-08-05
      • 2022-07-25
      相关资源
      最近更新 更多