我正在尝试使用此处描述的标准iOS集成创建费用:https://stripe.com/docs/mobile/ios/standard
要做到这一点,我在CheckoutController.swift中
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)
}
}
}
而且,在我的API(Ruby on Rails)中
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方法中,result.source.stripeID返回卡ID(card_xxxxxx),但我需要令牌(tok_xxxxxx)。所以,
如何从卡ID或STPPaymentResult对象获取令牌? 或如何让我的Rails API使用卡ID代替令牌? 或任何其他解决方案?
致谢。
答案 0 :(得分:2)
去!当您使用card_id代替令牌时,有必要将客户ID作为参数传递,因此,我修改了api:
def charge_params
params
.permit(:amount, :description, :source)
.merge(currency: 'gbp',
customer: current_user.stripe_customer_id)
end
(我将Stripe客户ID作为stripe_customer_id存储在我的用户表中)