Square Point of Sale SDK
Square Point of Sale SDK 允许您快速轻松地将对 Square Point of Sale 的店内支付支持添加到您的应用程序中。
需求
- Square 开发者账户。
- Xcode 12.0 或更高版本。
- iOS 12 或更高版本。
目前无法使用销售终端 API 处理假信用卡支付。如果您正在测试集成,则可以处理小额卡片支付(低至 1 美元),然后从 Square Point of Sale 退款。请访问 squareup.com/activate 确保您的账户已启用支付处理。
开始使用
将SDK添加到项目中
CocoaPods
platform :ios, '12.0'
pod 'SquarePointOfSaleSDK'
请务必调用pod update
并使用pod install --repo-update
确保您安装了最新版本的SDK。
将Pods/SquarePointOfSaleSDK.xcodeproj拖到您的项目中,并将SquarePointOfSaleSDK作为构建依赖项添加。
Carthage
github "Square/SquarePointOfSaleSDK-iOS"
SPM
要将SquarePointOfSaleSDK包添加到您的Xcode项目中,请选择文件 > Swift Packages > 添加包依赖,并输入
"https://github.com/square/SquarePointOfSaleSDK-iOS"
更新您的Info.plist
要开始使用Square点销售SDK,您需要将一些更改配置到您的 Info.plist
文件。
首先,在Xcode中转到项目设置并点击“Info”标签。在 自定义iOS目标属性
下
- 添加一个新的条目,密钥为
LSApplicationQueriesSchemes
。 - 将“类型”设置为
数组
。 - 将值
square-commerce-v1
添加到数组中。
接下来,创建一个 URL方案,以便Square点销售在客户完成交易后能够重新打开您的应用程序。如果您的应用程序已经有URL方案,您可以启用它。
最后,打开“URL类型”部分,点击"+"添加一个新URL类型。设置以下值
属性 | 值 |
---|---|
标识符 | Square |
URL方案 | 您的URL方案 |
角色 | 编辑器 |
如下所示
在Square注册您的应用
转到Square开发者门户 并创建一个新的应用程序。
在 点销售API
标签下,添加您应用程序的 捆绑标识符 和 URL方案,然后点击“保存”。
从“凭据”选项卡获取您的 应用程序ID
。
使用
Swift
导入声明: import SquarePointOfSaleSDK
// Replace with your app's URL scheme.
let callbackURL = URL(string: "<# Your URL Scheme #>://")!
// Your client ID is the same as your Square Application ID.
// Note: You only need to set your client ID once, before creating your first request.
SCCAPIRequest.setApplicationID("<# Application ID #>")
do {
// Specify the amount of money to charge.
let money = try SCCMoney(amountCents: 100, currencyCode: "USD")
// Create the request.
let apiRequest =
try SCCAPIRequest(
callbackURL: callbackURL,
amount: money,
userInfoString: nil,
locationID: nil,
notes: "Coffee",
customerID: nil,
supportedTenderTypes: .all,
clearsDefaultFees: false,
returnsAutomaticallyAfterPayment: false,
disablesKeyedInCardEntry: false,
skipsReceipt: false
)
// Open Point of Sale to complete the payment.
try SCCAPIConnection.perform(apiRequest)
} catch let error as NSError {
print(error.localizedDescription)
}
最后,按如下方式实现 UIApplication 的代理方法
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
guard SCCAPIResponse.isSquareResponse(url) else {
return true
}
do {
let response = try SCCAPIResponse(responseURL: url)
if let error = response.error {
// Handle a failed request.
print(error.localizedDescription)
} else {
// Handle a successful request.
}
} catch let error as NSError {
// Handle unexpected errors.
print(error.localizedDescription)
}
return true
}
Objective C
导入声明: @import SquarePointOfSaleSDK;
// Replace with your app's callback URL.
NSURL *const callbackURL = [NSURL URLWithString:@"<#Your URL Scheme#>://"];
// Specify the amount of money to charge.
SCCMoney *const amount = [SCCMoney moneyWithAmountCents:100 currencyCode:@"USD" error:NULL];
// Your client ID is the same as your Square Application ID.
// Note: You only need to set your client ID once, before creating your first request.
[SCCAPIRequest setClientID:<#Client ID#>];
SCCAPIRequest *request = [SCCAPIRequest requestWithCallbackURL:callbackURL
amount:amount
userInfoString:nil
locationID:nil
notes:@"Coffee"
customerID:nil
supportedTenderTypes:SCCAPIRequestTenderTypeAll
clearsDefaultFees:NO
returnAutomaticallyAfterPayment:NO
error:&error];
当准备好向客户收费时,将 Point of Sale 带入前台以完成支付
[SCCAPIConnection performRequest:request error:&error];
最后,实现相关的 UIApplication 代理
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)URL options:(NSDictionary<NSString *,id> *)options;
{
if ([SCCAPIResponse isSquareResponse:url]) {
SCCAPIResponse *const response = [SCCAPIResponse responseWithResponseURL:URL error:&decodeError];
...
return YES;
}
return NO;
}
Contributing
我们很高兴您对 Square Point of Sale SDK 产生兴趣,并希望看到您能将其带到哪里。在提交 Pull Request 之前,请阅读我们的 贡献指南。
Releasing
首先创建一个新的标签
git tag XYZ
将标签推送到 Github
git push --tags
在 Github.com 上生成一个新的发行版本,并使用
bundle exec pod gen && carthage build --no-skip-current --platform ios --use-xcframeworks && carthage archive SquarePointOfSaleSDK && bundle exec pod trunk push SquarePointOfSaleSDK.podspec --allow-warnings
支持
如果您在使用此SDK的项目中遇到问题,请在带有square-connect
标签的Stack Overflow上创建一个问题。我们的团队会查看这个标签,并将能够帮助您。如果您认为SDK本身存在问题,请创建一个问题。
许可
版权所有 2021 Square, Inc.
遵守Apache License, Version 2.0("许可");除非按照适用的法律规定或书面同意,否则不得使用此文件,除非符合本许可。您可以在以下位置获得许可证的副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非法律规定或书面同意,否则在许可下分发的软件按“现状”提供,不提供任何明示或暗示的保证或条件。有关许可中具体规定的权限和限制,请参阅许可。