静态代码中的应用内购买错误

Posted

技术标签:

【中文标题】静态代码中的应用内购买错误【英文标题】:In App Purchase Error in Static codes 【发布时间】:2013-08-10 00:05:26 【问题描述】:

我正在使用当前的购买代码来调用购买意图

Bundle buyIntentBundle = mService.getBuyIntent(3, pContext.getPackageName(), "android.test.canceled", "inapp", "bGoa+V7g/yqDXv");
Set<String> allKeys = buyIntentBundle.keySet();
Object  responseCode= buyIntentBundle.get("RESPONSE_CODE");
Object  intent= buyIntentBundle.get("BUY_INTENT");
Log.i(TAG,"buyIntentBundle"+buyIntentBundle.keySet()+"responseCode"+responseCode+"intent"+intent);
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
if (pendingIntent != null) 
    pContext.startIntentSenderForResult(pendingIntent.getIntentSender(), 2013, new Intent(), Integer.valueOf(0), Integer.valueOf(0),Integer.valueOf(0));

现在我正在检查 onActivityResult

if (requestCode == 2013)            
    Log.i(TAG, "onactivity result called inside request code");
    int responseCode = intent.getIntExtra("RESPONSE_CODE", 0);
    String purchaseData = intent.getStringExtra("INAPP_PURCHASE_DATA");
    String dataSignature = intent.getStringExtra("INAPP_DATA_SIGNATURE");      
    if (responseCode == Constants.BILLING_RESPONSE_RESULT_OK) 
    try 
        JSONObject jo = new JSONObject(purchaseData);
        String sku = jo.getString("productId");
        Toast.makeText(pContext, "You have bought the " + sku + ". Excellent choice, adventurer!", Toast.LENGTH_SHORT);
        JSONObject o = new JSONObject(purchaseData);
        String mOrderId = o.optString("orderId");
        String mPackageName = o.optString("packageName");
        String mSku = o.optString("productId");
        long mPurchaseTime = o.optLong("purchaseTime");
        int mPurchaseState = o.optInt("purchaseState");
        String mDeveloperPayload = o.optString("developerPayload");
        String mToken = o.optString("token", o.optString("purchaseToken"));
        try 
            mService.consumePurchase(3, pContext.getPackageName(), mToken);
         catch (RemoteException e) 
                    e.printStackTrace();
        //consumePurchase
    
    catch (JSONException e) 
                Toast.makeText(pContext, "Failed to parse purchase data.", Toast.LENGTH_SHORT);
                e.printStackTrace();
    
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_USER_CANCELED) 
         Toast.makeText(pContext, "User cancelled purchase.", Toast.LENGTH_SHORT);
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE) 
         Toast.makeText(pContext, "Your Device doesn't support inapp billing.", Toast.LENGTH_SHORT);
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE) 
         Toast.makeText(pContext, "Item is not available for billing.", Toast.LENGTH_SHORT);
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_DEVELOPER_ERROR) 
         Toast.makeText(pContext, "Can't purchase item due to some developer error.", Toast.LENGTH_SHORT);
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_ERROR) 
         Toast.makeText(pContext, "Can't purchase item due to some error in response.", Toast.LENGTH_SHORT);
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) 
         Toast.makeText(pContext, "You already own this item.", Toast.LENGTH_SHORT);
    else if (responseCode == Constants.BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED) 
         Toast.makeText(pContext, "You don't own this item.", Toast.LENGTH_SHORT);
    

因为我正在调用“android.test.canceled”,所以我应该得到 BILLING_RESPONSE_RESULT_USER_CANCELED 这个响应,但它显示为付款成功并返回 responseCode 作为 BILLING_RESPONSE_RESULT_OK,intent.getExtras 中没有其他参数。 我正在尝试静态响应 http://developer.android.com/google/play/billing/billing_testing.html#billing-testing-static

感谢和问候!!

【问题讨论】:

我对同样的行为感到困惑,我认为实际的 Android 开发人员应该修改代码。如果您(或其他任何人)仍需要测试 user_cancelled 响应,只需按退格键。响应代码将为 BILLING_RESPONSE_RESULT_USED_CANCELED。 【参考方案1】:

你没有检查:

if (resultCode == Activity.RESULT_OK) 
    ....

该过程可能未成功完成。由于responseCode 的默认值是“0”......

int responseCode = intent.getIntExtra("RESPONSE_CODE", 0);

.... responseCode 被初始化为 Constants.BILLING_RESPONSE_RESULT_OK == > 0

尝试使用:

int responseCode = intent.getIntExtra("RESPONSE_CODE", -1);  

并添加最后一个 else 块来处理 -1

【讨论】:

使用android.test.canceled时resultCode等于RESULT_OK @MrThys 好的。您是否尝试过使用int responseCode = intent.getIntExtra("RESPONSE_CODE", -1);responseCode 是否仍初始化为 0 Jep,我已将默认值设置为 -1,它仍然返回 0

以上是关于静态代码中的应用内购买错误的主要内容,如果未能解决你的问题,请参考以下文章

在应用描述中添加应用内购买价格表 [关闭]

静态库命名空间内简单函数的编译错误

在全局范围内两次声明相同的静态变量

应用内购买停止工作(错误代码 109)

iOS 11.1.2 中的 Apple 应用内购买错误

如何修复应用内购买错误“交易错误:无法连接到 iTunes Store 代码 0”? [复制]