欢迎来到 Apto iOS SDK。此 SDK 提供访问 Apto 移动 API 的权限,专为从移动应用程序使用而设计。使用此 SDK 无需集成 API 本身,所有 API 端点都作为易用方法公开,API 返回的数据得到适当的封装并易于访问。
使用此 SDK,您将能够注册新用户、发行卡片、获取卡片活动信息并管理卡片(设置 PIN、冻结/解冻等)。
更多信息,请参阅 Apto 开发者门户。
要求
* Xcode 10
* Swift 5
* CocoaPods
安装(使用 CocoaPods)
-
在您的
Podfile
中添加以下依赖项:platform :ios, '10.0' use_frameworks! pod 'AptoSDK'
-
运行
pod install
。
使用SDK
要运行SDK,您必须首先注册一个项目,以获得API密钥
。请联系Apto创建您项目的一个项目。然后,通过传递公开的API密钥初始化SDK
AptoPlatform.defaultManager().initializeWithApiKey("API KEY")
这将初始化SDK以在生产模式下运行。如果您想以沙箱模式使用它,可以在初始化期间发送一个额外的参数
AptoPlatform.defaultManager().initializeWithApiKey("API KEY",
environment: .sandbox)
用户会话令牌
为了验证用户并检索用户会话令牌,Apto移动API提供了注册或登录的机制。两种机制都是基于验证用户的原始凭证,这可以是用户的电话或电子邮件,具体取决于您项目的配置。请联系我们为您用户设置合适的原始凭证。
获取用户令牌的典型步骤包括
-
验证用户的原始凭证。一旦验证通过,验证将包含显示该凭证是否属于现有用户的或新用户的数据。
-
如果凭证属于现有用户,验证用户的次要凭证。
- 通过SDK上的登录方法获取用户会话令牌。该方法接收前两个验证。用户令牌将被SDK存储和处理。
-
如果凭证不属于任何现有用户,则使用验证的凭证创建新用户并获得用户令牌。用户令牌将被SDK存储和处理。
验证
启动新验证
要启动新验证,您可以使用以下SDK方法
AptoPlatform.defaultManager().startPhoneVerification(phone) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let verification):
// The verification started and the user received an SMS with a single use code.
}
}
AptoPlatform.defaultManager().startEmailVerification(email) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let verification):
// The verification started and the user received an email with a single use code.
}
}
重新启动验证
可以通过以下SDK方法重新启动验证:
AptoPlatform.defaultManager().restartVerification(verification) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let verification):
// The verification started and the user received a new secret via email or phone.
}
}
完成验证
要完成新验证,您可以使用以下SDK方法:
verification.secret = pin // pin entered by the user
AptoPlatform.defaultManager().completeVerification(verification) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let verification):
if verification.status == .passed {
// The verification succeeded. If it belongs to an existing user, it will contain a non null `secondaryCredential`.
}
else {
// The verification failed: the secret is invalid.
}
}
}
用户管理
创建新用户
一旦主要凭证被验证,您可以使用以下SDK方法创建新用户:
// Prepare a DataPointList containing the verified user's primary credential.
let primaryCredential = PhoneNumber(countryCode, phoneNumber)
primaryCredential.verification = verification // The verification obtained before.
let userPII = DataPointList()
userPII.add(primaryCredential)
AptoPlatform.defaultManager().createUser(userData: userPII) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let user):
// The user created. It contains the user id and the user session token.
}
}
使用现有用户登录
一旦主要和次要凭证都被验证,您可以使用以下SDK方法为现有用户获取用户令牌:
AptoPlatform.defaultManager().loginUserWith([phoneVerification, dobVerification]) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let user):
// The user logged in. The user variable contains the user id and the user session token.
}
}
更新用户信息
要更新用户信息,请使用以下SDK方法:
let userPII = DataPointList()
// Add to userPII the datapoints that you want to update
AptoPlatform.defaultManager().updateUserInfo(userPII) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let user):
// Update successful
}
}
关闭用户会话
要关闭当前用户的会话,请使用以下SDK方法:
AptoPlatform.defaultManager().logout()
卡片管理
获取卡片
要通过以下SDK方法检索用户卡片:
AptoPlatform.defaultManager().fetchCards() { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let cards):
// cards contain an array of Card objects
}
}
物理卡片激活
物理卡需要通过为卡持有人发送的激活码进行激活。为了激活物理卡,您可以使用此SDK方法
AptoPlatform.defaultManager().activatePhysicalCard(cardId) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let result):
// Result contains information about the activation process and, if it failed, the reason why it failed.
}
}
更改卡片PIN
为了设置卡片PIN,您可以使用以下SDK方法
AptoPlatform.defaultManager().changeCardPIN(cardId, newPIN) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let card):
// Operation successful
}
}
冻结/解冻卡片
任何时候都可以对卡片进行冻结和解冻。冻结卡片的交易将在商家的POS中被拒绝。要冻结/解冻卡片,您可以使用以下SDK方法
AptoPlatform.defaultManager().lockCard(cardId) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let card):
// Operation successful
}
}
AptoPlatform.defaultManager().unlockCard(cardId) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let card):
// Operation successful
}
}
资金来源管理
Apto卡片可以连接到不同的资金来源。您可以获取当前用户的可用资金来源列表,并将其中一个连接到用户的卡片。
获取可用资金来源列表
AptoPlatform.defaultManager().fetchCardFundingSources(cardId) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let fundingSources):
// fundingSources contain a list of FundingSource objects.
}
}
获取与卡关联的资金来源
AptoPlatform.defaultManager().fetchCardFundingSource(cardId) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let fundingSource):
// fundingSource is a FundingSource object.
}
}
将资金来源连接到卡上
AptoPlatform.defaultManager().setCardFundingSource(cardId, fundingSourceId) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let fundingSource):
// Operation successful
}
}
每月支出统计
要获取特定卡的按类别分类的月度消费信息,可以使用以下SDK方法
AptoPlatform.defaultManager().cardMonthlySpending(cardId, date) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let monthlySpending):
// monthlySpending contains the stats of the given month; spendings classified by transaction category and difference with the previous month.
}
}
date代表支出统计的月份。
通知偏好设置
用户可以通过推送通知接收有关某些事件(交易、卡片状态更改等)的通知。SDK提供了一些功能,允许用户决定他们如何接收这些通知。
获取当前用户通知偏好设置
AptoPlatform.defaultManager().fetchNotificationPreferences() { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let notificationPreferences):
// notificationPreferences contains all the information regarding the current user preferences.
}
}
更新当前用户通知偏好设置
let preferences = NotificationPreferences()
// Set the user preferences in `preferences`
AptoPlatform.defaultManager().updateNotificationPreferences(preferences) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let notificationPreferences):
// Operation successful
}
}
交易管理
要获取交易列表,您可以使用以下SDK方法
AptoPlatform.defaultManager().fetchCardTransactions(cardId, filters, forceRefresh) { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(let error):
// Do something with the error
case .success(let transactions):
// Operation successful
}
}
过滤器参数允许您过滤SDK返回的交易类型。
forceRefresh参数控制SDK是返回本地缓存的交易列表,还是通过Apto移动API请求交易列表。
贡献 & 开发
我们期待收到您的反馈,包括新功能请求、错误修复和文档改进。如果您想帮助我们,请首先查阅仓库中的问题部分;也许其他人有同样的想法,这可能是正在进行的任务,甚至更好的是已完成的任务!如果您想分享的内容不在问题部分,请创建一个,我们会尽快与您联系。
此外,如果您想通过添加新功能或修复错误来帮助我们改进我们的SDK,我们将非常愿意看到您的pull请求!