如何使用Stripe.com为客户的特定卡充电

时间:2013-11-27 15:22:39

标签: ruby-on-rails stripe-payments

客户对象可以在Stripe.com中拥有多张卡片。你如何收取现有的卡?

我已经尝试了一些东西,但条纹api出于某种原因会在何时获得旧的客户令牌和新的卡片令牌,而不是仅仅在该客户上创建新卡片。所以我走了检索所有客户卡的路线,然后通过单选按钮选择一个,然后将所选卡的令牌提交到收费

      charge = Stripe::Charge.create(
        :amount => "#{@subscription.price}",
        :currency => "usd",
        :card => params[:existing_card_id],
        :description => "Subscription for #{current_user.email}"
      )

但我收到了错误

Stripe::InvalidRequestError: Invalid token id: card_24j3i2390s9df

2 个答案:

答案 0 :(得分:43)

我想出来了。

使用现有的卡片令牌,您还必须发送客户令牌

     charge = Stripe::Charge.create(
        :amount => "#{@subscription.price}",
        :currency => "usd",
        :customer => current_user.stripe_customer_id,
        :card => params[:existing_card_id],
        :description => "Subscription for #{current_user.email}"
      )

答案 1 :(得分:8)

这个答案有助于我在PHP中应用相同的解决方案,以便为默认信用卡以外的特定信用卡客户收费。这里是件:

JSON:

{
  "charge": {
    "amount":122,
    "currency":"usd",
    "customer":"cus_7hVsgytCc79BL7",
    "card":"card_17SENFJ4sx1xVfmD8skoSjNs"
  }
}

PHP

    $item = $params->request->getJsonRawBody();
    $new_charge = \Stripe\Charge::create(array(
      "amount" => $this->ConvertAmount($item->charge->amount),
      "currency" => $item->charge->currency,
      "customer" => $item->charge->customer,
      "card" => $item->charge->card
    ));
    return $new_charge;