【发布时间】:2022-01-05 02:56:54
【问题描述】:
我正在使用 Paypal-Braintree 的官方教程进行支付网关。
我按照教程仍然报错。
我的 viewcontroller 代码有错误 -
import UIKit
import Braintree
class MyViewController: UIViewController {
var braintreeClient: BTAPIClient?
override func viewDidLoad() {
super.viewDidLoad()
// Example: Initialize BTAPIClient, if you haven't already
braintreeClient = BTAPIClient(authorization: "sandbox_bnkgmk59_5dxr37xvb54mg8sp ")
}
@IBAction func clickedBtnPay(_ sender: Any) {
let payPalDriver = BTPayPalDriver(apiClient:braintreeClient) ***/*Error - i) Value of optional type 'BTAPIClient?' must be unwrapped to a value of type 'BTAPIClient'
ii) 使用 '??' 合并当可选值包含“nil”时提供默认值 iii) 使用 '!' 强制展开如果可选值包含“nil”则中止执行/**
payPalDriver.viewControllerPresentingDelegate = self
payPalDriver.appSwitchDelegate = self // Optional
// Specify the transaction amount here. "2.32" is used in this example.
let request = BTPayPalRequest(amount: "2.32")
request.currencyCode = "USD" // Optional; see BTPayPalRequest.h for more options
payPalDriver.requestOneTimePayment(request) { (tokenizedPayPalAccount, error) in
if let tokenizedPayPalAccount = tokenizedPayPalAccount {
print("Got a nonce: \(tokenizedPayPalAccount.nonce)")
// Access additional information
let email = tokenizedPayPalAccount.email
let firstName = tokenizedPayPalAccount.firstName
let lastName = tokenizedPayPalAccount.lastName
let phone = tokenizedPayPalAccount.phone
// See BTPostalAddress.h for details
let billingAddress = tokenizedPayPalAccount.billingAddress
let shippingAddress = tokenizedPayPalAccount.shippingAddress
} else if let error = error {
// Handle error here...
} else {
// Buyer canceled payment approval
}
}
}
}
extension MyViewController:BTViewControllerPresentingDelegate{
func paymentDriver(_ driver: Any, requestsPresentationOf viewController: UIViewController) {
}
func paymentDriver(_ driver: Any, requestsDismissalOf viewController: UIViewController) {
}
}
extension MyViewController: BTAppSwitchDelegate {
func appSwitcherWillPerformAppSwitch(_ appSwitcher: Any) {
}
func appSwitcher(_ appSwitcher: Any, didPerformSwitchTo target: BTAppSwitchTarget) {
}
func appSwitcherWillProcessPaymentInfo(_ appSwitcher: Any) {
}
}
当我遵循错误中提到的建议时,它会显示更多错误。如何解决?
【问题讨论】:
-
如果不展示如何在 IBAction 中修改代码,很难说。
-
错误很明显,您不能将可选值传递给该函数。您需要先使用
if let或guard let解开它 -
当我添加感叹号时,构建成功 - “让 payPalDriver = BTPayPalDriver(apiClient:braintreeClient!)”,但是当我按下按钮时它显示错误 i) 线程 1:致命错误:意外在展开 Optional 值时发现 nil。 ii) xcode 控制台显示错误 - “[BraintreeSDK] 错误 BTClient 无法初始化,因为提供的 clientToken 无效”
标签: ios swift xcode paypal-sandbox