如何向用户添加卡并使用Stripe进行充电

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

本文关键字:Stripe 用户 添加      更新时间:2023-09-26

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

我使用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]);

为了给cc充电,我使用php以及

$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

保存信用卡详细信息以备日后使用

条纹代币只能使用一次,但这并不意味着你必须要求您的客户每次付款的卡详细信息。条纹提供了一个Customer对象类型,使保存该对象变得容易--并且其他——供以后使用的信息。

创建一个新客户,而不是立即向卡收费,在该过程中将令牌保存在Customer上。这会让你在未来的任何时候向客户收费:

(以下是多种语言的示例)。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, // $15.00 this time
  "currency" => "usd",
  "customer" => $customerId // Previously stored, then retrieved
  ));

在Stripe中使用存储的支付方式创建客户后,您可以在任何时间点通过路过客户向该客户收费收费请求中的ID,而不是卡片表示。要确定将客户ID存储在您的一侧以备日后使用。

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

Stripe有优秀的文档,请阅读!