How do you permanently delete a payment method from Stripe? - stripe-payments

Is it possible to delete a payment method from Stripe? I can't seem to find that functionality described anywhere in the docs. You can create a payment method, attach it to a customer, and detach it from that customer, but how do you delete the payment method from Stripe's system entirely?
If you can't, then that means once you send your credit card info to Stripe, you can't ever take it back...

I think the good way to do so is to detach the paymentMethod from a customer:
const paymentMethod = await stripe.paymentMethods.detach(
'pm_xxx'
);
Api reference
Like #Rasmus Christoffer Nielsen said in comments, "the payment method cannot be attached to any customer again thus serving the purpose of deletion"

you can scan the list:
get the customer lists by type cards:
cards = stripe.Customer.list_payment_methods(customer_id, type="card")
then loop and find the same fingerprint used on the client card to be removed. Remember fingerprint can be retrieved on payment_method;
for card in cards['data']:
card['fingerprint'] == removable_card:
stripe.Customer.delete_source(customer_id, card['id'])
that should be all

Related

How to use stripe checkout for first come, first served buying experience

I'm trying to use Stripe checkout in a first-come, first-served buying process. Multiple buyers may be trying to buy the same item at the same time, and only the one who completes the stripe checkout process first should get it. At the moment, the stripe checkout session duration requires me to 'book' the stock item and only release them back into stock once the session duration expires (even if they close the tab).
Is there a way to set up Stripe Checkout in a way that would detect whether the item has already been purchased by another buyer (e.g. the stock is no longer available), and for example show an error when the user tries to pay?
If not, any suggestions as to alternative ways of implementing this functionality while still using Stripe?
You can listen for checkout.session.completed events and add some event handler logic to retrieve the Session object while expanding the line_items:
https://stripe.com/docs/payments/checkout/fulfill-orders
https://stripe.com/docs/api/expanding_objects
This will allow you to inspect the price and product IDs for the completed Session. You could then have some logic to expire any other Checkout sessions so no other customers are able to go through the payment flow:
https://stripe.com/docs/payments/checkout/managing-limited-inventory
https://stripe.com/docs/api/checkout/sessions/expire
You can use the paymentIntent that allows you to confirm or reject the payment later. You can create the paymentIntent for all the customers are trying at the same time and next take the first one and confirm only this paymentIntent and reject the others
Read the Stripe documentation:
Stripe | PaymentIntent

Is it possible to get card brand from payment element

So I am using Stripe-Js Payment elements and need to get the card brand (e.g visa, mastercard)
But it seems that brand is only available on cardElement's onChange. I only get the payment method type when onChange is triggered
paymentElement.on('change', function(event) {
// event.brand
});
Now I know I could get the brand details if I do confirmPayment but I need this brand first before doing a payment due to some business-related logic that needs to be done before placing the payment.
Is there a way to get the card brand of what user entered before doing confirmPayment using Payment Elements?
Thanks a lot
Payment Element does not return card brand in any event of StripeJS. This is only possible in Card Element with onChange event as you mentioned.
Another possible way in Payment Element is to go with server-side confirmation instead of using client-side confirmation. At step 6 of the doc, make an extra call to PaymentMethod Retrieval API to get card information including card brands from the PaymentMethod ID (pm_xxx) in the response of stripe.updatePaymentIntent. After checking the card brand, then make a PaymentIntent confirmation call at the server.
Please note that sign up is required to use server-side confirmation since it is currently in beta.

Get purchased product from PaymentIntent

in my app i want to list past purchases for a stripe customer. So I was looking at the relevant API objects like PaymentIntents, Sessions or Charges. But they all do not seem to contain any reference to Product or Price, which I would need to list the purchased products.
Subscriptions contain a list of items that are contained in that subscription, so I was expecting PaymentIntents to have something like that too.
Does anyone has an idea how to archive my list of past purchases? Thanks!
I did some digging through the Stripe API docs[1] and, out of the three objects you referrenced (PaymentIntent, Session, Charges), the only one that I can see being able to trace back to a product is the Session.
Session objects have a line_items property[2] which can be followed all the way down to line_items.data.price.product[3]. To access this you’ll need to inlcude the expand=["data.line_items"] parameter to your call to list Checkout Sessions. You can read more about expanding API responses here[4]
So for all the charges to your customers that were done using Checkout Sessions, you could list them all, use the customer property to associate earch session with a customer in your application, traverse the the returned data, and then query the API for the product details. If you have a lot of customers & products these API calls will add up fast so I would store this data in your back-end to avoid hitting rate limits[5].
Alternatively, you could just save the product ID (either Stripe or your local version) as metadata[6] for any of the above Stripe payment objects listed. That would allow you to link any payment object you wish to a product.
https://stripe.com/docs/api
https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items
https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items-data-price-product
https://stripe.com/docs/expand
https://stripe.com/docs/rate-limits
https://stripe.com/docs/api/metadata
I had the same question I found a way to retrieve the products related to a specific PaymentIntent.
You need to get the sessions that was linked to PaymentIntent when the Checkout process was made.
I will give you the code (in PHP) that I use to to this.
\Stripe\Stripe::setApiKey(STRIPE_API_SECRET);
// Find the Session for that PaymentIntent
$sessions = \Stripe\Checkout\Session::all([
'payment_intent' => "pi_xxxxxxxxxx",
'expand' => ['data.line_items'],
]);
You will then have an key line_items that contain the products linked.
Enjoy

