skip to Main Content

The function is working fine for ios but for android it gives error on my physical device. Screen shot is attached . Anyone who is experiencing the same problem?(https://i.sstatic.net/TMQN8BpJ.jpg)

const handleBuySubscription = async (productId, planType) => {
    setIsRequestInProgress(true);
    try {
      const offer = subscriptionOffers[productId]?.[planType];
      console.log("Selected Product ID:", productId);
      console.log("Selected Plan Type:", planType);
      console.log("Offer:", offer);
      if (Platform.OS === 'android' && offer && offer.basePlanId && offer.offerId) {
        try {
          await requestSubscription({
            sku: productId,
            subscriptionOffers: [{ basePlanId: offer.basePlanId, offerId: offer.offerId }],
          });
        } catch (error) {
          console.error("Error during requestSubscription:", error);
        }
      } else if (Platform.OS === 'ios') {
        await requestSubscription({ sku: productId });
      } else {
        throw new Error("Invalid subscription offer details.");
      }

      setLoading(false);
      setIsRequestInProgress(false);
    } catch (error) {
      setLoading(false);
      setIsRequestInProgress(false);
      if (error instanceof PurchaseError) {
        console.error(`[${error.code}]: ${error.message}`, error);
      } else {
        console.error("handleBuySubscription", error);
      }
    }
  };

Tried another method requestPurchase but it didnt work.

2

Answers


  1. I’m not able to make comment (but take this as a remark rather than solution)

    The issue seems of null Pointer
    study the stacktrace and see if you can catch the problem, or post it in the question itself

    Login or Signup to reply.
  2. when you’re trying to call a method on a null object.the requestSubscription function is causing the issue. i just modify your code with some additional logging and null checks. i hope this helps you to figure out

    const handleBuySubscription = async (productId, planType) => {
      setIsRequestInProgress(true);
      try {
        const offer = subscriptionOffers[productId]?.[planType];
        console.log("Selected Product ID:", productId);
        console.log("Selected Plan Type:", planType);
        console.log("Offer:", offer);
    
        if (!offer) {
          console.error("Offer is null");
          throw new Error("Invalid subscription offer details.");
        }
    
        if (Platform.OS === 'android') {
          if (!offer.basePlanId || !offer.offerId) {
            console.error("Offer details are incomplete");
            throw new Error("Invalid subscription offer details.");
          }
    
          try {
            await requestSubscription({
              sku: productId,
              subscriptionOffers: [{ basePlanId: offer.basePlanId, offerId: offer.offerId }],
            });
          } catch (error) {
            console.error("Error during requestSubscription:", error);
          }
        } else if (Platform.OS === 'ios') {
          await requestSubscription({ sku: productId });
        } else {
          throw new Error("Invalid subscription offer details.");
        }
    
        setLoading(false);
        setIsRequestInProgress(false);
      } catch (error) {
        setLoading(false);
        setIsRequestInProgress(false);
        if (error instanceof PurchaseError) {
          console.error(`[${error.code}]: ${error.message}`, error);
        } else {
          console.error("handleBuySubscription", error);
        }
      }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search