When saving a customer in production, where to locate Stripe's source token - stripe-payments

I am following Stripe's account here: https://stripe.com/docs/saving-cards
in particular where the customer information is saved:
(async function() {
// Create a Customer:
const customer = await stripe.customers.create({
source: 'tok_mastercard',
email: 'paying.user#example.com',
});
// Charge the Customer instead of the card:
const charge = await stripe.charges.create({
amount: 1000,
currency: 'usd',
customer: customer.id,
});
// YOUR CODE: Save the customer ID and other info in a database for later.
})();
in development I can input source: 'tok_mastercard' and it operates as intended, however what would the token be in production when using sk_live_... ? the doc is not clear so far as I can tell.

The token would be something you create client-side when you collect the card details securely either using Stripe Elements or Stripe Checkout. Those let you exchange card details for a Stripe Token with a unique id tok_12345 that you then send to your server to create the customer and the charge.

Related

Stripe recurring payment integrate with masterpass

Is there any way to take recurring payment with Masterpass using Stripe?
Google pay and Apple pay can take the recurring payment with Stripe by returning the card token for stripe process. Is there any way to get card token like this with Masterpass?
Yes you can, you'd create a PaymentMethod with the data you get back from Secure Remote Commerce (which is what Masterpass is calling itself now) and use that to start a subscription.
Follow Stripe's SRC guide, then when you get to the "complete the payment" part you'd do something this instead:
// create the payment method
const pm = await stripe.paymentMethods.create({
type: 'card',
card: {
masterpass: {
cart_id: cartId, // your unique cart ID
transaction_id: req.query.oauth_verifier, // get this from the SRC callback URL
},
}
});
// create the Stripe customer
const cust = await stripe.customers.create({
payment_method: pm.id,
});
// create the subscription
const sub = await stripe.subscriptions.create({
customer: cust.id,
items: [{
price: {{PRICE_ID}},
}],
default_payment_method: pm.id,
});

Create Stripe Connect Customer with Payment Method on Platform Account

I am following the docs here about enabling other business to accept payments directly.
I have successfully created a connected account and accepted a payment from a customer on the connected account, but I have not yet been able to save that customers payment information for future payments.
All across the documentation, especially here it assumes you already created a customer on the platform account WITH a payment method, before you should try and clone the payment method to the connected accounts.
I cannot for the life of me figure out how to create a customer with payment information on the platform account before I clone them on the connected account.
As per the documentation, I started here on the client side where the accountID is the ID of the connected account:
const [stripePromise, setstripePromise] = useState(() =>
loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY, {
stripeAccount: uid.accountID,
})
);
My Stripe Elements are created with the stripePromise.
When trying to save the card details I do this on the client side:
This is where I believe my mistake is I am using the connected accounts credentials while trying to create a platform payment method.
const handleRememberMe = async () => {
const { token } = await stripe.createToken(
elements.getElement(CardElement)
);
console.log(token);
const res = await fetchPostJSON("/api/pay_performer", {
accountID: accountID,
amount: value,
cardToken: token,
});
That API call goes to "/api/pay_performer":
//Handle onboarding a new connect user for x
require("dotenv").config();
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2020-08-27",
});
import setCookie from "../../../utils/cookies";
export default async function createPayment(req, res) {
if (req.method === "POST") {
// Connected account ID and the token generated on the client to be saved.
const { accountID, amount, cardToken } = req.body;
console.log(`API side cardToken: ${cardToken.used}`);
try {
// Trying to create a customer with that token.
const customer = await stripe.customers.create({
email: "test2#example.com",
source: cardToken.card,
});
customer.sources = cardToken.card;
// Beginning the cloning process for the connected account.
const token = await stripe.tokens.create(
{
customer: customer.id,
},
{
stripeAccount: accountID,
}
);
const clonedCustomer = await stripe.customers.create(
{
source: token.id,
},
{
stripeAccount: accountID,
}
);
console.log(`Default Source: ${clonedCustomer.default_source}`);
console.log(`AccountID: ${accountID}, Amount: ${amount}`);
const paymentIntent = await stripe.paymentIntents.create(
{
payment_method_types: ["card"],
payment_method: clonedCustomer.default_source
? clonedCustomer.default_source
: null,
amount: amount * 100,
currency: "usd",
application_fee_amount: 1,
customer: clonedCustomer.id,
},
{
stripeAccount: accountID,
}
);
const secret = paymentIntent.client_secret;
console.log(secret);
const payment_method = paymentIntent.payment_method;
return res.status(200).send({ secret, payment_method });
} catch (e) {
console.log(e);
return res.status(500).send({ error: e.message });
}
}
}
But I get an error that the token provided is not a valid token. I am assuming this is because I created the token using the credentials for the connected account on the client and then tried to apply it to the platform account.
How do I have a separate UI to create platform customers FIRST, and then come back and clone them to the connected accounts upon purchase.
Should I not pass a token to the server and just pass the card information over HTTPS? Very lost and the documentation has not helped!
Any help is appreciated!
In order to create a Customer, collect card information and attach a PaymentMethod to them, you could for example use a SetupIntent and Elements [1] or create a PaymentMethod [2]. The core point being, if the system should initially store that information on the platform, the connected account isn't coming into play at all yet. Broadly speaking the steps are (not providing the account id of the connected account anywhere as this is only happening on the platform account), using the platform's secret key:
Create a customer
Collect card data using Stripe.js and Elements
Attach the pm_xxx to the customer in the backend
[1] https://stripe.com/docs/payments/save-and-reuse
[2] https://stripe.com/docs/js/payment_methods/create_payment_method

