簡體   English   中英

Stripe iOS:我無法使用卡ID進行收費

[英]Stripe iOS: I'm not able to create charge using card id

我正在嘗試使用此處描述的標准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代替令牌? 或其他解決方案?

問候。

加油! 當您使用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存儲在我的用戶表中)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM