3.2.0
该门户网站正在开发中。文档的完整版本请看这里.
- Kotlin
- Java
您可以查看快速集成应用程序支付的相关部分。
实现示例
查看示例应用程序,了解如何正确集成支付。
支付条件
为了进行支付,必须遵守所有条件:
- 用户的设备上安装了RuStore应用程序。
- 用户在RuStore应用程序中已授权。
- 用户和应用程序不应在RuStore中被封锁。
- 在RuStore 控制台系统中为应用程序启用了购买功能。
服务在俄罗斯联邦以外有一些运作限制。
添加存储库
连接存储库:
repositories {
maven {
url = uri("https://artifactory-external.vkpartner.ru/artifactory/maven")
}
}
集成依赖项
在您的配置文件中添加以下代码以连接依赖项:
dependencies {
implementation("ru.rustore.sdk:billingclient:3.2.0")
}
初始化
在调用库的方法之前,需要先进行其初始化。
使用 RuStoreBillingClientFactory.create() 创建 RuStoreBillingClient:
创建 RuStoreBillingClient
val billingClient: RuStoreBillingClient = RuStoreBillingClientFactory.create(
context = app,
consoleApplicationId = "111111",
deeplinkScheme = "yourappscheme",
// Опциональные параметры
themeProvider = null,
debugLogs = false,
externalPaymentLoggerFactory = null,
)
context
- Android 的上下文。可以是任何上下文,在实现中使用 applicationContext。consoleApplicationId
- 来自RuStore开发者控制台的应用程序代码(例如:https://console.rustore.ru/apps/111111)。deeplinkScheme
- 用于通过第三方应用程序支付后返回您的应用程序的deeplink方案 (例如通过 SberPay 或快速支付系统(SBP))。SDK 会为这个方案生成它自己的宿主。themeProvider
- 提供 BillingClientTheme 主题的接口。有2种 BillingClientTheme 主题实现:亮色(Light)和暗色(Dark)。这个接口是可选的,默认情况下使用亮色主题。externalPaymentLoggerFactory
- 提供对外部日志器访问的接口。事件日志记录debugLogs
- 控制日志记录的标志(Release 构建将自动关闭日志)。事件日志记录
**重要提示。**在 build.gradle 中指定的 ApplicationId 必须与您在 applicationIdRuStore 控制台[系统中发布的] [apk] 文件的 匹配。
传递给 deeplinkScheme 的 deeplink 方案必须与 AndroidManifest.xml 中[处理] deeplink部分指定的方案匹配。
keystore 的签名必须与发布在 RuStore 控制台系统中的应用程序签名匹配。确保使用的 buildType(debug)使用与发布的应用程序相同的签名。
处理deeplink
为了通过第三方应用程序(如快速支付系统(SBP)或 SberPay)正确进行支付,您需要正确实现 deeplink 的处理。为此,您需要在 AndroidManifest.xml 中指定带有您项目 scheme 的 intent-filter:
AndroidManifest.xml
<activity
android:name=".YourBillingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="yourappscheme" />
</intent-filter>
</activity>
中"yourappscheme" - 是您的 deeplink 方案,可以更改为其他方案。
这个方案必须与传递给 init() 的 deeplinkScheme 参数匹配。
接下来,在需要在支付后返回的 Activity(您的商店页面)中,您需要添加:
class YourBillingActivity: AppCompatActivity() {
// Previously created with RuStoreBillingClientFactory.create()
private val billingClient: RuStoreBillingClient = YourDependencyInjection.getBillingClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
billingClient.onNewIntent(intent)
}
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
billingClient.onNewIntent(intent)
}
}
了在通过 deeplink 返回时恢复您的应用程序状态,您需要在 AndroidManifest.xml 中添加:
android:launchMode="singleTask"
AndroidManifest.xml
<activity
android:name=".YourBillingActivity"
android:launchMode="singleTask"
android:exported="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
检查支付功能的可用性
要检查支付功能的可用性,请调用 checkPurchasesAvailability 方法。在调用时,将检查以下条件:
- 用户的设备上必须安装 RuStore。
- RuStore 必须支持支付功能。
- 用户必须在 RuStore 中获得授权。
- 用户和应用程序不应在RuStore中被封锁。
- 应用程序必须在 RuStore 控制台系统中启用购买功能。
果所有条件都满足,则返回 FeatureAvailabilityResult.Available。否则,返回 FeatureAvailabilityResult.Unavailable(val cause: RuStoreException),其中 cause - 是未满足条件的错误。所有可能的 RuStoreException 错误都被描述在错误处理部分。其他错误(例如,"没有互联网连接")将在 onFailure
中返回。
val billingClient: RuStoreBillingClient = RuStoreBillingClientFactory.create(
context = app,
consoleApplicationId = "111111",
deeplinkScheme = "yourappscheme",
themeProvider = null,
debugLogs = false,
externalPaymentLoggerFactory = null,
)
中 context - 是 Android 的上下文。
获取产品列表的最新信息
关键词:RuStore 开发者,文档RuStoreSDK,连接支付,产品列表,产品。 要获取产品列表,请使用 getProducts 方法:
val productsUseCase: ProductsUseCase = billingClient.products
productsUseCase.getProducts(productIds = listOf("id1", "id2"))
.addOnSuccessListener { products: List<Product> ->
// Process success
}
.addOnFailureListener { throwable: Throwable ->
// Process error
}
productIds: list - 产品标识符列表。列表中的最大长度为
- 2083个字符。
此方法返回:
data class Product(
val productId: String,
val productType: ProductType?,
val productStatus: ProductStatus,
val priceLabel: String?,
val price: Int?,
val currency: String?,
val language: String?,
val title: String?,
val description: String?,
val imageUrl: Uri?,
val promoImageUrl: Uri?,
val subscription: ProductSubscription?,
)
productId
- 产品标识符;productType
- 产品类型;productStatus
- 产品状态;priceLable
- 格式化的产品价格,包括[language]语言的货币符号;price
- 以最小单位(戈比)表示的价格;currency
- ISO 4217货币代码;language
- 使用 BCP 47 编码指定的语言;title
- 产品名称,以[language]语言表示;description
- 产品描述,以[language]语言表示;imageUrl
- 图片链接;promoImageUrl
- 促销图片链接;subscription
- 订阅描述,仅对于类型为 subscription 的产品返回。
务器对产品请求的响应结构:
产品结构
data class ProductSubscription(
val subscriptionPeriod: SubscriptionPeriod?,
val freeTrialPeriod: SubscriptionPeriod?,
val gracePeriod: SubscriptionPeriod?,
val introductoryPrice: String?,
val introductoryPriceAmount: String?,
val introductoryPricePeriod: SubscriptionPeriod?
)
meta
- 请求的附加信息;code
- 响应代码;errorMessage
- 用户的错误消息;errorDescription
- 错误消息的详细解释;errors
- 请求产品的错误列表;products
- 请求的产品列表。
订阅结构:
data class ProductSubscription(
val subscriptionPeriod: SubscriptionPeriod?,
val freeTrialPeriod: SubscriptionPeriod?,
val gracePeriod: SubscriptionPeriod?,
val introductoryPrice: String?,
val introductoryPriceAmount: String?,
val introductoryPricePeriod: SubscriptionPeriod?
)
subscriptionPeriod
- 订阅周期;freeTrialPeriod
- 订阅试用期;gracePeriod
- 订阅宽限期;introductoryPrice
- 格式化的介绍性订阅价格,包括product:language语言的货币符号;introductoryPriceAmount
- 以货币最小单位(戈比)表示的介绍性价格;introductoryPricePeriod
- 介绍性价格计算周期。
订阅周期结构:
data class SubscriptionPeriod(
val years: Int,
val months: Int,
val days: Int,
)
years
- 年数;months
- 月数;days
- 天数。