Stripe create a direct charge node.js

I am trying to create a direct charge to a standard Stripe connected account from a node.js backend. The codes I wrote are:
async function create_direct_charge(language_learner_id, native_speaker_id, call_duration) {
try {
const customer_id = "xxxxxx";
const connected_standard_account_id = "xxxxxx;
const paymentMethods = await stripe.paymentMethods.list({
customer: customer_id,
type: 'card',
limit: 1,
});
const paymentMethod = paymentMethods.data[0].id;
const paymentIntent = await stripe.paymentIntents.create({
payment_method_types: ['card'],
payment_method: paymentMethod,
amount: 1000,
customer: customer_id,
currency: "cad",
off_session: true,
confirm: true,
application_fee_amount: 100,
}, {
stripeAccount: connected_standard_account_id,
});
}
catch (error) {
console.log(error);
}
}
The above codes generate an error of: No such PaymentMethod: 'pm_xxxxxx'; OAuth key or Stripe-Account header was used but API request was provided with a platform-owned payment method ID. Please ensure that the provided payment method matches the specified account.
The thing is that the customer with the customer_id already has a valid payment method attached:
The payment method is valid, since I was able to create a destination charge with that payment method. The payment is not set as the default source though. I am wondering if that is the problem. What did I do wrong here?
The payment method is valid, since I was able to create a destination charge with that payment method.
That tells me that the PaymentMethod (e.g. pm_123) was created on the Platform Stripe account and hence lives there.
So when tell Stripe to "Charge pm_123 on Connect account acct_1 as a Direct Charge", that won't work as the PaymentMethod object pm_123 doesn't exist on the Connect account, it exists on the Platform account.
In order to get this to work, you have to first "clone" the PaymentMethod pm_123 to the Connect account, then create a Direct Charge with this newly cloned PaymentMethod token, as explained here: https://stripe.com/docs/payments/payment-methods/connect#cloning-payment-methods

How to attach a payment method with Stripe?

