How to save card details in stripe payment gateway and use card details for next payment for particular user who entered the card details before
thank you
Before saving card you need to create customer first like below
Right now I am giving you an example with PHP language.
Creating customer :
$customer = \Stripe\Customer::create([
'name' => 'Test User',
'email' => 'cardtestuser#gmail.com',
'description' => 'My First Test Customer',
]);
You can refer the link for more detail : https://stripe.com/docs/api/customers/create
It will return the object with customer id e.g : cust_**** that you need to keep with you for later used else you can get it from Stripe dashboard.
Now we need to generate token for the card that we need to add to the customer that recently created in stripe
$token = \Stripe\Token::create([
'card' => [
'number' => '4242424242424242',
'exp_month' => 3,
'exp_year' => 2021,
'cvc' => '314',
],
]);
Above code return the card token e.g tok_*** that you need to use to save card to the customer
Here is the code to create new card to customer
\Stripe\Customer::createSource(
$customer->id,
['source' => $token->id]
);
You can save customeId(cust_###) and cardId(card_###) in database or somewhere else so you can use it later while making payment from card
Hope this will help you
Related
I want to build the process below
When a client presses the Book button, we need to place a hold on the credit card for the amount of the transaction
I think the hold does not charge the card, ie the money does not leave the card
The hold just checks that the money is available and reserves it for a few days
Within these 7 days, I call another API to complete the charge, to cancel the hold, or just let the hold expire.
If a client books an appointment, the money goes on hold and then, after a day or so, he cancels, the hold should be released
I checked the Stripe API doc, but I am not sure how to do this process.
I think it must to using the paymentIntent API.
Please explain to me what process I must do.
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET_KEY'));
$getPaymentMethod = $stripe->paymentMethods->all([
'customer' => $request->customerId,
'type' => 'card',
]);
$createIntent = $stripe->paymentIntents->create([
'amount' => $request->price * 100,
'currency' => 'usd',
'payment_method' => $getPaymentMethod->data[0]->id,
'payment_method_types' => [$request->paymentType],
'customer' => $request->customerId,
'capture_method' => 'manual',
'description' => 'The payment of the appointment',
]);
$confirmPaymentIntent = $stripe->paymentIntents->confirm(
$createIntent->id,
['payment_method' => $getPaymentMethod->data[0]->id]
);
$captureIntent = $stripe->paymentIntents->retrieve($confirmPaymentIntent->id);
$results = $captureIntent->capture($confirmPaymentIntent->id);
return $results;
I tried this code so it is working and the payment capture.
But it didn't charge the money on the stripe and the balance is $0.00.
I am not sure it is correct and how to do next step for complete.
Please help me. what should I?
I am using ngx-stripe (frontend) and stripe-php (backend) and trying to create a Subscription for a Customer
I have successfully implemented a card element on the frontend, which calls stripe-php to create PaymentIntent. Before the PaymentIntent is created, I use the submitted information to create a Customer
Checking the successful payment in Stripe I can see the created customer attached to the payment and the payment has a payment method but if I check the customer, it shows as no payment method attached so I cannot create a Subscription for the customer
Create customer:
$this->stripeClient->customers->create([
'name' => $customer->getName(),
'description' => $customer->getDescription(),
'email' => $customer->getEmail()
]);
Create payment intent:
$paymentIntent = $this->stripeClient->paymentIntents->create([
'customer' => $customerId,
'amount' => $amount,
'currency' => $currency,
'payment_method_types' => ['card']
]);
Which Stripe API methods(s) do I need to call to instruct Stripe to create a payment method for the customer using the submitted details?
If you’re using stripe.confirmCardPayment [0] to confirm the PaymentIntent, you can include setup_future_usage = off_session [1].
stripe.confirmCardPayment(clientSecret, {
setup_future_usage : 'off_session',
payment_method: {
card: card,
billing_details: {
name: 'Jenny Rosen'
}
}
...
Including the setup_future_usage parameter will attach the payment method to the Customer, if present, after the PaymentIntent is confirmed and any required actions from the user are complete.
off_session indicates that your customer may or may not be present in your checkout flow. This is especially important for Subscriptions where your Customers are usually not available to make recurring payments on-session.
However, note that you don't need to create a PaymentIntent for a Subscription. You could save the card on the Customer for future use [2], then create the Subscription by passing in the Customer [3] and default payment method [4].
[0]https://stripe.com/docs/js/payment_intents/confirm_card_payment
[1]https://stripe.com/docs/js/payment_intents/confirm_card_payment#stripe_confirm_card_payment-data-setup_future_usage
[2]https://stripe.com/docs/payments/save-and-reuse
[3]https://stripe.com/docs/api/subscriptions/create#create_subscription-customer
[4]https://stripe.com/docs/api/subscriptions/create#create_subscription-default_payment_method
I'm currently trying to create charges with the server side stripe api.
But i'm facing a problem; I'm using two ways to proceed with the paiement:
either the user can pay using the stripe element => I'm therefore using the generated token ('tok_somethingId') to effectuate the paiement
or, if the user already added some cards on is account he can select on of them in a list => The server then use the card id ('card_somethingId') and the customer id ('cus_smoethingId')
I was wondering if there was a way to generate a token with a card id and a customer id in order to use this token to create the charge instead of using the card id and the customer id to charge the user.
I already tried with https://stripe.com/docs/api/node#create_card_token but it doesn't seem to work.
stripe.tokens.create({
card: cardId,
customer: customerId
}, function(err, token) {
// do smthg here with the token
});
This give me this error message
If you are doing payment with stored card then no need to get token,
1- Create customer on stripe
$customer = \Stripe\Customer::create([
'email' => $customer_email,
]);
$response = ['status' => 'success', 'response' => $customer];
when you have created customer then you have customer_id
if(isset($response ['response']['id']))
$response ['customer_id'] = $response ['response']['id'];
2 - you can add card on stripe by the customer id and card token
$customer = \Stripe\Customer::retrieve($customer_id);
$creditCard = $customer->sources->create(array("source" => $cardToken));
$response = ['status' => 'success', 'response' => $creditCard];
Now you have card id like this
"id": "card_1D4plsDExLRkbD8k1UWdqwIr"
3- you can store multiple cards on customer and also can have retreive
$cards = \Stripe\Customer::retrieve($customer_id)->sources->all(array(
"object" => "card"
));
4 -you can payment via card card that is stored on customer account
$params = [
'currency' => 'USD',
'amount' => $total_amount * 100,
// converting dollars to cents
'description' => $description, //it may be blank
'customer' => $customer_id,
"card" => $card_id'
];
$transaction = \Stripe\Charge::create($params);
$response = ['status' => 'success', 'response' => $transaction['id']];
Here we are not using 'source' parameter because it is used when we are payment via card token.
You can not create a new token for an existing card as this would not make sense. The card is already saved on the customer and you can charge it.
The easiest solution here is likely to do the reverse and save the card on a customer when you get a token tok_XXXX. This way, you always charge a card by passing the customer parameter as the customer id cus_XXXX and the source parameter as the card id card_XXXX.
Otherwise, you need to handle this dynamically so that you know if you are getting a token (tok_XXXX) or a card id (card_XXXX) and pass different parameters based on that decision. Your front-end code should know which case you ended up in.
just pass the values in the body,
[{"key":"amount","value":"1000","description":""},{"key":"currency","value":"usd","description":""},{"key":"customer","value":"cus_ID","description":""},{"key":"description","value":"\"Charge for jenny.rosen#example.com\"","description":""},{"key":"card","value":"card_ID","description":""}]
this working for me
I am using stripe.js with php and I am having issue in creating token for existing/saved cards and for new card everything works fine. Is there any function to generate token based on cardId and customerId
It is neither necessary nor possible to create a new token from an existing Customer or Card object. Once a card has been stored on a Customer object, you can complete a charge using only your secret key and the Customer ID. From the docs:
// 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("sk_test_BQokikJOvBiI2HlWgH4olfQ2");
// Token is created using Stripe.js or Checkout!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];
// Create a Customer:
$customer = \Stripe\Customer::create(array(
"email" => "paying.user#example.com",
"source" => $token,
));
// Charge the Customer instead of the card:
$charge = \Stripe\Charge::create(array(
"amount" => 1000,
"currency" => "usd",
"customer" => $customer->id
));
// 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.
$charge = \Stripe\Charge::create(array(
"amount" => 1500, // $15.00 this time
"currency" => "usd",
"customer" => $customer_id
));
If you have any other questions about how to implement a particular payment flow on Stripe I'd recommend getting in touch with Stripe support.
I'm using embed stripe.js
Upon form completion, the form redirects to: www.mydomain.com/doc-whatever/?action=payment
This url param fires the serverside code. Abbreviated pseudo code:
$token = $_POST['stripeToken'];
//check if customer ID exists in the DB.
//returns the stripe customer id of client attached to the document.
//May or may not be logged in.
$customers_stripe_id = get_customer_stripe_id()
if ( ! $customers_stripe_id ) {
$customer = \Stripe\Customer::create(array(
'email' => 'customer#example.com',
'source' => $token
));
$customers_stripe_id = $customer_id;
}
$charge = \Stripe\Charge::create(array(
'customer' => $customers_stripe_id,
'amount' => 5000,
'currency' => 'usd'
));
This works just fine, however there is an obvious security issue here. One can simply navigate to www.mydomain.com/doc-whatever/?action=payment and if there is stripe customer id stored in the database, the card customer will be charged.
Your server-side code should be authorized then, so that for example only a logged in customer himself or an admin could fire that action.