RecurlySDK 2.0.8

RecurlySDK 2.0.8

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2023年9月

由 Recurly, Inc. 维护。



  • Recurly, Inc.

Recurly iOS SDK

Recurly Client iOS

Recurly SDK 允许您在几分钟内将定期付款集成到现有的 iOS 应用中。

我们鼓励我们的合作伙伴查阅 Apple 的移动应用开发指导方针。特别是,请查阅付款部分以熟悉购买和货币准则:https://developer.apple.com/app-store/review/guidelines/#payments

当客户提交您的付款表单时,Recurly iOS SDK 会将客户付款信息发送至 Recurly 进行加密和存储,并提供一个计费令牌,您可以使用我们的强大 API 完成订阅过程。

凭借这个计费令牌,您可以使用我们的 API 做任何事情,这些事情需要付款信息。因为您从未处理过任何敏感的付款信息,您的 PCI 范围将大幅缩小。

1. 下载

通过 GitHub 访问我们的 SDK:iOS 客户端仓库

在通过 GitHub 检查我们的 SDK 后,您可以选择以下两种方式之一开始使用 Recurly iOS SDK。

1.1 使用 CocoaPods

如果您已经配置并使用 Cocoapods,则可跳至第 3 步。

  1. 安装 CocoaPods 如果您还没有的话。

  2. 配置您的项目中的CocoaPods。

  3. 将此行添加到您的Podfile中。

    pod 'RecurlySDK'
  4. 通过运行以下命令下载RecurlySDK和其他指定的库:

    $ pod install

有关CocoaPods和Podfile的更多信息,请访问:https://guides.cocoapods.org.cn/using/the-podfile.html

1.2 使用RecurlySDK.framework

  1. 从发行页面下载框架(或使用提供的build.sh脚本自行构建)。
  2. 将其添加到您的现有Xcode项目中。
  3. RecurlySDK需要以下框架:
  • Foundation
  • UIKit
  • AddressBook
  • Security
  • CoreTelephony
  • PassKit
  1. 将标志-ObjC添加到Other Linker Flags(位于Build Settings > Linking)。

2. 导入

一旦框架添加到您的项目中(通过上述任一方法),您只需要导入SDK。

import RecurlySDK_iOS

3. 配置

为了连接到Recurly API,您必须使用API公钥初始化SDK。这可以在Recurly站点的API凭据页面找到:https://app.recurly.com/go/developer/api_access

REConfiguration.shared.initialize(publicKey: "Your Public Key")
// after configuring, you can perform any operation with the SDK!

我们强烈建议您在应用程序启动时(例如在您的AppDelegate中或为您的@main App(SwiftUI)创建自定义init)配置SDK。

@main
struct ContainerApp: App {
    init() {
        REConfiguration.shared.initialize(publicKey: "Your Public Key")
    }
    var body: some Scene {

        WindowGroup {
            ContentView()
        }
    }
}

单元测试

RecurlySDK-iOSTests/RecurlySDK-iOSTests.swift从命令行接收环境变量(如PUBLIC_KEY)的预期。

如果您想在Xcode中运行测试,请确保在测试文件中直接使用自己的文件设置publicKey变量。没有您提供的有效公钥,测试将无法通过。

4. 示例

一旦导入并配置了SDK,我们就可以用它开始构建内容了!

显示我们的RECreditCardInputUI文本字段

       VStack(alignment: .center) {
            RECreditCardInputUI(cardNumberPlaceholder: "Card number",
                                expDatePlaceholder: "MM/YY",
                                cvvPlaceholder: "CVV")
                .padding(10)

            Button {
                getToken { myToken in
                    print(myToken)
                }
            } label: {
                Text("Subscribe")
                    .fontWeight(.bold)
            }
        }.padding(.vertical, 100)

显示单个组件

        VStack(alignment: .center, spacing: 20) {
            VStack(alignment: .leading) {
                RECardNumberTextField(placeholder: " Card number")
                    .padding(.bottom, 30)

                HStack(spacing: 15) {
                    REExpDateTextField(placeholder: "MM/YY")
                    RECVVTextField(placeholder: "CVV")
                }.padding(.bottom, 3)

            }.padding(.horizontal, 51)
            .padding(.vertical, 10)

            Button {
                getToken { myToken in
                    print(myToken)
                }
            } label: {
                Text("Subscribe")
                    .fontWeight(.bold)
            }
        }