I'm struggling to get Stripe to work on my server.
So, on the client side, I have this code (after much struggle to get element working) :
this.props.stripe.createPaymentMethod({ type: 'card', card: cardElement, billing_details: { name: name } }).then((paymentMethod) => {
// server request with customer_id and paymentMethod.id);
});
This works fine. Now, on the server (NodeJS), I want to add a new on-going subscription with a fixed fee for my customer.
Here's what I have :
const paymentIntent = await stripe.paymentIntents.create({
amount: 1000,
currency: 'usd',
payment_method_types: ['card'],
customer: req.body.customer_id,
metadata: { integration_check: 'accept_a_payment' },
});
const paymentIntentConfirmation = await stripe.paymentIntents.confirm(paymentIntent.id, { payment_method: req.body.paymentMethod_id });
const newSubscription = await stripe.subscriptions.create({
customer: req.body.customer_id,
items: [{ plan: premium_plan.id, quantity: 1 }],
default_payment_method: req.body.paymentMethod_id,
});
const attachPaymentToCustomer = await stripe.paymentMethods.attach(req.body.paymentMethod_id, { customer: req.body.customer_id });
const updateCustomerDefaultPaymentMethod = await stripe.customers.update(req.body.customer_id, {
invoice_settings: {
default_payment_method: req.body.paymentMethod_id,
},
});
So, if I don't attach the payment to customer, I get the following error message :
'The customer does not have a payment method with the ID pm_1Gx9m1HVGJbiGjghYhrkt6j. The payment method must be attached to the customer.'
If I do, I get the following error message :
'This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again.'
So, how do I add the damn payment method, so when I retrieve my customer, it shows this customer has been updated with a new subscription to the service he just subscribed to, together with his payment method (a CC in this case).
Any help here for a frustrated user is very appreciated !
On a more general note, implementing Stripe has been a very painful experience so far. Nothing seems to work. I use Typescript and there are so many bugs. The documentation is not very helpful and not well explained. "Create a source", "create a token", "create a payment intent", "create a setup intent", how am i supposed to understand the difference between all these things ? I want to add a god damn online subscription, which should be quite a standard procedure for an Internet service. Why are there so many different guidelines, with tokens, with sources, etc....
There's a few changes you can make here to get it working, in order to start a Subscription [1] you don't need to create and confirm a PaymentIntent. That is created automatically inside the Invoice(s) as they're created for payment. So the steps roughly are (you've done a lot of this already but just to have an end to end example):
Create a customer
Collect the payment information securely using Stripe.js
Attach the PaymentMethod to the Customer
(Optionally) save that as the invoice settings default payment method (because you can pass the PaymentMethod to the Subscription creation as a default payment method, but it's good practice so that you can start Subscriptions for that Customer with the same payment method)
Create a Subscription
Provision your service
Take into account SCA/3DS and handle authentication [2]
That's outlined in detail on [1]. Here's some sample code to get the Subscription started, you can replace the calls that create products and prices with your own IDs of course:
const customer = await stripe.customers.create({
name: "Foo Bartley"
});
const paymentMethod = await stripe.paymentMethods.create(
{
type: 'card',
card: {
number: '4242424242424242',
exp_month: 6,
exp_year: 2021,
cvc: '314',
},
}
);
const product = await stripe.products.create(
{name: 'Gold Special'}
);
const price = await stripe.prices.create(
{
unit_amount: 1111,
currency: 'eur',
recurring: {interval: 'month'},
product: product.id,
}
);
// Everything above here is just setting up this demo
const attachPaymentToCustomer = await stripe.paymentMethods.attach(
paymentMethod.id, // <-- your payment method ID collected via Stripe.js
{ customer: customer.id } // <-- your customer id from the request body
);
const updateCustomerDefaultPaymentMethod = await stripe.customers.update(
customer.id, { // <-- your customer id from the request body
invoice_settings: {
default_payment_method: paymentMethod.id, // <-- your payment method ID collected via Stripe.js
},
});
const newSubscription = await stripe.subscriptions.create({
customer: customer.id, // <-- your customer id from the request body
items: [{ plan: price.id, quantity: 1 }], // <-- plans and prices are compatible Prices is a newer API
default_payment_method: paymentMethod.id, // <-- your payment method ID collected via Stripe.js
});
Hope this helps!
[1] https://stripe.com/docs/billing/subscriptions/fixed-price#create-subscription
[2] https://stripe.com/docs/billing/subscriptions/fixed-price#manage-payment-authentication

Trying to register a Stripe customer with a credit card and use the token received to charge the card in Node.js server

I'm using the example HTML form to get the Stripe token, providing credit card information.
As a response from Stripe, I get an object containing
the Stripe token ("tok_173MXoHz........")
a card object with an ID ("card_173MXoHzqz0X....") which hads a different ID
These are Posted to the server and a customer object is created
var stripeFormToken = req.body.id;
var cardToken = req.body.cardToken;
stripe.customers.create({
description: email,
source: stripeFormToken // obtained with Stripe.js
},
function(err, customer) {
if(err){
console.warn(err);
});
and saving the customer ID in a database.
By looking at the dashboard at https://dashboard.stripe.com/test/customers I see that the customer objects are being created but no credit card is added for any of them.
Also when trying to charge a customer by
stripe.charges.create({
amount: 500,
currency: "cad",
customer: customer.id, // obtained with Stripe.js
},
function(err, charge) {
if(err){
console.warn(err);
}else{
console.log("successfully charged");
}
});
but I'm getting back the error:
[Error: Cannot charge a customer that has no active card]
type: 'StripeCardError',
stack: 'Error: Cannot charge a customer that has no active card\n at Error._Error
After extensively looking into the documentation and examples online I wasn't able to solve this.

Resources