Purchasely
Rechercher…
Anonymous user

Why anonymous ?

If your app can be used without requiring the user to register (like news app) and you want to offer purchases to unlock content, Apple will refuse that you force the user to register to subscribe.
In that case you will have to handle anonymous purchases (aka unregistered user purchases).
Purchasely helps you combine the power of Server to Server notifications (S2S) with anonymous purchases.
Consumable and non-consumables won't be transfered from an anonymous user to a authentified user

Handling anonymous purchasing

To handle anonymous users, Purchasely SDK automatically generates an anonymous_user_id for the user. This anonymous_id remains consistent as long as the user keeps the app on its device.
Anonymous Purchasing - General Process
  1. 1.
    The anonymous user purchase a product → Purchasely SDK generates ananonymous_user_id
  2. 2.
    Once the store receipt validated, an event carrying this anonymous_user_id is sent on the webhook
  3. 3.
    The association between the anonymous_user_idand the Product & Plan purchased must be stored in the developer database.
  4. 4.
    The app can use a dedicated method of the SDK to get the anonymous_user_idfrom the SDK and check with the database that the user can access the content / service.

1. Proceeding with an anonymous Purchase

If no UserIdhas been attached to the user by the app to Purchasely SDK, the user will be treated as an anonymous user.
When an anonymous user makes an In-App Purchase, the platform will automatically associate it to the anonymous_user_idfor the user. This anonymous_user_idis tied to this particular device and remains consistent as long as the app remains installed on the device and will be the key for your backend to authorize the purchase on the device.

2. Getting the user anonymous_id from the webhook

When an event occurs on a subscription made by an anonymous user, the webhook message carries the user anonymous_user_idgenerated by Purchasely.
1
{
2
"api_version": 3,
3
"content_id": "<content id you provided through the sdk>",
4
"environment": "SANDBOX",
5
"event_created_at": "2021-11-22T09:23:38.559Z",
6
"event_created_at_ms": 1637573018559,
7
"event_name": "ACTIVATE",
8
"is_family_shared": false,
9
"offer_type": "NONE",
10
"original_purchased_at": "2021-11-22T09:23:36.000Z",
11
"original_purchased_at_ms": 1637573016000,
12
"plan": "<plan vendorID defined in the Purchasely console>",
13
"product": "<product vendorID define in the Purchasely console>",
14
"purchased_at": "2021-11-22T09:23:36.000Z",
15
"purchased_at_ms": 1637573016000,
16
"purchasely_one_time_purchase_id": "otp_XXXXXXXFFFFFFFFF",
17
"store": "APPLE_APP_STORE",
18
"store_app_bundle_id": "<app bundle id defined in the store console>",
19
"store_country": "US",
20
"store_original_transaction_id": "100000099999999",
21
"store_product_id": "<store product id defined in the store console>",
22
"store_transaction_id": "100000099999999",
23
"anonymous_user_id": "<anonymous user id generated by Purchasely>"
24
}
Copied!

3. Attaching the purchase to the anonymous user

Purchases and corresponding entitlements must be attached to the anonymous user and stored in your database, using the anonymous_user_id.
Every subsequent purchase or event for this particular user will carry the same anonymous_user_id. In other words, when a particular event (renewal / cancellation / expiration etc...) happens on an anonymous user subscription, it will carry the same anonymous_user_id.

4. Getting user entitlements & anonymous_id in the app

Entitlements are managed by the developer backend and directly attached to the anonymous_user_id. The app can retrieve the anonymous_user_id from the SDK vy using the following method :
Swift
Objective-C
Kotlin
Java
React Native
Cordova
Flutter
1
Purchasely.anonymousUserId
Copied!
1
[Purchasely anonymousUserId];
Copied!
1
Purchasely.anonymousUserId
Copied!
1
Purchasely.getAnonymousUserId();
Copied!
1
Purchasely.getAnonymousUserId();
Copied!
1
Purchasely.getAnonymousUserId((anonymousId) => {
2
console.log("Purchasely anonymous Id: " + anonymousId);
3
});
Copied!
1
Purchasely.anonymousUserId;
Copied!
To ease customer support, we advise you to display both anonymous_user_id and user_id inside your app (e.g. in the section "My Subscriptions", in the settings or in the contact-us mail). This will help you identify the user in the Purchasely Console or in your own logs.
If you don't want to manage anonymous users you could use our mobile API to check subscription status and unlock the content locally.

Authentication of an anonymous user

