Airwallex iOS SDK
Airwallex iOS SDK 是一个框架,用于在您的应用中将简单的、快速的、安全的支付与 Airwallex 集成。它提供了简单功能,可以直接向 Airwallex 发送敏感信用卡数据,它还提供了一个强大、可定制的界面,用于收集用户支付详细信息。
使用我们的集成指南和示例项目开始。
目录
要求
Airwallex iOS SDK 支持 iOS 11.0 及以上版本。
集成
CocoaPods
iOS上的Airwallex可通过CocoaPods获取。
将此行添加到您的Podfile
pod 'Airwallex'
可选,您还可以直接包含模块(建议这样做以确保最小依赖)
pod 'Airwallex/Core'
pod 'Airwallex/Card'
pod 'Airwallex/WechatPay'
pod 'Airwallex/Redirect'
pod 'Airwallex/ApplePay'
运行以下命令
pod install
Swift
尽管Airwallex
是用Objective-C编写的,但它可以无障碍地在Swift中使用。如果您使用CocoaPods,请在Podfile中添加以下行
use_frameworks!
基本集成
当应用程序启动时,使用mode
配置SDK。
[Airwallex setMode:AirwallexSDKStagingMode]; // AirwallexSDKDemoMode, AirwallexSDKStagingMode, AirwallexSDKProductionMode
如果想在不同的端点测试,可以自定义模式和支付URL。
[Airwallex setDefaultBaseURL:[NSURL URLWithString:@”Airwallex payment base URL”]];
- 创建支付意向
当客户想结账一个订单时,您应该在服务器端创建一个支付意向,然后将该意向传递到移动端以确认所选的支付方式。
[AWXAPIClientConfiguration sharedConfiguration].clientSecret = "The payment intent's client secret";
注意:当checkoutMode
为AirwallexCheckoutRecurringMode
时,不需要创建支付意向。相反,您需要使用客户id生成一个客户端密钥,并将其传递给AWXAPIClientConfiguration
。
[AWXAPIClientConfiguration sharedConfiguration].clientSecret = "The client secret generated with customer id";
- 创建会话
如果想要进行一次性支付,创建一次性会话。
AWXOneOffSession *session = [AWXOneOffSession new];
session.countryCode = "Your country code";
session.billing = "Your shipping address";
session.returnURL = "App return url";
session.paymentIntent = "Payment intent";
session.autoCapture = "Whether the card payment will be captured automatically (Default YES)";
如果想要进行循环支付,创建循环支付会话。
AWXRecurringSession *session = [AWXRecurringSession new];
session.countryCode = "Your country code";
session.billing = "Your shipping address";
session.returnURL = "App return url";
session.currency = "Currency code";
session.amount = "Total amount";
session.customerId = "Customer id";
session.nextTriggerByType = "customer or merchant";
session.requiresCVC = "Whether it requires CVC (Default NO)";
session.merchantTriggerReason = "Unscheduled or scheduled";
如果想要利用支付意向进行循环支付,创建具有支付意向的循环支付会话。
AWXRecurringWithIntentSession *session = [AWXRecurringWithIntentSession new];
session.countryCode = "Your country code";
session.billing = "Your shipping address";
session.returnURL = "App return url";
session.paymentIntent = "Payment intent";
session.autoCapture = "Whether the card payment will be captured automatically (Default YES)";
session.nextTriggerByType = "customer or merchant";
session.requiresCVC = "Whether it requires CVC (Default NO)";
session.merchantTriggerReason = "Unscheduled or scheduled";
- 处理一次性支付或循环流程
结账时,使用AWXUIContext
显示支付流程,用户可以在其中选择支付方式。
AWXUIContext *context = [AWXUIContext sharedContext];
context.delegate = "The target to handle AWXPaymentResultDelegate protocol";
context.session = "The session created above";
[context presentPaymentFlowFrom:self];
- 处理支付结果
用户成功完成支付或出现错误后,您需要处理支付结果。
#pragma mark - AWXPaymentResultDelegate
- (void)paymentViewController:(UIViewController *)controller didCompleteWithStatus:(AirwallexPaymentStatus)status error:(nullable NSError *)error
{
[controller dismissViewControllerAnimated:YES completion:^{
// Status may be success/in progress/ failure / cancel
}];
}
配置微信支付
支付完成后,微信会将用户重定向到商户的应用程序,并使用onResp()进行回调,然后在商户服务器通知后,可以检索支付意向状态,因此请继续监听通知。
@interface AppDelegate () <WXApiDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[WXApi registerApp:@"WeChat app id" universalLink:@"https://airwallex.com/"];
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [WXApi handleOpenURL:url delegate:self];
}
/**
You can retrieve the payment intent status after your server is notified
*/
- (void)onResp:(BaseResp *)resp
{
if ([resp isKindOfClass:[PayResp class]]) {
NSString *message = nil;
PayResp *response = (PayResp *)resp;
switch (response.errCode) {
case WXSuccess:
message = NSLocalizedString(@"Succeed to pay", nil);
break;
case WXErrCodeUserCancel:
message = NSLocalizedString(@"User cancelled.", nil);
break;
default:
message = NSLocalizedString(@"Failed to pay", nil);
break;
}
UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
[controller addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Close", nil) style:UIAlertActionStyleCancel handler:nil]];
[self.window.rootViewController presentViewController:controller animated:YES completion:nil];
}
}
@end
设置苹果支付
Airwallex iOS SDK 允许商家将苹果支付作为支付方式提供给客户。
- 请确保在应用中正确设置苹果支付。更多信息,请参阅 Apple 的官方 文档。
- 请确保在您的 Airwallex 账户中启用了苹果支付。
- 安装 SDK 时,请包括苹果支付模块。
- 准备 商户标识符 并在支付会话对象上配置
applePayOptions
。
苹果支付现在将被显示为支付方式界面中的一个选项。
AWXOneOffSession *session = [AWXOneOffSession new];
...
... configure other properties
...
session.applePayOptions = [[AWXApplePayOptions alloc] initWithMerchantIdentifier:@"Merchant Identifier"];
请注意,目前苹果支付仅支持
AWXOneOffSession
。我们将未来添加对定期支付会话的支持。
定制苹果支付
您可以定制苹果支付选项以限制它,并同时提供额外上下文。更多信息,请参阅 AWXApplePayOptions.h
头文件。
AWXApplePayOptions *options = ...;
options.additionalPaymentSummaryItems = @[
[PKPaymentSummaryItem summaryItemWithLabel:@"goods" amount:[NSDecimalNumber decimalNumberWithString:@"10"]],
[PKPaymentSummaryItem summaryItemWithLabel:@"tax" amount:[NSDecimalNumber decimalNumberWithString:@"5"]]
];
options.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityDebit;
options.requiredBillingContactFields = [NSSet setWithObjects:PKContactFieldPostalAddress, nil];
options.supportedCountries = [NSSet setWithObjects:@"AU", nil];
options.totalPriceLabel = @"COMPANY, INC.";
限制
请注意,我们目前支持以下用于苹果支付的支付网络
- 威士(Visa)
- 万事达(MasterCard)
- 银联(ChinaUnionPay)
- 麦德龙(Maestro)(iOS 12+)
在苹果支付过程中,客户只能选择上述支付网络的卡片。
优惠券目前也不支持。
主题颜色
您可以自定义主题颜色。
UIColor *tintColor = [UIColor colorWithRed:97.0f/255.0f green:47.0f/255.0f blue:255.0f/255.0f alpha:1];
[AWXTheme sharedTheme].tintColor = tintColor;
[UIView.appearance setTintColor:tintColor];
示例
示例可以在最新的 Xcode 中运行。要运行示例应用,您应该遵循以下步骤。
- 克隆源代码
运行以下脚本将此项目克隆到您的本地磁盘。
git clone [email protected]:airwallex/airwallex-payment-ios.git
- 安装依赖项并打开项目
请确保您已安装Cocoapods,然后在项目目录中运行以下命令
pod install
- 更新密钥文件(可选)
在 Examples/Keys
文件夹中,使用适当的密钥编辑 Keys_sample.json
,并将其重命名为 Keys.json
。
- 构建并运行
Examples
脚本
如果您没有更新密钥文件,您可以在应用设置屏幕中更新密钥。在继续结账之前,请确保先点击 生成客户
按钮。
贡献
我们欢迎所有类型的贡献,包括新功能、错误修复和文档改进。最佳的贡献方式是通过提交拉取请求——我们将尽力尽快回复您的补丁。如果您发现错误或有任何问题,也可以提交问题。