Attaching a PaymentMethod to a Customer within PaymentIntent - stripe-payments

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

Related

Stripe Onboarding Flow: Stripe Onboarding Screens not coming up

I'm trying to onboard a customer for a Stripe Express account by using the Stripe API. I create an account requesting the relevant capabilities, providing simple information like first name, last name and email, and the account is created successfully. I expected the Stripe website to come up to request the missing information from the customer (like DOB, bank account info,...), but instead, the account creation just finishes and the account is restricted due to the missing information. Do I have to redirect the user after checking what the status of the account is? What's the flow?
This is the start_onboarding() method of my teacher_us object:
// methods
function start_onboarding() {
// load Stripe library--- moved to the main plugin file
// require_once('..\vendor\autoload.php');
global $stripe_api_key;
$stripe = new \Stripe\StripeClient($stripe_api_key);
$stripe_account = $stripe->accounts->create([
'type' => 'express',
'country' => 'US',
'business_type' => 'individual',
'email' => $this->get_email(),
'individual' => [
'first_name' => $this->get_first_name(),
'last_name' => $this->get_last_name(),
],
'capabilities' => [
'transfers' => ['requested' => true],
'card_payments' => ['requested' => true],
],
]);
It seems that just calling this method doesn't make Stripe to collect the missing info. What am I missing?
This does not happen automatically. After creating the Express account, you must create an Account Link for Connect Onboarding, and redirect your user to the url it includes to go through the onboarding flow to provide the rest of the required information.

Stripe v3 - SetupIntents and Subscriptions

I'm trying to use SetupIntents to validate and save a Payment Method and then create a Subscription using it to charge the customer (inmediately and then monthly) for the required amount.
This appears to be working fine:
The Card is validated (including SCA if needed)
The Payment Method is created, attached to customer as default and enabled for future usage with the status SUCEEDED.
The Subscription is created and uses the above payment method
The problem is that Stripe then generates the corresponding Invoice and Payment Intent but the latter with the status "requires_action" whenever the provided Card requires Secure Customer Authorization (SCA), even though the right payment method (enabled for future usage) is being used and the card validation has been already performed.
I thought the whole point of using SetupIntents was precisely to validate the payment method beforehand and be able to charge the customer afterwards.
Is my assumption simply wrong or this is actually possible and I might just be missing something?
Thanks in advance
EDIT: This is the subscription creation code in the backend:
# Set the default payment method on the customer
Stripe::Customer.update(
stripe_customer_id,
invoice_settings: {
default_payment_method: #requested_source
}
)
subscription = Stripe::Subscription.create({
"customer" => stripe_customer_id,
"proration_behavior" => 'create_prorations',
"items" => [
[
"plan" => "#{#stripe_plan_api_id}",
],
],
'default_tax_rates' => [
"#{#stripe_tax_rate_id}",
],
"expand" => ["latest_invoice.payment_intent"]
});
Thanks for the question, Eduardo.
There are a couple ways to create a Subscription while gaining the Customers SCA authentication and permission to charge the card later. Assuming we already have a Stripe Customer object created and their ID is stored in stripe_customer_id. With the flow you have now there are a couple steps:
Create the SetupIntent. Note that if you create it with usage: 'off_session' and the Customer it'll attach the resulting PaymentMethod when confirmed.
setup_intent = Stripe::SetupIntent.create({
customer: stripe_customer_id,
usage: 'off_session',
})
Collect payment details and confirm the SetupIntent using its client_secret on the client which will attach the PaymentMethod to the customer. Note that it will attach but will not be set as the invoice_settings.default_payment_method by default, so you'll need to make a separate API call later to update the Customer (see step 3).
stripe.confirmCardSetup(
'{{setup_intent.client_secret}}',
{
payment_method: {
card: cardElement,
},
}
).then(function(result) {
// Handle result.error or result.setupIntent
});
Update the Customer and set its invoice_settings.default_payment_method equal to the ID of the PaymentMethod on the successfully confirmed SetupIntent.
Stripe::Customer.update(
stripe_customer_id, {
invoice_settings: {
default_payment_method: 'pm_xxxx', # passed to server from client. On the client this is available on the result of confirmCardSetup
}
})
Create the Subscription with off_session: true
subscription = Stripe::Subscription.create({
customer: stripe_customer_id,
proration_behavior: 'create_prorations',
items: [{
plan: "#{#stripe_plan_api_id}",
}],
default_tax_rates: [
"#{#stripe_tax_rate_id}",
],
expand: ["latest_invoice.payment_intent"],
off_session: true,
})
This uses what's called a "Merchant Initiated Transaction" (MIT) for the Subscription's first payment. This is technically okay if the Subscription is created later after the Customer leaves and should technically work.
If the customer is on your site/app when you create the Subscription, there's another flow that is a bit more correct and doesn't require using a MIT exemption for SCA. The flow is the following for a Subscription without a trial:
Collect card details with createPaymentMethod on the client (no SetupIntent)
stripe.createPaymentMethod({
type: 'card',
card: cardElement,
}).then(function(result) {
//pass result to your server.
})
Attach those card details to the Customer
Stripe::PaymentMethod.attach(
"pm_xxx", {
customer: stripe_customer_id
}
)
Update the Customer's invoice_settings.default_payment_method
Stripe::Customer.update(
stripe_customer_id,
invoice_settings: {
default_payment_method: #requested_source
}
)
Create the Subscription (without off_session: true)
subscription = Stripe::Subscription.create(
customer: data['customerId'],
items: [
{
price: 'price_H1NlVtpo6ubk0m'
}
],
expand: ['latest_invoice.payment_intent']
)
Use the Subscription's latest_invoice's payment_intent's client_secret to collect payment details and confirm on the client.
stripe.confirmCardPayment(
'{{subscription.latest_invoice.payment_intent.client_secret}}', { ......
This second payment flow is a bit more correct from an SCA standpoint for getting authorization to charge the card. The second approach is outlined in the guide here: https://stripe.com/docs/billing/subscriptions/fixed-price
We also have a Stripe Sample you can use to experiment here: https://github.com/stripe-samples/subscription-use-cases/tree/master/fixed-price-subscriptions

How to save card details using stripe?

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

Testing manual confirmation of Stripe's PaymentIntent

I am trying to write tests for the new SCA Stripe integration using Elements & their API. I am stuck testing the manual confirmation of a PaymentIntent.
I am able to create the PaymentIntent. Usually it returns a client_secret that is then used with Stripe.js handleCardAction() which then returns a PaymentIntent id that I can confirm using PaymentIntent::retrieve().
Since my tests are running on the PHP-side I am not able to fire the handleCardAction() and if I skip that step and use the client_secret I get this exception:
Stripe\Exception\InvalidRequestException : No such payment_intent: pi_1FFIp5HU59FHbUqraBNXk9Br_secret_agKRuHJx4y83RFnjpKkGmd29W
How do I get a PaymentIntent ID that is ready for confirmation without using Stripe.js?
Apparently I could just use
$intent = PaymentIntent::create([
'payment_method' => 'pm_card_threeDSecure2Required',
'amount' => 1000,
'currency' => 'USD',
'description' => 'blabla',
'confirmation_method' => 'manual',
'confirm' => true,
]);
// $intent->id can be used with PaymentIntent::retrieve()

Stripe API transfer to Bank Account

Does anyone know how to transfer to a bank account using Stripe API?
In the Stripe Reference it looks very simple: https://stripe.com/docs/api/python#create_transfer
stripe.Transfer.create(
amount=400,
currency="usd",
destination="acct_19MKfXGyOv0GIG6Z",
description="Transfer for test#example.com"
)
I don't understand where the destination id comes from. How can I get the destination id of a bank account?
Thanks
There are two types of transfers with Stripe:
internal transfers, from a Stripe platform account to one of its connected accounts. This is known as a "special-case transfer".
external transfers, from a Stripe account to its associated bank account.
If you only have a single bank account for a given currency, you can use the special value "default_for_currency" for the destination parameter.
E.g. if you run this code:
stripe.Transfer.create(
amount=400,
currency="usd",
destination="default_for_currency",
description="Transfer for test#example.com"
)
then $4.00 would be sent to your USD bank account.
EDIT: On newer API versions (>= 2017-04-06), "internal transfers" are now simply known as "transfers", and "external transfers" are now known as "payouts". Cf. this doc page for more information.
I have to use the below code in my existing project
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
// Create a Charge:
$charge = \Stripe\Charge::create([
'amount' => 1300,
'currency' => 'GBP',
'customer' => 'cus_FM5OdvqpYD7AbR', // customer id
'transfer_group' => date('y-m-d_h:i:s'),
'transfer_data' => [
'destination' => 'acct_1EuyLUIpWjdwbl8y', // destination id
'amount' => 700
]
]);
dd($charge);
https://stripe.com/docs/connect/custom-accounts
this above link will let you know how to get bank account id
stripe.accounts.create({
country: "US",
type: "custom"
}).then(function(acct) {
// asynchronously called
});
you can get
this code will give acc. id in response that you can use in destination.

Resources