To authenticate an anonymous user, just set the appUserId. If your user had a subscription we will transfer it to the connected user. That implies sending events to your backend and wait for it to confirm before you try to refresh your entitlements. This is why you have a closure that passes a boolean telling you if the entitlements of the logged in user should be refreshed.
Swift
Objective-C
Kotlin
Java
React Native
Cordova
Flutter
1
Purchasely.userLogin(with: "123456789") { (shouldRefreshCredentials) in
2
if (shouldRefreshCredentials) {
3
// You should call your backend to refresh user entitlements
4
}
5
}
Copied!
1
[Purchasely userLoginWith:@"123456789" shouldRefresh:^(BOOL shouldRefreshCredentials) {
2
if (shouldRefreshCredentials) {
3
// You should call your backend to refresh user entitlements
4
}
5
}];
Copied!
1
Purchasely.userLogin("123456789") { refresh ->
2
if (refresh) {
3
// You should call your backend to refresh user entitlements
4
}
5
}
Copied!
1
Purchasely.userLogin("123456789", refresh -> {
2
if(refresh) {
3
// You should call your backend to refresh user entitlements
4
}
5
return null;
6
});
Copied!
1
Purchasely.userLogin('123456789').then((refresh) => {
2
if (refresh) {
3
// You should call your backend to refresh user entitlements
4
}
5
});
Copied!
1
Purchasely.userLogin("123456789", (shouldRefresh) => {
2
if (shouldRefresh) {
3
// You should call your backend to refresh user entitlements
4
}
5
});
Copied!
1
Purchasely.userLogin('123456789').then((refresh) => {
2
if (refresh) {
3
//call your backend to refresh user information
4
}
5
});
Copied!
Do not forget to call Purchasely.userLogin("YOUR_USER_ID")before returning the result to Purchasely to properly update the presentation without the sign-in button.
Once the user has been connected all the purchased subscriptions are transferred from the anonymous user, to the connected user. User entitlements shall thus be refreshed by the App.
The Webhook will receive 2 Transactional events, a ACTIVATE for the connected user and a DEACTIVATE for the anonymous user. The webhook and integratiosn you activated will also receive 2 events, a SUBSCRIPTION_RECEIVEDfor the connected user and a SUBSCRIPTION_TRANSFERREDfor the anonymous user.
1
{
2
"plan": "<plan vendorID defined in the Purchasely console>",
3
"store": "APPLE_APP_STORE",
4
"product": "<product vendorID define in the Purchasely console>",
5
"anonymous_user_id": "<anonymous user id originally generated by Purchasely>",
6
"event_name": "DEACTIVATE",
7
"offer_type": "NONE",
8
"api_version": 3,
9
"environment": "SANDBOX",
10
"purchased_at": "2021-11-07T17:44:17.000Z",
11
"store_country": "FR",
12
"next_renewal_at": "2021-11-07T17:47:17.000Z",
13
"purchased_at_ms": 1636307057000,
14
"event_created_at": "2021-11-07T17:43:35.225Z",
15
"is_family_shared": false,
16
"store_product_id": "<store product id defined in the store console>",
17
"next_renewal_at_ms": 1636307237000,
18
"event_created_at_ms": 1636307015225,
19
"previous_offer_type": "NONE",
20
"store_app_bundle_id": "<app bundle id defined in the store console>",
21
"subscription_status": "AUTO_RENEWING",
22
"store_transaction_id": "100000099999999",
23
"original_purchased_at": "2021-11-07T17:41:18.000Z",
24
"original_purchased_at_ms": 1636306878000,
25
"effective_next_renewal_at": "2021-11-07T17:47:17.000Z",
26
"purchasely_subscription_id": "subs_XFJFJEBFFU757FUJH",
27
"effective_next_renewal_at_ms": 1636307237000,
28
"store_original_transaction_id": "10000009999999"
29
}
Copied!
1
{
2
"plan": "<plan vendorID defined in the Purchasely console>",
3
"store": "APPLE_APP_STORE",
4
"product": "<product vendorID define in the Purchasely console>",
5
"user_id": "<user you provided through the SDK when the user logged in>",
6
"event_name": "ACTIVATE",
7
"offer_type": "NONE",
8
"api_version": 3,
9
"environment": "SANDBOX",
10
"purchased_at": "2021-11-07T17:44:17.000Z",
11
"store_country": "FR",
12
"next_renewal_at": "2021-11-07T17:47:17.000Z",
13
"purchased_at_ms": 1636307057000,
14
"event_created_at": "2021-11-07T17:43:35.225Z",
15
"is_family_shared": false,
16
"store_product_id": "<store product id defined in the store console>",
17
"next_renewal_at_ms": 1636307237000,
18
"event_created_at_ms": 1636307015225,
19
"previous_offer_type": "NONE",
20
"store_app_bundle_id": "<app bundle id defined in the store console>",
21
"subscription_status": "AUTO_RENEWING",
22
"store_transaction_id": "100000099999999",
23
"original_purchased_at": "2021-11-07T17:41:18.000Z",
24
"original_purchased_at_ms": 1636306878000,
25
"effective_next_renewal_at": "2021-11-07T17:47:17.000Z",
26
"purchasely_subscription_id": "subs_XFJFJEBFFU757FUJH",
27
"effective_next_renewal_at_ms": 1636307237000,
28
"store_original_transaction_id": "10000009999999"
29
}
Copied!

