我正在尝试使用此处描述的标准iOS集成创建费用:
https://stripe.com/docs/mobile/ios/standard
func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPErrorBlock) {
StripeClient.shared.completeCharge(paymentResult, amount: 1000, shippingAddress: nil, shippingMethod: nil, completion: { (error: Error?) in
if let error = error {
completion(error)
} else {
completion(nil)
}
})
}
在我的StripeClient.swift文件
func completeCharge(_ result: STPPaymentResult,
amount: Int,
shippingAddress: STPAddress?,
shippingMethod: PKShippingMethod?,
completion: @escaping STPErrorBlock) {
let url = self.baseURL.appendingPathComponent("charge")
var params: [String: Any] = [
"source": result.source.stripeID,
"amount": amount,
"description": Purchase.shared.description()
]
params["shipping"] = STPAddress.shippingInfoForCharge(with: shippingAddress, shippingMethod: shippingMethod)
Alamofire.request(url, method: .post, parameters: params, headers: Credentials.headersDictionary())
.validate(statusCode: 200..<300)
.responseString { response in
switch response.result {
case .success:
completion(nil)
case .failure(let error):
completion(error)
}
}
}
def charge
Stripe::Charge.create(charge_params)
render json: { success: true }, status: :ok
rescue Stripe::StripeError => e
render json: { error: "Error creating charge: #{e.message}" },
status: :payment_required
end
private
def charge_params
params
.permit(:amount, :description, :source)
.merge(currency: 'gbp')
end
问题出在completeCharge方法中,结果.source.stripeID正在返回卡id(card_xxxxxx),但我需要一个令牌(tok_xxxxxx)。所以,
如何从card id或STPPaymentResult对象获取令牌?
或者其他解决方案?
当做。