CheckoutKit 3.0.6

CheckoutKit 3.0.6

测试已测试
Lang语言 SwiftSwift
许可 MIT
发布最后发布2018年7月
SPM支持 SPM

Checkout.com Integration 维护。



  • Checkout.com Integration

需求

Swift 3, iOS 8.0 和更高版本

如果您使用 Swift 1.2,请参阅 CheckoutKit 1.0.0

如果您使用 Swift 2.0,请参阅 CheckoutKit 2.0.0

如果您使用 Objective-C,请参阅 CheckoutKit objective-c

如何使用库

您可以将 CheckoutKit 添加到项目的两种方式

CocoaPods

该框架在 CocoaPods 上可用。以下需要在 Podfile 中添加:

use_frameworks!
pod 'CheckoutKit', '~>3.0.0'
xcodeproj 'path/to/proj.xcodeproj'

然后运行 pod install,框架即可使用。

手动

否则,可以从 GitHub 仓库 下载框架源代码并手动将其添加到你的 Xcode 工作区。在工作区中,选择“文件”>“将文件添加到…”并选择 CheckoutKit/CheckoutKit.xcodeproj。然后在你的项目中选择“项目”>“通用”>“链接的框架和库”并添加 CheckoutKit.framework

示例

CheckoutKit/CheckoutKitExample 文件夹中提供了一个演示应用程序。

按如下方式在代码中导入 CheckoutKit 框架:

import CheckoutKit

您需要在创建 CheckoutKit 实例时指定至少公钥。我们提供了一系列构造函数,CheckoutKit 实例可以完全自定义。

CheckoutKit.getInstance(pk: String, env: Environment, debug: Bool, logger: Log) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String, debug: Bool, logger: Log) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String, env: Environment, logger: Log) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String, env: Environment, debug: Bool) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String, env: Environment) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String, logger: Log) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String, debug: Bool) throws -> CheckoutKit?
CheckoutKit.getInstance(pk: String) throws -> CheckoutKit?
CheckoutKit.getInstance() throws -> CheckoutKit? // to be called only once the CheckoutKit object has been initialized and a public key has been provided

这些函数可能会抛出 CheckoutError。如果公钥无效,则抛出的错误是 CheckoutError.InvalidPK,如果在实例化对象之前调用 getInstance 而没有公钥参数,则抛出的错误是 CheckoutError.NoPK(两者都包含自定义的错误消息)。以下是参数的更多详细信息:

  • pk : 包含公钥的字符串。它将与以下正则表达式进行测试:^pk_(?:test_)?(?:\w{8})-(?:\w{4})-(?:\w{4})-(?:\w{4})-(?:\w{12})$。这是必需的,否则 CheckoutKit 对象无法实例化。
  • env : 包含商户环境信息的 Environment 对象,默认为 SANDBOX。可选。
  • debug : Bool,是否激活调试模式,默认为 true。可选。
  • 日志记录器:将日志对象信息、警告和错误打印到控制台(目前)。可选。

如果将 调试 设置为 true,许多操作将被追踪到日志系统中。

CheckoutKit 的可用函数可以在 文档 中找到。

另一个可用于实用函数的类是 CardValidator。它提供了用于在处理之前验证所有与卡相关的信息的静态函数。可用函数的列表可在 文档 中找到。

创建卡令牌

import CheckoutKit

let publicKey: String = "your_public_key"

/* Instantiates the CheckoutKit object with the minimum parameters, default configuration for the other ones */
var ck: CheckoutKit? = nil
do {
    try ck = CheckoutKit.getInstance(publicKey)
} catch _ as NSError {
    /* Happens when the public key is not valid, raised during the instanciation of the CheckoutKit object */
    /* Type of the error in the enum CheckoutError. Different types are NoPK (if getInstance is called with no parameters and no public key has been provided before) and InvalidPK (if the public key is invalid) */
}


/* Take the card information where ever you want (form, fields in the application page...) */
var card: Card? = nil
do {
    try card = Card(name: nameField.text!, number: numberField.text!, expYear: year, expMonth: month, cvv: cvvField.text!, billingDetails: nil)
} catch let err as CardError {
    /* Happens when the card informations entered by the user are not correct */
    /* Type of the error in the enum CardError. Different types are InvalidCVV (if the CVV does not have the correct format), InvalidExpiryDate (if the card is expired), InvalidNumber (if the card's number is wrong). */
} catch _ as NSError {
    /* If any other exception was thrown */
}

ck!.createCardToken(card!, completion:{ (resp: Response<CardTokenResponse>) -> Void in
        if (resp.hasError) {
            /* Print error message or handle it */
        } else {
            /* The card object */
            let ct: CardToken = resp.model!.card
            /* The field containing the card token to make a charge */
            let cardToken: String = resp.model!.getCardToken()
        }
    })
}

日志记录

大部分的 CheckoutKit 活动要么作为信息、警告或错误记录。目前所有记录都是输出到控制台。只有在启用调试模式(默认为 true,但可显式设置为 false)时才会发生记录。打印格式为 日期(yyyy/MM/dd HH:mm:ss)**主题/类名**记录消息。可以使用 setLogger 或 getInstance 函数修改记录器。然后将记录条目添加到指定的记录器中。

单元测试

所有单元测试均使用 XCTest 和 Nimble 编写,位于 CheckoutKit/CheckoutKitTests。要运行测试,需要安装 Nimble v5.0。这可以通过在 CheckoutKit 项目的内部运行 pod install(Podfile 已编写)或以其他方式安装。