Skip to main content
Solved

Update customerInfo after successful purchase with Paywall's PayWallView?

  • Jan 15, 2024 7:05 PM UTC
  • 12 replies
  • 1439 views

Tim Mitra
Forum|alt.badge.img+2

Hi, I’m using the PaywallView in my app after the user creates 5 records. When the user tries to add a record greater than 5, the Paywall displays.

However I don’t know how to update my logic if the user successfully purchases a subscription. The Pay wall dismisses after “you’re all set” displays. Is there a callback I can use to update the local state of the subscription? Does .sheet(isPresented:_) have an onEnded or the like where I can call Purchases.shared.customerInfo() ?

I can check the in a task when the view loads… but not sure how to update the state during/after the purchase

Best answer by Tim Mitra

This is my solution:
 

   .sheet(isPresented: $displayPaywall,
onDismiss: {
Task {
do {
let customerInfo = try await Purchases.shared.customerInfo()
isProUser = customerInfo.entitlements["Pro"]?.isActive == true
// update AppStorage
subscribed = isProUser
print("isProUser: \(isProUser)")
} catch {
print(error.localizedDescription)
}
}
}) {
PaywallView(displayCloseButton: true)
}

 

View original
This post has been closed for comments

12 replies

Michael Fogel
Forum|alt.badge.img+6
  • Dedicated Contributor
  • Jan 18, 2024 5:34 PM UTC

Hey @Tim Mitra !

 

You can listen to the customerInfo during the purchase of the paywall by calling the following: 

purchaseCompleted: { customerInfo in
print("Purchase completed: \(customerInfo.entitlements)")
}

 

More information on this can be found in the code block here: https://www.revenuecat.com/docs/displaying-paywalls#ios

 

Hope that helps! 


Tim Mitra
Forum|alt.badge.img+2
  • Author
  • Helper
  • Jan 18, 2024 5:40 PM UTC

I’m doing that but not at the app level. I want my users to be able to perform some actions deeper in the app. When they reach a number of activities, I’m using PaywallView(), but there doesn’t seem to be a reliable way to get notice that the purchase was successful.

 

.task {
do {
let customerInfo = try await Purchases.shared.customerInfo()
isProUser = customerInfo.entitlements["Pro"]?.isActive == true
// uddate AppStorage
subscribed = isProUser
print("isProUser: \(isProUser)")
} catch {
print(error.localizedDescription)
}
}

 


Tim Mitra
Forum|alt.badge.img+2
  • Author
  • Helper
  • Answer
  • Jan 22, 2024 5:11 AM UTC

This is my solution:
 

   .sheet(isPresented: $displayPaywall,
onDismiss: {
Task {
do {
let customerInfo = try await Purchases.shared.customerInfo()
isProUser = customerInfo.entitlements["Pro"]?.isActive == true
// update AppStorage
subscribed = isProUser
print("isProUser: \(isProUser)")
} catch {
print(error.localizedDescription)
}
}
}) {
PaywallView(displayCloseButton: true)
}

 


Chris-29
Forum|alt.badge.img+5
  • Dedicated Member
  • May 11, 2024 12:14 AM UTC

@Tim Mitra I had the EXACT same question. This answer is how I’m doing to do it with PaywallView.


Chris-29
Forum|alt.badge.img+5
  • Dedicated Member
  • May 11, 2024 12:26 AM UTC

If the documentation on this page was updated it would really help.  For the “custom logic” example it shows a purchaseCompleted completion handler you can use.  However, it’s not clear at all how to do that with the “manually” example.  From that other thread I learned you can use .onPurchaseCompleted .


Forum|alt.badge.img
  • New Member
  • Aug 5, 2024 12:31 PM UTC

I’m attempting to use .onPurchaseCompleted but don’t see it called after PaywallView is dismissed.

.sheet(isPresented: self.$displayPaywall) {
PaywallView(displayCloseButton: true)
}
.onPurchaseCompleted { customerInfo in
    print("purchase completed")
    isSubscribed = customerInfo.entitlements["pro"]?.isActive == true
}

 

What is wrong or missing?


Forum|alt.badge.img
  • New Member
  • Aug 27, 2024 1:29 PM UTC

Anyone could help here?  Thanks.


ptorab
Forum|alt.badge.img+1
  • New Member
  • Sep 28, 2024 9:09 PM UTC

I wonder what I’m missing, is this problem specific to using RC’s own Paywall?

In my case after calling purchase() from Paywall (and Paywall view being dismissed), the purchases() function updates customerInfo, and as long as you have customerInfo as an @ObservedObject in your view (e.g. @ObservedObject userInfo: UserInfo = .shared), you can promptly see the updated entitlements. This works fine with limited testing so far, so a bit worried if I’m missing something.

It seems with some proper flow in the app, user can kept in a spot where “doing more” needs a purchase, without that spot being tight in the sense that it has to be unblocked right at purchase completion.

func purchases(_ purchases: Purchases, receivedUpdated customerInfo: CustomerInfo) { UserInfo.shared.customerInfo = customerInfo }

 


Forum|alt.badge.img+3
  • Member
  • Nov 22, 2024 12:21 PM UTC

Similar (same?) issue here.

  • iOS 18.0/18.1
  • RC 5.10.0
  • Compiled with Xcode 16.1 beta

 

PaywallView(displayCloseButton: false)
.onPurchaseCompleted { customerInfo in
print(customerInfo.entitlements)
self.isPro = !customerInfo.entitlements.active.isEmpty
}


The ‘onPurchaseCompleted’ callback/modifier never gets called.


Michael Fogel
Forum|alt.badge.img+6
  • Dedicated Contributor
  • Nov 22, 2024 9:50 PM UTC

Hey ​@joey_ ,

 

Is the onPurchaseCompleted block never triggering or only in some instances? 


Forum|alt.badge.img+3
  • Member
  • Nov 26, 2024 10:22 AM UTC

Hey Michael, thanks for coming back to me.

I haven't seen it fire yet on device (iPhone 14 Pro) or simulator.

In the meantime, we have added defensive code to check customer info e.g. `onDisappear`.


Forum|alt.badge.img+7
  • Helper
  • Nov 28, 2024 9:00 PM UTC

Hi all, this solution works for us in our live app with all the relevant closures being called.

Hope it helps someone 🙂
 

.sheet(isPresented: $showPaywall) {
PaywallView(displayCloseButton: true)
.onPurchaseStarted { info in

}
.onPurchaseCompleted { info in

}
.onPurchaseCancelled {

}
.onRestoreStarted {

}
.onRestoreCompleted { info in

}
.onPurchaseFailure { error in

}
.onRestoreFailure { error in

}
}