有时我的用户会报告未记录应用内购买(即使它已经过并经过授权)。我使用了TrivialDrive示例并使用以下代码:
public class FragmentGoPro extends Fragment{
private static final String TAG = AppConstants.Keys.TAG;
private final String SKU_PREMIUM = "001";
private IabHelper mHelper;
private static final int RC_REQUEST = 546731;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_billing, container, false);
getActivity().getActionBar().setTitle(getActivity().getString(R.string.app_name));
setupHelper();
TextView descriptionView = (TextView)rootView.findViewById(R.id.pro_description);
String proDescription = getString(R.string.pro_description);
descriptionView.setText(proDescription);
Button goPro = (Button)rootView.findViewById(R.id.purchase);
goPro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makePurchase();
}
});
return rootView;
}
private void setupHelper() {
mHelper = new IabHelper(getActivity(), AppConstants.Keys.PRO);
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);
}
});
}
private void makePurchase() {
Log.i(TAG, "MakePurchase requested.");
mHelper.launchPurchaseFlow(getActivity(), SKU_PREMIUM, RC_REQUEST,
mPurchaseFinishedListener, null);
}
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.i(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
if (result.isFailure()) {
complain("Error purchasing: " + result);
return;
}
Log.i(TAG, "Purchase of " + purchase.getSku() + " Sku confirmed.");
if (purchase.getSku().equals(SKU_PREMIUM)) {
// bought the premium upgrade!
alert("Thank you for upgrading to premium!");
Log.i(TAG, "Premium features unlocked!");
setPreference("mIsPremium", true);
setPreference("Purchased", true);
updateMetrics();
updateNav();
}
}
};
protected 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 premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
boolean mIsPremium = (premiumPurchase != null);
if (mIsPremium) {
Log.i(TAG, "Premium purchased previously, restoring transactions.");
setPreference("mIsPremium", mIsPremium);
setPreference("Purchased", true);
updateNav();
}
}
};
private void updateMetrics() {
EasyTracker.getInstance().setContext(getActivity());
flurryEvent("state", "purchase_state", "purchased");
if (!checkIfTesting()) {
Log.i(TAG, "Purchase Logged");
Transaction myTrans = new Transaction.Builder(
"0_3164", // (String) Transaction Id, should be unique.
(long) 2.99 * 1000000) // (long) Order total (in micros)
.setAffiliation("In-App Google Play") // (String) Affiliation
.build();
myTrans.addItem(new Transaction.Item.Builder(
"001", // (String) Product SKU
"Pro License", // (String) Product name
(long) 2.99 * 1000000, // (long) Product price (in micros)
(long) 1 ) // (long) Product quantity
.build());
Tracker myTracker = EasyTracker.getTracker(); // Get reference to tracker.
myTracker.sendTransaction(myTrans); // Track the transaction.
}
}
protected void updateNav() {
Activity activity = getActivity();
if(activity instanceof MainActivity) {
((MainActivity) activity).initializeNavigation();
} else {
getActivity().finish();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + 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);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
我的updateMetrics()和updateNav()似乎没有被调用,但它看起来像我一直应该这样。同样,这只发生在一小部分用户中。
答案 0 :(得分:0)
我认为这与我的orderID是静态的有关。我按照指南here使用更新的分析。使用System.currentTimeInMillis()应该提供唯一的orderID并防止数据冲突。
另外,我在一个片段中使用它,所以onActivityResult没有被调用(奇怪的是它在我的测试设备上工作)所以我需要修改帐单代码。