AffirmSDK 5.0.32

AffirmSDK 5.0.32

测试已测试
Lang语言 Obj-CObjective C
许可证 BSD-3-Clause
发布上次发布2024年6月

Affirm PodOwner 维护。



AffirmSDK 5.0.32

  • Affirm, Inc.

Affirm iOS SDK

CocoaPods Carthage compatible license

该 Affirm iOS SDK 允许您在自己的应用中提供 Affirm。

安装

建议使用 CocoaPodsCarthage 来安装 Affirm SDK。

CocoaPods

将以下内容添加到您的 Podfile 中,然后运行 pod install

pod 'AffirmSDK'

Carthage

将以下内容添加到您的 Cartfile 中,然后遵循此处的设置说明。

github "Affirm/affirm-merchant-sdk-ios"

Swift 包管理器

从 Xcode 11+ 开始

  1. 选择 File > Swift Packages > Add Package Dependency。在“选择包仓库”对话框中输入 https://github.com/Affirm/affirm-merchant-sdk-ios
  2. 在下一页中,将版本解析规则指定为“Up to Next Major”,版本为“5.0.26”。
  3. 在 Xcode 检出源并解析版本后,您可以选择“AffirmSDK”库并将其添加到您的应用程序目标。

有关更多信息,请参阅 Apple 的将包依赖项添加到您的应用程序

或者,您还可以将 AffirmSDK 添加到您的 Package.swift 文件中

dependencies: [
    .package(url: "https://github.com/Affirm/affirm-merchant-sdk-ios", .upToNextMajor(from: "5.0.26"))
]

手动

或者,如果您不想使用CocoaPods或Carthage,您可以将我们的GitHub仓库克隆到本地,并将AffirmSDK文件夹简单拖放到您的XCode项目中。

使用概述

Affirm集成包括两个组件:结账和促销消息。

在您可以使用这些组件之前,您必须首先使用来自您沙箱商家控制台的公共API密钥设置AffirmSDK。您必须将此密钥设置为一次性的shared AffirmConfiguration(最好在AppDelegate中)如下

[[AffirmConfiguration sharedInstance] configureWithPublicKey:@"PUBLIC_API_KEY" locale:AffirmLocaleUS environment:AffirmEnvironmentSandbox merchantName:@"Affirm Example"];

结账

结账创建

结账创建是指客户使用Affirm在您的应用中支付购买的过程。这个过程受AffirmCheckoutViewController对象控制,该对象需要三个参数

  • 包含订单详情的AffirmCheckout对象

  • 确定结账流程是否应使用虚拟卡片网络处理的useVCN对象

    • 如果设置为YES,它将返回来自此代理的借记卡信息

      - (void)vcnCheckout:(AffirmCheckoutViewController *)checkoutViewController completedWithCreditCard:(AffirmCreditCard *)creditCard
      
    • 如果设置为NO,它将返回结账令牌从此代理

      - (void)checkout:(AffirmCheckoutViewController *)checkoutViewController completedWithToken:(NSString *)checkoutToken
      
  • 接收结账过程中各个阶段的消息的AffirmCheckoutDelegate对象

一旦使用上述参数构造了AffirmCheckoutViewController,您可以使用任何其他视图控制器展示它。这启动了引导用户通过Affirm结账流程的过程。如何实现此过程的示例如下

// initialize an AffirmItem with item details
AffirmItem *item = [AffirmItem itemWithName:@"Affirm Test Item" SKU:@"test_item" unitPrice:price quantity:1 URL:[NSURL URLWithString:@"http://sandbox.affirm.com/item"]];

// initialize an AffirmShippingDetail with the user's shipping address
AffirmShippingDetail *shipping = [AffirmShippingDetail shippingDetailWithName:@"Chester Cheetah" addressWithLine1:@"633 Folsom Street" line2:@"" city:@"San Francisco" state:@"CA" zipCode:@"94107" countryCode:@"USA"];

// initialize an AffirmCheckout object with the item(s), shipping details, tax amount, shipping amount, discounts, financing program, and order ID
AffirmCheckout *checkout = [[AffirmCheckout alloc] initWithItems:@[item] shipping:shipping taxAmount:[NSDecimalNumber zero] shippingAmount:[NSDecimalNumber zero] discounts:nil metadata:nil financingProgram:nil orderId:@"JKLMO4321"];

// The minimum requirements are to initialize the AffirmCheckout object with the item(s), shipping details, and payout Amount
AffirmCheckout *checkout = [AffirmCheckout checkoutWithItems:@[item] shipping:shipping payoutAmount:price];

// initialize an UINavigationController with the checkout object and present it
AffirmCheckoutViewController *checkoutViewController = [[AffirmCheckoutViewController alloc] initWithDelegate:self checkout:checkout useVCN:NO getReasonCodes:NO cardAuthWindow:10];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:checkoutViewController];
[self presentViewController:nav animated:YES completion:nil];

// It is recommended that you round the total in the checkout request to two decimal places. Affirm SDK converts the float total to integer cents before initiating the checkout, so may round up or down depending on the decimal places. Ensure that the rounding in your app uses the same calculation across your other backend systems, otherwise, it may cause an error of 1 cent or more in the total validation on your end. 

流程结束,当用户成功确认结账或vcn结账,取消结账,或在过程中遇到错误时。在这些情况下,Affirm将向AffirmCheckoutDelegate发送消息,并包含有关结果的附加信息。

