Skip to content

Commit 6ea8d94

Browse files
authored
Add RN VC Docs (#1006)
1 parent 803f64d commit 6ea8d94

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

code_blocks/virtual-currency/vc-balance-sdk-android.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void onError(@NonNull PurchasesError error) {
1414
);
1515

1616
// Get the details of a specific virtual currency
17-
VirtualCurrency virtualCurrency = virtualCurrencies.getAll().get("");
17+
VirtualCurrency virtualCurrency = virtualCurrencies.getAll().get(<virtual_currency_code>);
1818
int balance = virtualCurrency.getBalance();
1919
String name = virtualCurrency.getName();
2020
String code = virtualCurrency.getCode();
@@ -23,7 +23,7 @@ public void onError(@NonNull PurchasesError error) {
2323
// in the RevenueCat dashboard
2424
String serverDescription = virtualCurrency.getServerDescription();
2525

26-
// Iterate through all virtual currency balances
26+
// Iterate through all virtual currencies
2727
virtualCurrencies.getAll().forEach((key, virtualCurrency) -> {
2828
System.out.println(virtualCurrency.getCode() + ": " + virtualCurrency.getBalance());
2929
});

code_blocks/virtual-currency/vc-balance-sdk-android.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ val virtualCurrency = virtualCurrencies.all[<your_virtual_currency_code>]
2222
val balance = virtualCurrency?.balance
2323
val name = virtualCurrency?.name
2424
val code = virtualCurrency?.code
25+
26+
// Keep in mind that serverDescription may be null if no description was provided
27+
// in the RevenueCat dashboard
2528
val description = virtualCurrency?.serverDescription
2629

27-
// Iterate through all virtual currency balances
30+
// Iterate through all virtual currencies
2831
for ((virtualCurrencyCode, virtualCurrency) in virtualCurrencies.all) {
2932
println("$virtualCurrencyCode: ${virtualCurrency.balance}")
3033
}

code_blocks/virtual-currency/vc-balance-sdk-ios.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ let virtualCurrencies = try? await Purchases.shared.virtualCurrencies()
44

55
// With Completion Handlers
66
Purchases.shared.virtualCurrencies { virtualCurrencies, error in
7-
7+
// TODO: Handle virtual currencies & error
88
}
99

1010
// Get the details of a specific virtual currency
1111
let virtualCurrency = virtualCurrencies.all[<your_virtual_currency_code>]
1212
let balance = virtualCurrency?.balance
1313
let name = virtualCurrency?.name
1414
let code = virtualCurrency?.code
15+
16+
// Keep in mind that serverDescription may be null if no description was provided
17+
// in the RevenueCat dashboard
1518
let description = virtualCurrency?.serverDescription
1619

17-
// Iterate through all virtual currency balances
20+
// Iterate through all virtual currencies
1821
for(virtualCurrencyCode, virtualCurrencyInfo) in virtualCurrencies.all {
1922
print("\(virtualCurrencyCode): \(virtualCurrencyInfo.balance)")
2023
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Fetch virtual currencies
2+
try {
3+
const virtualCurrencies = await Purchases.getVirtualCurrencies();
4+
// TODO: Handle virtual currencies
5+
} catch (error) {
6+
// TODO: Handle error
7+
}
8+
9+
// Get the details of a specific virtual currency
10+
const virtualCurrency = virtualCurrencies.all['<virtual_currency_code>'];
11+
const balance = virtualCurrency.balance;
12+
const name = virtualCurrency.name;
13+
const code = virtualCurrency.code;
14+
15+
// Keep in mind that serverDescription may be null if no description was provided
16+
// in the RevenueCat dashboard
17+
const serverDescription = virtualCurrency.serverDescription;
18+
19+
// Iterate through all virtual currencies
20+
for (const [virtualCurrencyCode, virtualCurrency] of Object.entries(virtualCurrencies.all)) {
21+
console.log(`${virtualCurrency.code}: ${virtualCurrency.balance}`);
22+
}

docs/offerings/virtual-currency.mdx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ import content5 from "@site/code_blocks/virtual-currency/balance-response.json?r
9595
import fetchVCBalancesSwift from "@site/code_blocks/virtual-currency/vc-balance-sdk-ios.swift?raw";
9696
import fetchVCBalancesKotlin from "@site/code_blocks/virtual-currency/vc-balance-sdk-android.kt?raw";
9797
import fetchVCBalancesJava from "@site/code_blocks/virtual-currency/vc-balance-sdk-android.java?raw";
98+
import fetchVCBalancesReactNative from "@site/code_blocks/virtual-currency/vc-balance-sdk-react-native.ts?raw";
9899

99100
Fetching virtual currency balances is supported in the following SDK versions:
100101

101-
| SDK | Supported Versions |
102-
|-------------|--------------------|
103-
| iOS SDK | 5.32.0+ |
104-
| Android SDK | 9.1.0+ |
102+
| SDK | Supported Versions |
103+
|------------------|--------------------|
104+
| iOS SDK | 5.32.0+ |
105+
| Android SDK | 9.1.0+ |
106+
| React Native SDK | 9.1.0+ |
105107

106108
You can use the `virtualCurrencies()` function to retrieve a customer's balance. The function returns a `VirtualCurrencies` object, which includes the customer's balances along with each virtual currency's metadata.
107109

@@ -122,11 +124,18 @@ You can use the `virtualCurrencies()` function to retrieve a customer's balance.
122124
content: fetchVCBalancesJava,
123125
name: "Java",
124126
},
127+
{
128+
type: "ts",
129+
title: "React Native",
130+
content: fetchVCBalancesReactNative,
131+
},
125132
]}
126133
/>
127134

128135
:::warning VirtualCurrencies does not update automatically when balance changes
129136
When a customer's balance is updated from your backend, the `VirtualCurrencies` object remains cached and is not automatically updated. To get the updated balance, you need to call `Purchases.shared.invalidateVirtualCurrenciesCache()` and fetch the `VirtualCurrencies` object again.
137+
138+
We also recommend calling `invalidateVirtualCurrenciesCache()` after a purchase has completed successfully to ensure that the balances are up to date the next time you fetch them.
130139
:::
131140

132141
You may directly access the cached virtual currencies using the `cachedVirtualCurrencies` property. This is helpful for rendering UI immediately, or for displaying virtual currencies when there is no network connection. Keep in mind that this value is cached and isn't guaranteed to be up to date.

0 commit comments

Comments
 (0)