【问题标题】:How to add a card to a user and charge it using Stripe?如何向用户添加卡并使用 Stripe 对其进行收费?
【发布时间】:2016-03-18 01:12:36
【问题描述】:

我有一个页面,用户输入抄送并被收费。

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

添加我正在使用php的cc

$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 也很棒。

【问题讨论】:

    标签: javascript php stripe-payments credit-card


    【解决方案1】:

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

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

    条带标记只能使用一次,但这并不意味着您必须 为每次付款请求您客户的卡详细信息。条纹 提供了一个 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-customer

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 2021-06-03
      • 2019-03-27
      • 2017-06-17
      • 2020-08-14
      • 2015-03-13
      • 2012-04-19
      • 1970-01-01
      相关资源
      最近更新 更多