应用自定义字体和大小

    RECreditCardInputUI(cardNumberPlaceholder: "Card number",
                                expDatePlaceholder: "MM/YY",
                                cvvPlaceholder: "CVV",
                                textFieldFont: Font.system(size: 15, weight: .bold, design: .default),
                                titleLabelFont: Font.system(size: 13, weight: .bold, design: .default))

获取支付令牌

let billingInfo = REBillingInfo(firstName: "David",
                                lastName: "Figueroa",
                                address1: "123 Main St",
                                address2: "",
                                company: "CH2",
                                country: "USA",
                                city: "Miami",
                                state: "Florida",
                                postalCode: "33101",
                                phone: "555-555-5555",
                                vatNumber: "",
                                taxIdentifier: "",
                                taxIdentifierType: "")
//Inject the BillingInfo
RETokenizationManager.shared.billingInfo = billingInfo

//Get the TokenId for your Billing Info
RETokenizationManager.shared.getTokenId { tokenId, error in
        if let errorResponse = error {
            print(errorResponse.error.message ?? "")
            return
        }    
            print(tokenId ?? "")
        }

或(请求tokenId时与CardData完全相同)

当用户输入卡数据时,将其传递到我们的框架中,所以您不需要它也没有权限访问敏感用户信息

// Display The RECreditCardInputUI or The Individual Components and as the User types in, the info will be ready to submitt inside our Framework

//Get the TokenId for your Card Data
RETokenizationManager.shared.getTokenId { tokenId, error in
        if let errorResponse = error {
            print(errorResponse.error.message ?? "")
            return
        }    
            print(tokenId ?? "")
        }

5. 支持Apple Pay

以下假设您的公司已经设置为Apple Pay商家。有关配置和设置的更多信息,请参阅仓库中的README-APPLE-PAY-CONFIG.md文档。

要使用我们的SDK包含Apple Pay支持,您需要遵循以下步骤:

实例化 REApplePaymentHandler 类

// Apple Payment handler instance
    let paymentHandler = REApplePaymentHandler()

使用我们的 REApplePayButton

/// Apple Pay
            VStack(alignment: .center) {
                Group {
                    REApplePayButton(action: {
                        getTokenApplePayment { completed in
                            // Do something
                        }
                    })
                    .padding()
                    .preferredColorScheme(.light)
                }
                .previewLayout(.sizeThatFits)
            }

Apple Pay Token 回调

// Get token from ApplePay button
    private func getTokenApplePayment(completion: @escaping (Bool) -> ()) {

        // Test items
        var items = [REApplePayItem]()

        items.append(REApplePayItem(amountLabel: "Foo",
                                    amount: NSDecimalNumber(string: "3.80")))
        items.append(REApplePayItem(amountLabel: "Bar",
                                    amount: NSDecimalNumber(string: "0.99")))
        items.append(REApplePayItem(amountLabel: "Tax",
                                    amount: NSDecimalNumber(string: "1.53")))

        // Using 'var' instance to change some default properties values
        var applePayInfo = REApplePayInfo(purchaseItems: items)
        // By default only require .phoneNumber and emailAddress
        applePayInfo.requiredContactFields = [.name, .phoneNumber, .emailAddress]
        // This MerchantID is required by Apple, you can learn more about:
        // https://developer.apple.com/apple-pay/sandbox-testing/
        applePayInfo.merchantIdentifier = "merchant.com.YOURDOMAIN.YOURAPPNAME"
        applePayInfo.countryCode = "US"
        applePayInfo.currencyCode = "USD"

        /// Starting the Apple Pay flow
        self.paymentHandler.startApplePayment(with: applePayInfo) { (success, token) in

            if success {
                /// Token object 'PKPaymentToken' returned by Apple Pay
                guard let token = token else { return }

                /// Decode Token
                let paymentData = String(data: token.paymentData, encoding: .utf8)
                guard let paymentJson = paymentData else { return }

                let displayName = token.paymentMethod.displayName ?? "unknown"
                let network = token.paymentMethod.network?.rawValue ?? "unknown"
                let type = token.paymentMethod.type
                let txId = token.transactionIdentifier

                // Creating a test object to send Recurly
                let applePayTokenString = """
                {\"merchantIdentifier\":\"\(applePayInfo.merchantIdentifier)\",\
                \"payment\":{\"token\":{
                \"paymentData\":\(paymentJson),\
                \"paymentMethod\":{\"displayName\":\"\(displayName)\",\"network\":\"\(network)\",\"type\":\"\(type)\"},\
                \"transactionIdentifier\":\"\(txId)\"}}}
                """

                print("Success Apple Payment with token: \(applePayTokenString)")

            } else {
                print("Apple Payment Failed")
            }

            completion(success)
        }
    }