【问题标题】:Stripe iOS: I'm not able to create charge using card idStripe iOS:我无法使用卡 ID 创建费用
【发布时间】:2019-03-16 00:43:06
【问题描述】:

我正在尝试使用此处描述的标准 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 代替令牌? 或任何其他解决方案?

问候。

【问题讨论】:

    标签: ios swift stripe-payments


    【解决方案1】:

    去吧!当您使用 card_id 而不是令牌时,必须将客户 ID 作为参数传递,所以,我修改了我的 api:

      def charge_params
        params
          .permit(:amount, :description, :source)
          .merge(currency: 'gbp',
                 customer: current_user.stripe_customer_id)
      end
    

    (我将 Stripe 客户 ID 存储为我的 users 表中的 stripe_customer_id)

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2013-08-22
      • 2019-06-06
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2015-12-10
      • 2020-04-23
      • 2017-04-24
      相关资源
      最近更新 更多