Cannot charge a customer that has no active card - javascript

So I am getting the error in Stripe that "Cannot charge a customer that has no active cards".
I am using node js for this
Payment Code is given below
try {
const customer = await stripe.customers.create({
email: req.body.email,
source: req.body.id,
});
const payment = await stripe.charges.create(
{
amount: req.body.totalAmount * 100,
currency: "inr",
customer: customer.id,
receipt_email: req.body.email,
confirm: true,
off_session: true,
},
{
idempotencyKey: uuidv4(),
}
);
And I am getting the following error
type: 'StripeCardError',
raw: {
code: 'missing',
doc_url: 'https://stripe.com/docs/error-codes/missing',
message: 'Cannot charge a customer that has no active card',
param: 'card',
type: 'card_error',
}

Log out req.body.id as that might be null and then investigate why your frontend is not passing that parameter to your backend.
Second, confirm and off_session are not supported parameters on the /v1/charges endpoint.

Related

Filter checkout session by metadata Stripe

I'm building a marketplace (you can imagine it like an Ebay where there are buyer and seller). And i want to get an items that bought customer (and vice versa with seller).
Here is my checkout session service:
async charge(userId: number, dto: CreateChargeDto) {
//Get an item by id
const item = await this.prisma.item.findUnique({
where: {
id: dto.itemId,
},
});
if (!item) {
throw new BadRequestException('There is no item with this id');
}
// Search for user
const user = await this.prisma.user.findUnique({
where: {
id: userId,
},
});
//update customer
await this.updateCustomer(user.stripeCustomerId, dto);
const session = await this.stripe.checkout.sessions.create({
success_url: 'http://localhost:3000/success?message=Succesful+payment',
cancel_url: 'http://localhost:3000/404?message=Payment+canceled',
mode: 'payment',
currency: 'pln',
customer: user.stripeCustomerId,
customer_update: {
address: 'auto',
},
metadata: {
seller_id: item.userId, // here is where i save seller id
},
line_items: [
{
quantity: 1,
price_data: {
currency: 'pln',
unit_amount: item.price * 100,
product_data: {
name: item.name,
images: item.images,
metadata: {
id: item.id,
},
},
},
},
],
});
return session.id;
}
async getCheckoutList(userId: number) {
const user = await this.prisma.user.findUnique({
where: {
id: userId,
},
});
const checkouts = await this.stripe.
return checkouts.data;
}
And now i wanna filter this session checkouts so i can display them in buyer (or seller) personal page.
const checkouts = await this.stripe.checkout.sessions.list({
where: {
metadata: {
seller_id: // seller id
}
}
How can i do it?
There's currently no way to filter for Checkout Sessions by metadata.
Some other options are :
Listen for the checkout.session.completed webhook event, save the data to your DB and then perform your own filtering. See https://stripe.com/docs/webhooks for more information on handling webhooks.
Add the metadata to the PaymentIntent also using the following parameter : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata. You can then use the Search API to filter for the
corresponding PaymentIntent with https://stripe.com/docs/search. Once
you have the PaymentIntent, retrieve the corresponding
Checkout Session with
https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-payment_intent
To filter for Checkout Sessions by Customer, you can use https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-customer
So the best in my opinion in this situation is to use a webhook.
The perfect example in Nestjs you can read here: https://wanago.io/2021/07/05/api-nestjs-stripe-events-webhooks/

Refund stripe payment

I am using the stripe connect feature. The payment feature is working fine but when I run stripe.refunds.create() method to refund the payment, though it returns successful status but the stripe dashboard does not update.
Request:
const intent = await stripe.refunds.create({
payment_intent: booking.paymentIntent,
reason: 'requested_by_customer',
});
Response:
{
id: 're_*********************',
object: 'refund',
amount: 20000,
balance_transaction: 'txn_*********************',
charge: 'ch_*********************',
created: 1670775881,
currency: 'usd',
metadata: {},
payment_intent: 'pi_*********************',
reason: 'requested_by_customer',
receipt_number: null,
source_transfer_reversal: null,
status: 'succeeded',
transfer_reversal: null
}
Dashboard:
I am new to stripe. If I am doing anything wrong please help me to find it or give me any article or doc. The stripe doc seems huge and confusing to me.

Can't create subscriptions on Stripe

I can't create stripe subscription due to Missing required param: items.. I request it with items although.
The error:
{
"error": {
"code": "parameter_missing",
"doc_url": "https://stripe.com/docs/error-codes/parameter-missing",
"message": "Missing required param: items.",
"param": "items",
"type": "invalid_request_error"
}
}
The code:
const stripeCustomer = await stripe.customers.create({
name: name,
email: email,
plan: basicPlan,
})
const stripeSubscription = await stripe.subscriptions.create({
items: [{ plan: basicPlan }],
customer: stripeCustomer.id,
})
Stripe Customer account was successfully added.
Got the same problem. I've found out I'm lacking some package details

How to send mandrill emails to array of emails?

I'm building an app in Node and I'm using mandrill to send emails every time there is a new user to a predefined array of emails. I have an array of emails:
And I have this function where
newUserEmail(user_name, email) {
emailArray = [example1#ex.com, example2#ex.com, example3#ex.com]
const message = {
html: '<p>Name: *|NAME|* <br> Email: *|EMAIL|*</p>',
text: 'Name: *|NAME|*, Email: *|EMAIL|*',
subject: 'New person arrived',
from_email: 'newperson#example.com',
from_name: 'New',
to: [{
email: emailArray,
type: 'to'
}],
merge: true,
merge_vars: [{
rcpt: emailArray,
vars: [{
name: 'NAME',
content: user_name
}, {
email: 'EMAIL',
content: email
}]
}]
};
mandrill_client.messages.send({ message }, function(result) {
console.log(result);
}, function(e) {
console.log(`A mandrill error occurred: ${e.name} - ${e.message}`);
});
}
I get this on my console:
[ { email: 'Array',
status: 'invalid',
_id: '...',
reject_reason: null } ]
If I set only one email, it gets sent without problems.
Do I need to make a loop and run this function as many times as there are emails in the array? I hoped mandrill would recognise emails in the array :(
From what I gathered after a look at the documentation it looks like each object in the "to" array is an individual email address.
I would not run the function for each email address. Just map over the email array.
For example:
const formattedArray = emailArray.map(email => ({ email, type: 'to' }));
// if you're not a fan of arrow functions
const formattedArray = emailArray.map(function(email) {
return { email, type: 'to' };
});
Then in the mandrill message you can just set "to" equal to the formattedArray
to: formattedArray

Merge an Item Fulfillment with an advanced template and email it Netsuite 2.0

So without the merge function below, this code sends an email on save, but I cannot for the life of me get email merge to work in Netsuite 2.0, so how do I merge an advanced pdf template with an item fulfillment and email it?
/**
*#NApiVersion 2.x
*#NScriptType UserEventScript
*/
define(['N/email','N/render', 'N/record', 'N/file'],
function(email, record, file,render) {
function afterSubmit(context) {
function templatemerge() {
var myMergeResult = render.mergeEmail({
templateId: 121,
entity: {
type: 'employee',
id: 18040
},
recipient: {
type: 'employee',
id: 18040
},
supportCaseId: 'NULL',
transactionId: 1176527,
customRecord: 'NULL'
});
}
templatemerge();
function sendEmailWithAttachement() {
var newId = context.newRecord;
var emailbody = 'attachment';
var senderId = 18040;
var recipientEmail = 'email#email.com';
email.send({
author: senderId,
recipients: recipientEmail,
subject: 'Item Fulfillments',
body: emailbody
});
}
sendEmailWithAttachement();
}
return {
afterSubmit: afterSubmit
};
});
Try rearranging the first function signature to function(email, render, record, file)
They are probably in the wrong order.

Categories