Intercept paywall actions

Every presentation, has a Already subscribed? Sign-in button to let your customers connect to unlock a feature / access a content. This button is displayed if you did not set a user id with Purchasely.userLogin() see SDK configuration for more information.
To intercept this event, you can setup a global handler that passes you the source paywall controller / fragment above which to display login.
  • IF the user signs in AND has a subscription, you can dismiss the paywall controller
  • IF the user signs in AND doesn't have a subscription, dismiss your login controller and notify the SDK by calling the isLoggedIn closure with true
  • IF the user cancels sign-in, dismiss your login controller and notify the SDK by calling the isLoggedIn closure with false
Swift
Objective-C
Kotlin
Java
React Native
Cordova
Flutter
1
Purchasely.setPaywallActionsInterceptor { [weak self] (action, parameters, presentationInfo, proceed) in
2
3
switch action {
4
5
// Intercept the tap on login
6
case .login:
7
// When the user has completed the process
8
// Pass true to reload the paywall or dismiss the paywall if the user already has an active subscription
9
self?.presentLogin(above: presentationInfo?.controller) { (loggedIn) in
10
proceed(loggedIn)
11
}
12
default:
13
proceed(true)
14
break
15
}
16
}
Copied!
1
[Purchasely setPaywallActionsInterceptor:^(enum PLYPresentationAction action, PLYPresentationActionParameters *parameters, PLYPresentationInfo *info, void (^ proceed)(BOOL)) {
2
switch (action) {
3
// Intercept the tap on purchase to display the terms and condition
4
case PLYPresentationActionPurchase:{
5
[self presentTermsAndConditionsAbove:info.controller completion:^(BOOL userAcceptedTerms) {
6
proceed(userAcceptedTerms);
7
}];
8
break;
9
}
10
default:
11
proceed(YES);
12
break;
13
}
14
}];
Copied!
1
Purchasely.setPaywallActionsInterceptor { info, action, parameters, processAction ->
2
if (info?.activity == null) return@setPaywallActionsInterceptor
3
4
when(action) {
5
PLYPresentationAction.LOGIN -> {
6
// Call your method to display your view
7
// and return boolean result to userLoggedIn
8
presentLogin(info.activity) { userLoggedIn ->
9
// Don't forget to notify the SDK by calling `processAction`
10
processAction(userLoggedIn)
11
}
12
}
13
else -> {
14
Log.d("PLYActionInterceptor", action.value + " " + parameters)
15
processAction(true)
16
}
17
}
18
}
Copied!
1
Purchasely.setPaywallActionsInterceptor((info, action, parameters, listener) -> {
2
//if there is no activity then there is nothing to display
3
if (info == null || info.getActivity() == null) return;
4
5
switch (action) {
6
case LOGIN:
7
// call your method to display your view
8
// and return boolean result to listener
9
// listener.processAction(true);
10
presentLogin(info.getActivity(), listener);
11
break;
12
default:
13
listener.processAction(true);
14
}
15
});
Copied!
1
Purchasely.setPaywallActionInterceptorCallback((result) => {
2
if (result.action === PLYPaywallAction.LOGIN) {
3
console.log('User wants to login');
4
//Present your own screen for user to log in
5
Purchasely.closePaywall();
6
Purchasely.userLogin('MY_USER_ID');
7
//Call this method to update Purchasely Paywall
8
Purchasely.onProcessAction(true);
9
} else {
10
Purchasely.onProcessAction(true);
11
}
12
});
Copied!
1
Purchasely.setPaywallActionInterceptorCallback((result) => {
2
if (result.action === PLYPaywallAction.LOGIN) {
3
console.log('User wants to login');
4
//Present your own screen for user to log in
5
Purchasely.closePaywall();
6
Purchasely.userLogin('MY_USER_ID');
7
//Call this method to update Purchasely Paywall
8
Purchasely.onProcessAction(true);
9
} else {
10
Purchasely.onProcessAction(true);
11
}
12
});
Copied!
1
Purchasely.setPaywallActionInterceptorCallback(
2
(PaywallActionInterceptorResult result) {
3
if (result.action == PLYPaywallAction.login) {
4
print('User wants to login');
5
//Present your own screen for user to log in
6
Purchasely.closePaywall();
7
Purchasely.userLogin('MY_USER_ID');
8
//Call this method to update Purchasely Paywall
9
Purchasely.onProcessAction(true);
10
} else {
11
Purchasely.onProcessAction(true);
12
}
13
});
Copied!
Do not forget to call Purchasely.userLogin("YOUR_USER_ID")before returning the result to Purchasely to properly update the presentation without the sign-in button.