Reputation: 30388
I'm implementing Stripe in my ASP.NET Core app using Checkout
.
I know how to get a token for charging a credit card using Checkout
but where do I get the token to create a customer?
In the documentation, I see that I need to get a token to create a customer but not sure where that token comes from. https://stripe.com/docs/api/dotnet#create_customer
As far as I know, a token can be used only once so it cannot be the same token I get before charging a credit card.
Upvotes: 5
Views: 7985
Reputation: 21
\Stripe\Stripe::setApiKey("----");
\Stripe\Stripe::setApiKey(".................");
$token= \Stripe\Token::create(array(
"card" => array(
"number" => "4242424242424242",
"exp_month" => 1,
"exp_year" => 2019,
"cvc" => "314"
)
));
$request['stripe_token'] =$token['id'];
// When Contact person have not Stripe Customer id then we have to do the following process.
try {
$customer = \Stripe\Customer::create([
"description" => "Customer for ".$contactDetails['email'],
"source" => $request['stripe_token'] // obtained with Stripe.js
]);
// update its customerid in the contact table
// Create Customer then save its id in table and use the customer id when you are verifiying the customer token
$result= \Stripe\Charge::create(array(
"amount" => $request['amount'],
"currency" => $request['currency'],
"customer" => $customer
));
$status = $result['succeeded'];
if($result['status'] == "succeeded"){
$success = 'Your payment was successful.';
$all['payment_done'] = "1";
$FinalArray = array('status'=>'true','message'=>'Your payment done successful.','result'=>$all);
}else{
$FinalArray = array('status'=>'fail','message'=>'Your Token is not generated successfully','result'=>[]);
}
}
catch (Exception $e) {
$error = $e->getMessage();
$all['payment_done'] = "0";
$FinalArray = array('status'=>'false','message'=>'The Stripe token id is not correctly','result'=>$all);
}
Upvotes: 1
Reputation: 1431
As I am referencing here from stripe document
When you collect a customer's payment information, a Stripe token is created. This token 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 that makes it easy to save this—and other—information for later use. You can use Customer objects for creating subscriptions or future one-off charges.
What you have to exactly do is Create a customer you have got while taking card details from the customer and charge that customer.
Do it using following code snippet, in this way you will create a customer and charge using a single token
StripeConfiguration.SetApiKey(secret_key_of_your_account);
var token = model.Token; // Using ASP.NET MVC
var customers = new StripeCustomerService();
var charges = new StripeChargeService();
var customer = customers.Create(new StripeCustomerCreateOptions {
Email = "[email protected]",
SourceToken = token
});
// YOUR CODE: Save the customer ID and other info in a database for later.
// YOUR CODE (LATER): When it's time to charge the customer again, retrieve the customer ID.
var charge = charges.Create(new StripeChargeCreateOptions {
Amount = 1500, // $15.00 this time
Currency = "usd",
CustomerId = customer.Id
});
read the referenced document for more details
Upvotes: 4