我正在使用Paypal Braintree的官方教程进行支付网关。
我按照教程还是会出错。
我的视图控制器代码有错误-
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) {
}
}
当我遵循错误中提到的建议时,它显示出进一步的错误。如何解决?
我在这里犯了两个愚蠢的错误-i( 当我添加一个感叹号时,构建成功了-";let payPalDriver=BTPayPalDriver(apiClient:braintreeClient!(";ii(xcode控制台显示错误-";[BraintreeSDK]错误BTClient无法初始化,因为提供的clientToken无效"-我没有做的是在密钥中留下一个空白区——我已经做了——brainstreeClient=BTAPIClient(授权:"sandbox_bsdfsfdfsg59_5dfgxr37xvb54mg8sp"(,即密钥后面的空白区。我纠正了它,即-braintreeClient=BTAPIClient(授权:"sandbox_bsdfsfdfsg59_5dfgxr37xvb54mg8sp"(-没有空格,一切都很顺利。