Skip to main content
Question

PurchaseParams.Builder(this, aPackage) usage

  • 21 September 2023
  • 3 replies
  • 271 views

Forum|alt.badge.img+2

Hello, I just started adding a subscription to an android app using java for the first time.

Following the getting started guide, I got to the “Making a purchase” section. I want to use the code example from this section in my app but there is one parameter I’m not sure where is coming from.

This is the code:

Purchases.getSharedInstance().purchase(
	new PurchaseParams.Builder(this, aPackage).build(), 
	new PurchaseCallback() {
    @Override
    public void onCompleted(@NonNull StoreTransaction storeTransaction, @NonNull CustomerInfo customerInfo) {
			if (customerInfo.getEntitlements().get(<my_entitlement_identifier>).isActive()) {
				// Unlock that great "pro" content
			}
		}

		@Override
		public void onError(@NonNull PurchasesError purchasesError, boolean b) {
			// No purchase
		}
	}
);

The missing parameter is aPackage in the second line. What should I put there?

Additional info: The app has only one product which is a subscription, it is connected to an entitlement, and there are no offerings.

Thanks.

3 replies

Forum|alt.badge.img+8
  • RevenueCat Staff
  • 418 replies
  • October 3, 2023

Forum|alt.badge.img+2

Hey everyone, 

 

I’m in test mode, and I’m trying to just test if a purchase will work. 

I’m stuck with the error “Error: Must provide aPackage parameter”. 

I’m using capacitor, using iOS simulator. Here’s the code I use to test a purchase:

const makePurchase = async () => {
  try {
    const purchaseResult = await Purchases.purchasePackage({
      aPackage: "$rc_monthly",
    });
    if (
      typeof purchaseResult.customerInfo.entitlements.active[
        "my_entitlement_identifier"
      ] !== "undefined"
    ) {
      // Unlock that great "pro" content
      console.log(purchaseResult);
      console.log(customerInfo);
    }
  } catch (error) {
    console.log(error);
    if (error.code === PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR) {
      // Purchase cancelled
    } else {
      // Error making purchase
    }
  }
};

 I suppose the value I pass to aPackage is not right, but what should you pass? An object? I can’t find in the documentation. 

 

Thanks in advance

 

 

 


Forum|alt.badge.img+2

Answering my own question : You need to pass the actual object you retrieve from offerings.current.availablePackages. You can can grab the packages like explained here: 

https://www.revenuecat.com/docs/getting-started/displaying-products

 

Hope that will help someone :) 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings