MB Payments
MB Payments 是一个为 MBurger 编写的插件库,用 Objective-C 编写,用于向 MBurger 添加支付功能。该库的最低部署目标为 iOS 10.0。MB Payments 基于 MBurger 和 Stripe,因此在使用客户端应用程序之前,您必须配置两者。
即使这是一个用 Objective-C 编写的库,它也可以在 Swift 项目中集成和使用。下面的示例代码将在两种语言中使用。
安装
使用 CocoaPods 安装
CocoaPods 是 iOS 的依赖项管理器,可自动化并简化在项目中使用第三方库的过程。您可以使用以下命令安装 CocoaPods:
$ gem install cocoapods
要使用 CocoaPods 在 Xcode 项目中将 MBurger 集成到 Podfile 中,指定它:
platform :ios, '10.0'
target 'TargetName' do
pod 'MBPayments', git: 'https://github.com/Mumble-SRL/MBPayments-iOS'
pod 'MBurger', git: 'https://github.com/Mumble-SRL/MBurger-iOS'
end
如果您使用 Swift,请记住在 pod 声明之前添加 use_frameworks!
。
然后,运行以下命令:
$ pod install
CocoaPods 是安装库的首选方法。
手动安装
要手动安装库,请将文件夹 MBPayments
拖放到 Xcode 中的项目结构中。
MBPayments
是MBurger
插件,且基于它进行开发,因此您还需要手动安装。
初始化
要初始化SDK,您需要将MBPayments
类添加到MBurger
的插件数组中。如果您想添加卡片、支付和订阅,则必须创建Stripe令牌,因此您需要配置其客户端SDK。
Objective-C:
#import "AppDelegate.h"
#import "MBurger.h"
#import "MPayments.h"
#import <Stripe/Stripe.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[MBManager sharedManager].apiToken = @"YOUR_API_TOKEN";
[MBManager sharedManager].plugins = @[[MBPPayments new]];
[STPPaymentConfiguration sharedInstance].publishableKey = @"YOUR_PUBLISHABLE_KEY";
return YES;
}
Swift:
import MBurger
import MBPayments
import Stripe
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
MBManager.shared().apiToken = "YOUR_API_TOKEN"
MBManager.shared().plugins = [MBPPayments()]
STPPaymentConfiguration.shared().publishableKey = "YOUR_PUBLISHABLE_KEY"
return true
}
创建客户
在设置SDK后,如果您想向用户收费,则需要为该用户创建一个客户。
Objective-C:
[MBPPayments createCustomerForCurrentUserWithSuccess:^{
} Failure:^(NSError *error) {
NSLog(@"There was an error: %@", error.localizedDescription);
}];
Swift:
MBPPayments.createCustomerForCurrentUserWithSuccess({ in
}, failure: { (error) in
})
用户设置
当前认证用户的用户设置位于MBUser
的pluginsObjects
字典中,键为MBPaymentsUserKey
。它将返回一个MBPUserPaymentSettings
实例。
Objective-C:
MBPUserPaymentSettings *settings = (MBPUserPaymentSettings *) user.pluginsObjects[MBPaymentsUserKey]
Swift:
if let settings = user.pluginsObjects?[MBPaymentsUserKey] as? MBPUserPaymentSettings {
// Do something with the settings.
}
向客户收费
要向客户进行一次性付款,您必须调用createPayment
函数。
Objective-C:
[MBPPayments createPayment:@"PAYMENT_ID"
Amount:AMOUNT_OF_PAYMENT
Quantity:1
Token:TOKEN_OF_THE_CARD
Meta:nil
Success:^{
}
Failure:^(NSError * _Nonnull error) {
}];
Swift:
MBPPayments.createPayment("PAYMENT_ID",
amount: AMOUNT_OF_PAYMENT,
quantity: 1,
token: TOKEN_OF_THE_CARD,
meta: nil,
success: {
}) { error in
}
您可以在meta字段中包含元数据,它将在购买中返回。如果您没有添加令牌,则MBPayments
将尝试使用客户的默认卡片向其收费。
订阅
您还可以使用MBPayments
的函数createSubscription:..
、cancelSubscription:..
和resumeSubscription:...
处理循环订阅。它们都有参数subscription
,这是在Stripe仪表板上创建的订阅名称。如果订阅被取消且尚未过期,则它将进入宽限期。它将有效直到它过期。只能在该订阅处于宽限期时恢复订阅,否则您必须为该客户创建新的订阅。
信用卡
您可以使用 createCardWithToken:..
、getCardsWithSuccess:
和 deleteCardWithCardId
函数来创建、检索和删除用户的信用卡。创建信用卡需要使用 Stripe SDK 生成的 Stripe 令牌。您还可以使用 setDefaultCardWithCardId:
函数将用户的信用卡设置为默认卡片,需要传入卡片 ID。
文档
了解更多信息,您可以查看 docs 文件夹中的完整 SDK 参考。
联系方式
您可以通过以下邮箱联系我们:[email protected]。
许可证
MBPayments 在 Apache 2.0 许可证下发布。有关详情,请参阅 LICENSE。