如何为用户添加卡并使用 Stripe 对其进行收费?

How to add a card to a user and charge it using Stripe?

我有一个页面,用户在其中输入抄送并收取费用。

我使用 js 创建了一个卡片令牌

Stripe.card.createToken(ccData, function stripeResponseHandler(status, response) { 
    var token = response.id;

    // add the cc info to the user using
    // charge the cc for an amount
});

添加我正在使用的 cc php

$stripeResp = Stripe_Customer::retrieve($stripeUserId);
$stripeResp->sources->create(['source' => $cardToken]);

我也在用 php 给 cc 充电

$stripeCharge = Stripe_Charge::create([
    'source'      => $token,
    'amount'      => $amount
]);

完成所有这些我得到 You cannot use a Stripe token more than once

关于如何将抄送保存给该用户 $stripeUserId 并对其收费的任何想法。

PHP欢迎,js也很棒

https://stripe.com/docs/tutorials/charges

Saving credit card details for later

Stripe tokens can only be used once, but that doesn't mean you have to request your customer's card details for every payment. Stripe provides a Customer object type that makes it easy to save this—and other—information for later use.

Instead of charging the card immediately, create a new Customer, saving the token on the Customer in the process. This will let you charge the customer at any point in the future:

(示例以多种语言显示)。 PHP版本:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("yourkey");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "description" => "Example customer")
);

// Charge the Customer instead of the card
\Stripe\Charge::create(array(
  "amount" => 1000, // amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id)
);

// YOUR CODE: Save the customer ID and other info in a database for later!

// YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

\Stripe\Charge::create(array(
  "amount"   => 1500, // .00 this time
  "currency" => "usd",
  "customer" => $customerId // Previously stored, then retrieved
  ));

After creating a customer in Stripe with a stored payment method, you can charge that customer at any point in time by passing the customer ID—instead of a card representation—in the charge request. Be certain to store the customer ID on your side for later use.

更多信息请访问 https://stripe.com/docs/api#create_charge-customer

Stripe 有很好的文档,请阅读!