I am integrating a 3d payment system with iyzico in my project. When I try with non-3d, I can get paid without any problems, but when I use 3d, I successfully send the threeDSHtmlContent value to the page. I get sms verification and after entering the code I get a csrf error when I have to go back to callBack and go to /paymentSuccess page. I couldn’t understand why. I integrated csrf protection into routes with middleware (locals). I would appreciate it if you could help.
`exports.postProcessPayment = (req, res, next) => {
const userId = req.user._id;
User.findById(userId)
.then(user => {
if (!user) {
console.error('Kullanıcı bulunamadı.');
return res.status(404).json({ error: 'Kullanıcı bulunamadı.' });
}
console.log('Kullanıcı bilgileri:', user);
const { name,cart,surname } = user;
const { cardNumber, expireMonth, expireYear, cvc, cardHolderName } = req.body;
const cartItems = user.cart;
user.cart = [];
user.save();
const totalPrice = calculateTotalPrice(cart);
const request = {
locale: Iyzipay.LOCALE.TR,
conversationId: '123456789',
price: totalPrice.toString(),
paidPrice: totalPrice.toString(),
currency: Iyzipay.CURRENCY.TRY,
installment: '1',
basketId: 'B67832',
paymentChannel: Iyzipay.PAYMENT_CHANNEL.WEB,
paymentGroup: Iyzipay.PAYMENT_GROUP.PRODUCT,
callbackUrl: 'http://localhost:5050/paymentSuccess',
paymentCard: {
cardHolderName: cardHolderName,
cardNumber: cardNumber,
expireMonth: expireMonth,
expireYear: expireYear,
cvc: cvc,
registerCard: '0'
},
buyer: {
id: user.id,
name: user.name,
surname: user.surname,
gsmNumber: `+90${user.phone}`,
email: user.email,
identityNumber: '74300864791',
registrationAddress: user.adress,
address: user.address,
city: 'İstanbul',
country: 'Turkey',
zipCode: '35000'
},
shippingAddress: {
contactName: name + ' ' + surname,
city: 'İstanbul',
country: 'Turkey',
address: user.adress,
zipCode: '35000'
},
billingAddress: {
contactName: name + ' ' + surname,
city: 'İstanbul',
country: 'Turkey',
address: user.adress,
zipCode: '735000'
},
basketItems: cart.map(item => ({
id: item.productId,
name: item.name,
category1: 'Product',
category2: 'General',
itemType: Iyzipay.BASKET_ITEM_TYPE.PHYSICAL,
price: item.price.toString()
})),
};
console.log('Request Bilgileri:', request);
iyzipay.threedsInitialize.create(request, function (err, result) {
if (err) {
console.error("Ödeme işleminde bir hata oluştu:", err);
return res.status(500).json({ error: "Ödeme işlemi sırasında bir hata oluştu." });
} else {
console.log("Ödeme işlemi başarıyla tamamlandı:", result);
if (result && result.threeDSHtmlContent) {
const threeDSHtmlContent = Buffer.from(result.threeDSHtmlContent,'base64').toString('utf-8');
res.send(threeDSHtmlContent);
} else {
const newOrder = new Order({
userId: userId,
products: cartItems.map(item => ({
productId: item.productId,
name: item.name,
quantity: item.quantity,
imageUrl: item.imageUrl,
price: item.price
})),
totalPrice: totalPrice,
paymentStatus: 'success',
});
newOrder.save()
.then(savedOrder => {
console.log('Sipariş başarıyla oluşturuldu:', savedOrder);
// res.redirect('/paymentSuccess');
})
.catch(error => {
console.error("Sipariş oluşturma sırasında bir hata oluştu:", error);
res.status(500).json({ error: "Sipariş oluşturulurken bir hata oluştu." });
});
}
}
});
})
.catch(error => {
console.error("Kullanıcı bilgilerini alma sırasında bir hata oluştu:", error);
res.status(500).json({ error: "Kullanıcı bilgilerini alırken bir hata oluştu." });
});
};
routes.post('/processPayment', locals, shopController.postProcessPayment);
routes.get('/paymentSuccess', locals, shopController.getPaymentSuccess);`
/paymentSuccess needs to return to page.