代码之家  ›  专栏  ›  技术社区  ›  el_quick

条纹iOS:我无法使用卡id创建收费

  •  0
  • el_quick  · 技术社区  · 6 年前

    我正在尝试使用此处描述的标准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对象获取令牌? 或者其他解决方案?

    当做。

    1 回复  |  直到 6 年前
        1
  •  2
  •   el_quick    6 年前

    加油!当您使用一个卡\u id而不是一个令牌时,必须将客户id作为参数传递,因此,我修改了api:

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

    (我将Stripe customer id作为Stripe\u customer\u id存储在我的users表中)