gpt4 book ai didi

ios - Stripe iOS : I'm not able to create charge using card id

转载 作者:搜寻专家 更新时间:2023-11-01 06:57:19 24 4
gpt4 key购买 nike

我正在尝试使用此处描述的标准 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),但我需要一个 token (tok_xxxxxx)。所以,

如何从卡 ID 或 STPPaymentResult 对象中获取 token ?或者如何让我的 Rails API 使用卡 ID 代替 token ?或任何其他解决方案?

问候。

最佳答案

加油!当您使用 card_id 而不是 token 时,必须将客户 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 存储在我的用户表中)

关于ios - Stripe iOS : I'm not able to create charge using card id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52751486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com