为银行帐户信息创建令牌条带

时间:2019-01-22 03:43:24

标签: ios swift stripe-payments

我正在尝试创建一个iOS应用程序,该应用程序使用Stripe安全地收集用户的银行帐户信息(目的是向用户付款)。 Stripe建议我在STPBankAccountParams的实例中收集银行信息。这还不错:

var bankAccount = STPBankAccountParams()
bankAccount.routingNumber = routingNumber
bankAccount.accountNumber = accountNumber
...

条带然后建议出于安全目的将bankAccount标记化,然后再发送到后端。他们建议您使用此功能:

func createToken(withBankAccount bankAccount: STPBankAccountParams, completion: STPTokenCompletionBlock? = nil)

有关此功能的文档有点稀疏:Docs

我不确定如何在我的代码中运行此功能。我想使用此功能并获取令牌,但是我对如何在代码中做到这一点缺乏了解。我想运行类似的东西:

token = createToken(withBankAccount: bankAccount)

但是,当然,我尝试过的其他方法尚未奏效。有没有人有在Stripe中运行createTokenWithBankAccount()函数的经验?

2 个答案:

答案 0 :(得分:0)

STPTokenCompletionBlock是闭包(或回调),当函数完成时(执行的任务),它将调用此块,并向您传递STPTokenError。您将使用类似的

createToken(withBankAccount: bankAccount) { (token, error) in
    // your code to check the token and error
}

这是一个非常常见的模式,我建议您看看类似Swift Programming Language: Closures

答案 1 :(得分:0)

MadProgrammer's answer was very close, but did not actually work. I did talk with a representative from Stripe. For reference, he recommended the following code, which seems to work:

STPAPIClient.shared().createToken(withBankAccount: bankAccount) { (token, error) in
        if let error = error {
            print(error)
        }
        else {
            print(token)
        }
    }