充值授权

用户成功完成结账后,AffirmCheckoutDelegate 对象将收到一个结账令牌。应将此令牌转发到您的服务器,服务器应使用此令牌在用户账户中进行充值授权。有关服务器集成的更多详细信息,请参阅我们的API 文档

注意 - 对于 VCN 结账,所有操作均应使用您现有的支付网关和借记卡处理器,使用成功结账后返回的虚拟卡号执行。

促销信息

Affirm 促销信息组件——每月付款信息和教育性模态——向客户展示他们如何使用 Affirm 为购买融资。促销包括直接出现在您的应用中的促销信息和模态,用户可以通过模态预先审查资格。

要创建促销信息视图,SDK 提供 AffirmPromotionalButton 类,开发人员只需要将其添加到视图并配置即可实现。AffirmPromotionalButton 的实现如下

self.promotionalButton = [[AffirmPromotionalButton alloc] initWithPromoID:nil
                                                                  showCTA:YES
                                                                 pageType:AffirmPageTypeProduct
                                                 presentingViewController:self
                                                                    frame:CGRectMake(0, 0, 315, 34)];
[self.stackView insertArrangedSubview:self.promotionalButton atIndex:0];

要显示/刷新促销信息,请使用

[self.promotionalButton configureByHtmlStylingWithAmount:[NSDecimalNumber decimalNumberWithString:amountText]
                                          affirmLogoType:AffirmLogoTypeName
                                             affirmColor:AffirmColorTypeBlue
                                           remoteFontURL:fontURL
                                            remoteCssURL:cssURL];

self.promotionalButton.configure(amount: NSDecimalNumber(string: amountText),
                         affirmLogoType: .name,
                            affirmColor: .blue,
                                   font: UIFont.italicSystemFont(ofSize: 15),
                              textColor: .gray)

如果您拥有 HTML 原始字符串,可以通过以下方式显示促销信息

[self.promotionalButton configureWithHtmlString:html
                                         amount:amount
                                  remoteFontURL:fontURL
                                   remoteCssURL:cssURL];

(注意:传递给促销信息配置方法的金额字段应以美元为单位(不带美分),因此最好在传递之前将金额四舍五入到最接近的美分)

如果您想使用本地字体,需要执行以下步骤

  1. 将字体文件添加到项目中(确保文件已正确针对应用程序)
  2. 将字体文件添加到您的App-Info.plist中
  3. 在 CSS 文件中使用字体,例如
@font-face
{
font-family: 'OpenSansCondensed-Bold';
src: local('OpenSansCondensed-Bold'),url('OpenSansCondensed-Bold.ttf') format('truetype');
}

body {
font-family: 'OpenSansCondensed-Light';
font-weight: normal;
!important;
}

(注意:如果未返回促销信息,按钮将自动隐藏)

点击促销按钮将自动打开一个包含更多信息的模态,包括(如果已配置)按钮,提示用户预先审查 Affirm 融资资格。

(注意:从 SDK 版本 4.0.13 开始,AffirmPrequalModalViewController 已弃用。)要在不点击 AffirmPromotionalButton 的情况下显示 AffirmPromoModal,可以按照以下方式初始化和显示一个 promo modal viewController 实例

AffirmPromoModalViewController *viewController = [[AffirmPromoModalViewController alloc] initWithPromoId:@"promo_id" amount:amount delegate:delegate];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.presentingViewController presentViewController:nav animated:YES completion:nil];

从“价格尽可能低”的消息中检索原始字符串

您可以使用AffirmDataHandler检索原始字符串。

NSDecimalNumber *dollarPrice = [NSDecimalNumber decimalNumberWithString:self.amountTextField.text];
[AffirmDataHandler getPromoMessageWithPromoID:nil
                                       amount:dollarPrice
                                      showCTA:YES
                                     pageType:AffirmPageTypeBanner
                                     logoType:AffirmLogoTypeName
                                    colorType:AffirmColorTypeBlue
                                         font:[UIFont boldSystemFontOfSize:15]
                                    textColor:[UIColor grayColor]
                     presentingViewController:self
                            completionHandler:^(NSAttributedString *attributedString, UIViewController *viewController, NSError *error) {
                                [self.promoButton setAttributedTitle:attributedString forState:UIControlStateNormal];
                                self.promoViewController = viewController;
}];

之后,您可以使用以下方法展示促销模态框:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.promoViewController];
[self presentViewController:nav animated:YES completion:nil];

跟踪订单确认

当客户完成购买时,将触发trackOrderConfirmed事件。SDK提供了一个AffirmOrderTrackerViewController类来跟踪它,它需要AffirmOrder和一个包含AffirmProduct的数组。

[AffirmOrderTrackerViewController trackOrder:order products:@[product0, product1]];

[注意:此功能将在端点准备就绪后进行改进,并在5秒后消失]

示例

包含与Affirm集成的示例应用的代码库。您可以将我们的GitHub存储库(https://github.com/Affirm/affirm-merchant-sdk-ios)克隆到新的XCode项目文件夹中,并运行Examples项目。

升级

如果您正在使用较旧的SDK版本,您可以参考此升级文档。我们建议您安装最新版本的此SDK,以获取最新功能和体验。

变更日志

本项目的所有重要更改都将记录在 变更日志文档 中。

打包大小

最终二进制大小:0.67 MB