javascript - 如何使用 Stripe 向用户添加卡并进行收费?

标签 javascript php stripe-payments credit-card

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

我使用js创建了一个卡 token

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
});

要添加我正在使用 php 的抄送

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

为了给 cc 充电,我也使用 php

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

完成这一切后,我得到您不能多次使用 Stripe token

有什么想法可以将抄送保存给该用户$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, // $15.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 有优秀的文档,请阅读它!

关于javascript - 如何使用 Stripe 向用户添加卡并进行收费?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235127/

相关文章:

javascript - 使用 jquery 或 PHP 捕获当前屏幕的正确图像或将 div 转换为 pdf

javascript - 类型 'update' 上不存在属性 'Observable<{}>'。火力地堡 5 Angular 火 2 5

php - JavaScript/jQuery 和 PHP Ajax 强制报错

php - 如何从 Twitter 主题标签中删除#?

php - mysqli_real_connect() : (HY000/2002): No such file or directory

php - Stripe支付网关实现

javascript - Angular 1.4.8 错误 : [$injector:modulerr]

php - 检测匹配的高效算法

stripe-payments - 如何按 strip api 拆分付款?

javascript - 3D Secure 的 Stripe 支付意图仍然不完整