这是一个支持主流支付方式,如微信支付、支付宝和 Braintree 等 的支付 SDK。
- 在集成支付之前,请联系 Pockyt 团队创建账户。我们可能需要您提交文档文件。
- 对于微信支付,请在微信开放平台中配置包识别符和通用链接。官方指南
- 对于 Braintree,请联系 Pockyt 团队确认支付模式。当与 Braintree 集成时,您需要创建一个账户并在官方平台上配置。官方指南
- Pockyt 可通过 CocoaPods 获得。由于支付方式的独立性,您可以自由选择要安装和组合的支付方式。Pockyt 默认包括支付宝和微信支付,如果只需要这两种方式。要安装它,只需将以下行添加到您的 Podfile 中
pod 'Pockyt'
- 以下是各种支付方法的单独安装形式,可以将它们下载为 'Pockyt/xxx' 子组件。重要的是要注意,'Pockyt/DropIn' 将自动包含其他 Braintree 支付方式。DropIn 是一个使用官方 UI 库的快速集成方法,但是您仍然需要从 SDK 中导入子模块以便于使用。而非 DropIn 方法需要单独添加每个支付组件。
pod 'Pockyt/WechatPay', :path => '../'
pod 'Pockyt/Alipay', :path => '../'
pod 'Pockyt/DropIn', :path => '../'
pod 'Pockyt/ApplePay', :path => '../'
pod 'Pockyt/CardPal', :path => '../'
pod 'Pockyt/Venmo', :path => '../'
pod 'Pockyt/ThreeDSecure', :path => '../'
pod 'Pockyt/DataCollect', :path => '../'
- 在 Xcode 中,选择您项目的设置,选择“目标”标签,然后选择“info”标签。在“URL 类型”部分下,添加一个包含应用程序标识符的“URL Scheme”,例如“pockyt2alipay”。建议使用独特且不与其他商户应用重叠的标识符。否则,可能会导致无法从支付宝正确重定向回商户的应用。
- 在 Xcode 中,选择您项目的设置,选择“目标”标签,然后选择“info”标签。在“URL 类型”部分下,添加您已注册的应用程序 ID。
- 在 Xcode 中,选择您项目的设置,选择“目标”标签,然后选择“info”标签。在“LSApplicationQueriesSchemes”部分下,添加“weixin”和“weixinULAPI”。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
<string>weixinULAPI</string>
</array>
- 配置应用程序的通用链接。
根据苹果文档(https://developer.apple.com/documentation/xcode/allowing-apps-and-websites-to-link-to-your-content)配置您的应用的通用链接。在Xcode中,开启“关联域名”开关,并将通用链接域名添加到配置中。请前往微信公众平台-开发者应用注册页面进行注册。注册并选择配置的移动应用程序后,您将获得一个可用于开发的App ID。然而,在完成应用程序注册后,仍然需要经过提交和审查流程。只有通过审查的应用才能正式发布和使用。
- Xcode 12+
- 最低部署目标为iOS 12.0
- Swift 5.1+(或Objective-C)
- 在Xcode中,选择您的项目设置,选择“TARGETS”选项卡,然后选择“info”选项卡。在“URL Types”部分,将标识符添加为“braintree”。请注意,“braintree”是专有名词,不可更改,因为它将影响Venmo的集成。添加一个带有应用程序标识符的“URL Scheme”(以您的应用包标识符开头),例如“com.yuansfer.msdk.pockyt2braintree”。
- 您必须将以下内容添加到您的应用程序info.plist中允许的查询方案中
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.venmo.touch.v2</string>
</array>
- 您的应用程序的info.plist中必须有一个显示名称,以帮助Venmo识别您的应用程序
<key>CFBundleDisplayName</key>
<string>Your App Name</string>
- 为了在真实设备上使用Apple Pay,您必须在苹果开发者中心配置Apple Pay商户ID和Apple Pay支付处理证书,请参阅官方指南(https://developer.paypal.com/braintree/docs/guides/apple-pay/configuration/ios/v5)。
- 在Xcode中,在项目设置中启用“功能”下的Apple Pay。然后启用Apple Pay商户ID。重要的是,您必须使用带有Apple Pay商户ID的开发团队配置文件编译应用程序。Apple Pay不支持企业配置。
- 对于微信支付、支付宝和Venmo,您需要在AppDelegate中重写以下方法
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return Pockyt.shared.handleOpenURL(url)
}
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return Pockyt.shared.handleOpenURL(url)
}
// For WeChat Pay
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return Pockyt.shared.handleOpenUniversalLink(userActivity)
}
- 调用Pockyt预支付API(/micropay/v3/prepay或/online/v3/secure-pay)后,创建一个支付对象并调用Pockyt.shared.requestPay方法。
// For Alipay
let payment = Alipay(payInfo: payInfo, fromScheme: "pockyt2alipay")
Pockyt.shared.requestPay(payment) { result in
DispatchQueue.main.async {
self.resultLabel.text = "Paid: \(result.isSuccessful), cancelled: \(result.isCancelled), \(result)"
}
}
// For WeChat Pay
let request = WechatPayRequest(partnerId: partnerid, prepayId: prepayid, packageValue: package, nonceStr: noncestr, timeStamp: timestamp, sign: sign)
Pockyt.shared.requestPay(WechatPay(request)) { result in
DispatchQueue.main.async {
self.resultLabel.text = "Paid: \(result.isSuccessful), cancelled: \(result.isCancelled), \(result)"
}
}
// For Drop-in UI
let dropReq = BTDropInRequest()
// BTDropInRequest has many configuration options
// ThreeDSeucre for card, optional
// dropReq.threeDSecureRequest = createThreeDSecure()
let payment = DropInPay(uiViewController: self, clientToken: authorization, dropInRequest: dropReq)
Pockyt.shared.requestPay(payment) { result in
DispatchQueue.main.async {
if let nonce = result.dropInResult?.paymentMethod?.nonce{
self.resultLabel.text = "Obtained nonce: \(result.isSuccessful), cancelled: \(result.isCancelled), nonce: \(nonce)"
} else if .applePay == result.dropInResult?.paymentMethodType {
self.resultLabel.text = result.respMsg
// Note that Apple Pay requires continuing the payment flow initiation
self.startApplePay()
} else if let error = result.respMsg {
self.resultLabel.text = "Failed to obtain nonce, cancelled: \(result.isCancelled), error: \(error)"
} else {
self.resultLabel.text = "Failed to obtain nonce, cancelled: \(result.isCancelled)"
}
}
}
// For PayPal
let request = BTPayPalCheckoutRequest(amount: "1.00")
let request = BTPayPalVaultRequest()
let paypal = PayPal(authorization: HttpUtils.CLIENT_TOKEN, paypalRequest: request)
Pockyt.shared.requestPay(paypal) { result in
DispatchQueue.main.async {
if result.isSuccessful {
if let nonce = result.paypalAccountNonce?.nonce {
self.resultLabel.text = "Obtained nonce: \(nonce)"
} else {
self.resultLabel.text = "Failed to obtain nonce"
}
} else {
self.resultLabel.text = "Failed to obtain nonce, error: \(result.respMsg ?? "Unknown error")"
}
}
}
// For Venmo
let request = BTVenmoRequest()
request.paymentMethodUsage = .multiUse
let venmo = Venmo(authorization: HttpUtils.CLIENT_TOKEN, venmoRequest: request)
Pockyt.shared.requestPay(venmo) { result in
DispatchQueue.main.async {
if result.isSuccessful {
if let nonce = result.venmoNonce?.nonce {
self.resultLabel.text = "Obtained nonce: \(nonce)"
} else {
self.resultLabel.text = "Failed to obtain nonce"
}
} else {
self.resultLabel.text = "Failed to obtain nonce, error: \(result.respMsg ?? "Unknown error")"
}
}
}
// For Apple Pay, There are a few additional steps in the payment process compared to the ones mentioned above
// First, initialize the Apple Pay request parameters
let applePay = ApplePay(viewController: self, authorization: HttpUtils.CLIENT_TOKEN)
applePay.initPaymentRequest() { paymentRequest, error in
DispatchQueue.main.async {
if let paymentRequest = paymentRequest {
self.resultLabel.text = "Payment request initialized"
self.showApplePaySheet(paymentRequest: paymentRequest)
self.presentAuthorizationViewController(applePay)
} else {
self.resultLabel.text = "Failed to initialize payment request"
}
}
}
// Then, present the Apple Pay sheet
private func showApplePaySheet(paymentRequest: PKPaymentRequest) {
paymentRequest.requiredBillingContactFields = [.postalAddress]
// Set other PKPaymentRequest properties here
paymentRequest.merchantCapabilities = .capability3DS
paymentRequest.paymentSummaryItems =
[
PKPaymentSummaryItem(label: "test_item", amount: NSDecimalNumber(string: "0.02")),
// Add add'l payment summary items...
PKPaymentSummaryItem(label: "Pockyt.io", amount: NSDecimalNumber(string: "0.02")),
]
// ...
}
// Secondly, present the Apple Pay authorization view controller
private func presentAuthorizationViewController(_ applePay: ApplePay) {
applePay.requestPay() { result in
DispatchQueue.main.async {
if result.isSuccessful {
self.resultLabel.text = "Payment processing, please wait..."
self.submitNonceToServer(applePay: applePay, transactionNo: "xxx", nonce: result.applePayNonce!.nonce)
self.resultLabel.text = "Payment successful, nonce: \(result.applePayNonce!.nonce)"
} else {
self.resultLabel.text = result.respMsg
}
}
}
}
// Finally, Submit the Apple Pay nonce to your server, Notify the Apple Pay wallet of the payment status based on the API result
if (apiSuccess) {
applePay.notifyPaymentCompletion(true)
} else {
applePay.notifyPaymentCompletion(false)
}
- 对于Braintree,获取支付结果中的nonce后,调用Pockyt处理API(/creditpay/v3/process)以完成支付。
- 'deviceData'用于降低退货率。建议使用DataCollector的collectData方法获取并提交数据到服务器进行处理。
- 当生成客户端令牌时传递'customerNo',Drop-in将显示该客户的保存的支付方式,并将任何新输入的支付方式自动添加到他们的Vault记录中。创建客户API: https://docs.pockyt.io/reference/register-customer 如果存在保存在库中的支付方式,这就是它们在Drop-in中的显示方式。
- 有关SDK的详细使用方法,请参阅Pockyt提供的示例。