2015-12-12 26 views
1

我有一個頁面,用戶輸入cc並獲得收費。如何將卡添加到用戶並使用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 
}); 

添加我使用PHP

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

充電,並抄送我使用PHP以及

$stripeCharge = Stripe_Charge::create([ 
    'source'  => $token, 
    'amount'  => $amount 
]); 
的CC卡令牌

做所有這一切,我得到You cannot use a Stripe token more than once

任何想法如何將cc保存到此用戶$stripeUserId並收取費用。

PHP是受歡迎的,但js也很棒。

回答

0

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

條紋與存儲的付款方式創建一個客戶後,您 可以是客戶在任何時候通過將客戶 ID,而不是卡的表示,在充電時間收費請求。確定 將客戶ID存儲在您身邊供以後使用。

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

條紋具有出色的文檔,請閱讀!

+0

謝謝,我想我剛剛在某個時候感到困惑 – Patrioticcow

相關問題