If you only accept credit card payments, are there any functions that can not be done with src_xxxx but only with tok_xxxx?

Can all the functions that can be done with tok_xxxx be done with src_xxxx?
Even if you specify tok_xxxx or src_xxxx, it will be used by linking to the Customer object, so in the end is not the same thing?
Is checkout.js the only feature that can not be done with 'src_xxxx'?
The following PHP code works as expected, but is it better to use 'src_xxxx'?
\Stripe\Stripe::setApiKey("sk_test_xxxx");
\Stripe\Customer::create([
"source" => $_POST['stripeToken'],
'email' => $_POST['stripeEmail'],
]);
\Stripe\Charge::create(array(
"amount" => 777,
"customer" => $customer->id,
"currency" => "USD",
));
If you would like to make a one-time payment, is it better to use 'tok_xxxx'?
If you want to associate a customer with a one-time payment, is it better to 'src_xxxx'?
Isn't it recommended to implement everything with 'src_xxxx', as it is cumbersome?
Here is some background on Sources/Token in Stripe's World. 
Source in Stripe's concept is a payment method such as Credit Card, Bank Transfer, ATH, Alipay, WeChat Pay etc. 
And for "Credit Card" payment, Token and Source can be safely used interchangeably. Basically it is a secure and PCI compliant way to pass Credit Card information between systems. 
Source/Token are one-time-use only, this one time use can be a "Charge" or "Attach to Customer". Once you've charged the source or attach the source to a customer, the source/token will become "consumed". 
When you use "consumed" source again such as charge the source or attach the source to the customer, you will see "invalid_request_error" .
So how does Stripe do recurring charges allowing user using stored card? 
The high level workflow will be 
Create a source -> Attach the source to the customer -> Charge the customer with the source id AND customer id
meaning you will need to save/attach the source to the customer to make the source "reusable". And it is required that you need to use the Source ID together with Customer ID. And if you do not provide source id and use only Customer ID, the default source of the customer will be used.
Saying all that, to your question:
If you would like to make a one-time payment, is it better to use 'tok_xxxx'?
You could use either token or source, you could create source or token using stripe Checkout or Elements.
If you want to associate a customer with a one-time payment, is it better to 'src_xxxx'?
Yes, you will have to save a token/source to the customer. When you charge the customer, the transaction will appear under the customer
Isn't it recommended to implement everything with 'src_xxxx', as it is cumbersome?
If you are using non-credit card payment such as ACH, Alipay and 3DS etc, source is required. But for normal credit card, it does not have any difference between token and source.

Stripe for one time payment. - Ruby on Rails

Can I use Stripe for receiving a one-time payment instead of recurring payments?
If so what settings do I have to make?
Yes, just use the Stripe::Charge.create method. As the payment method, you can either pass in a card directly or reference a Customer whose card should be charged.
You can receive one-time payments via the Stripe dashboard. Go to:
https://dashboard.stripe.com/payments
and click the button for a "+New" payment. That will let you manually enter the cc info. I do this with my Pairing as a Service clients all the time... they just read me their card info, and I enter it with the amount I'm charging them and a description. Your account password is then required to complete the charge.
It's fast and easy... I just wish there was a way to let them enter the info directly. Maybe I'll make something like that...
Yes, You can just create a Stripe account and use a simple example of Stripe payment for Ruby on Rails I've made recently. Check the code: https://github.com/ab00zar/StripePayment-example
Run the server using your test keys like:
PUBLISHABLE_KEY=pk_test_g0XSu8r2ugAETksVJQXJjU30
SECRET_KEY=sk_test_A3rwGLOMNxEKDkaJOTgi4frd rails s

Resources