How to tell if a customer's default payment source is a bank or card in Stripe? - stripe-payments

My customers use the Stripe Customer portal to enter their default payment method, which can be a card or bank account. I'm having trouble figuring out which one they chose. I'm using the api.stripe.com/v1/customers API and the "default_source" object will return a card when the payment method is a card but when it is a bank I'm getting null. I just need to know whether they are using card or bank account so I can get the workflow going from there
Call looks like this:
curl https://api.stripe.com/v1/customers/cus_Ia2m9sl1Na1j1E -u sk_test_51Hx2pbGbd.. -d expand[] default_source -d expand[] sources -d expand[] subscriptions

You can retrieve the Source object. Its type tells you what kind of payment method this source object represents.

Related

Stripe webhooks to monitor whether customer has an active payment method

Customers on my site use the Stripe Billing portal on user sign up to enter a payment method (card or ACH). What webhook can I use to monitor whether they have at least one active payment method?
I know I can use customer.source.created when a payment method is created and customer.source.deleted when a source is deleted. However a user can initially create two methods and delete one but still have an active method. I just need to know whether they have at least one available
Whenever you receive customer.source.created OR customer.source.deleted events, you typically receive card object as the payload. Since the customer property on the card object is expandable, you may not see it included by default on the card object.
In which case, you'd likely need to retrieve the card object again by calling the API and expanding customer property on it. Once you get the customer ID, you can call this API to list customer's payment methods. This should allow you to see if they have any other PaymentMethods or not.

Stripe get Apple Pay token to create a customer - web

Is there a way to get the apple pay payment method id without charging the user? Right now I've been trying to do some variation of step 5: https://stripe.com/docs/stripe-js/elements/payment-request-button, but I seem to only be able to get the payment method id after i have confirmed a payment.
You listen to the PaymentRequest's paymentmethod event on your page, like shown here: https://stripe.com/docs/stripe-js/elements/payment-request-button#html-js-complete-payment
The example uses the ev.paymentMethod.id to confirm a PaymentIntent but you can choose to not do that and instead post the PaymentMethod token to your server and use it some other way (e.g. to attach to a Customer and use in a Subscription).
Make sure to call ev.complete() with success/fail as appropriate so that the payment sheet closes with the correct message to the customer.

Stripe - How to get purchaser's information for a charge with Connect (Express)?

I've set up a test charge using stripe.Charge.create() and it is working, i.e. I can see the dollar amounts processed in the Connect test dashboard. It appears to be money going from the tok_visa to a dummy Stripe account I set up.
charge = stripe.Charge.create(
amount=1000,
currency="usd",
source="tok_visa",
application_fee_amount=123,
transfer_data={
"destination": "{{CONNECTED_STRIPE_ACCOUNT_ID}}",
}
)
For a marketplace type platform, I need to be able to send funds from the purchasing user to the selling user, however.
Would I need to get the purchaser's account (or some other object?) to use as the "source" in the charge?
How do I get that to put it into the "source" field?
I feel a little lost in Stripe's docs, so I could be missing something. Thank you for any tips.
I need to be able to send funds from the purchasing user to the
selling user, however
If by 'purchasing user' you mean a cardholder customer of your marketplace, this is exactly what you have. tok_visa here represents a credit card, that you accept through a payment page.
If instead you mean, the purchasing user is another connected account in the marketplace, you can either :
use an account debit to debit one account, and then transfer to
another.
collect a card from users when they sign up and create a
Customer object for them. You can then charge that customer as needed
like in your example code. You need to maintain a link in your system that
'customer cus_xxx is also my marketplace user acct_xxx'

Stripe - Create subscriptions with a customer with PaymentMethod instead of Source (Token)

I'm integrating Stripe Subscriptions in our workflow, but when creating a subscription with a Customer created with a paymentMethod instead of source I receive the error This customer has no attached payment source.
If I call in the browser createToken() and attach it to the customer in the source field it works.
We integrated one-time payments using the new docs with SCA that use createPaymentMethod() and not createToken(), so our customers are saved with paymentMethod, not source, like explained here:
https://stripe.com/docs/payments/cards/saving-cards-after-payment#save-payment-method
curl https://api.stripe.com/v1/customers \
-u sk_test_secret_token \
-d payment_method="{{PAYMENT_METHOD_ID}}"
Furthermore, in the migration guide it says to replace createToken() with createPaymentMethod().
In Scenario 2: Charging customers off-session for their initial payment, I saw the following:
To create subscriptions and charge customers off-session for their
initial payment, you need to:
1) Use CreatePaymentMethod to collect payment information
2) Create a customer using the ID of the PaymentMethod you created
3) Create the subscription
4) Set up error handling using handleCardSetup for authentication failures and handleCardPayment for authorization failures
I followed those steps. I don't create a SetupIntent (just like I don't create it in one-time payments and it works in these cases), and receive the error I said before when trying to create subscriptions.
Is it possible to create subscriptions with a customer with paymentMethod instead of source?
(We reuse cards using the customers created with paymentMethod for one-time payments, so it would be very important to be able to reuse the same customer/card for subscriptions, without the need for the user input data in stripe elements or anything of the sort, because it would break our flow for reusing cards)
I was able to solve it by including the payment method in the field default_payment_method.
This field is described as follows:
ID of the default payment method for the subscription. It must belong
to the customer associated with the subscription. If not set, invoices
will use the default payment method in the customer’s invoice
settings.
So I assumed that it would use the payment method I associated with the customer when not defined. It seems it is not happening, tough, so I needed to pass it explicitly (is it a Stripe bug? or creating the customer with paymentMethod doesn't make it the default payment method in the customer’s invoice settings? I will contact Stripe to make sure).
Update (2019-09-23)
I contacted Stripe asking if this was a bug and they replied:
[...] With that being said though, this wouldn't be a bug on our end, rather
expected behavior.
If you're wanting for the Payment Method that you're adding to the
customer object to be used on subscriptions, or invoices, by default
without specifying the default_payment_method when creating the
subscription then you would want to attach the Payment Method and
specify the invoice_settings.default_payment_method parameter when
updating the customer. This information can be found within our API
reference here:
https://stripe.com/docs/api/payment_methods/attach
The parameter to use when updating this can be found on the customer
object itself, here:
https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method
This can also be specified upon customer creation, which can be seen
here:
https://stripe.com/docs/api/customers/create#create_customer-invoice_settings-default_payment_method
Specifying this parameter would indicate that the card being added
would be the default for subscriptions, as well as invoices, so that
the default_payment_method wouldn't need to be specified upon the
subscription, or invoice, creation.

Use the same currency as the credit card

I am working on an application where payments are handled by stripe. Customers are from 2 different countries.
stripe.js is used on the client side. stripe-php library is used on the back-end to charge the customer after stripe.js posts to the back-end page.
Is there a way to detect the country of the card the customer is using so that I can use the corresponding currency in the back-end call charging the customer?
Thanks
You can get a card's country of issue from a Stripe token response that comes from Stripe.js or Checkout. In addition to the token id this includes a card object with the properties like the card brand and country.
When you call stripe.createToken take a look at the token object in the response, e.g. result.token.card.country
The token object also contains a "client_ip" property that can be helpful for deciding if you want to process a charge given a particular IP address / locale derived from that. https://stripe.com/docs/api#token_object
Beyond trying to determine locale from card data or ip address, you could simply ask the user which currency they would like to be billed in, giving some additional flexiblity

Resources