gpt4 book ai didi

node.js - 如何在成功购买后阻止 Stripe 覆盖客户的 session cookie

转载 作者:行者123 更新时间:2023-12-04 08:56:30 27 4
gpt4 key购买 nike

环境: express 、 express session 、 Stripe
在下面的简化示例中,当用户请求主页时 express-session为用户分配一个 session cookie。刷新页面会保留与访问成功或失败路由相同的 session ID。单击升级按钮会将客户端带到同样保持相同 session ID 的 Stripe 购物车屏幕。然而,一旦用户进入 Stripe 购物车,如果用户成功购买,他就会被转发到成功路线,并且 session ID 会被 Stripe 覆盖。在完整版中,这是一个问题,因为用户会登录,这会导致用户在成功购买后自动注销。我不确定为什么会发生这种情况或如何阻止它。
app.js

const bodyParser = require('body-parser');
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

const app = express();

app.set('view engine', 'ejs');

app.use(express.static('views'));

app.use(
session({
maxAge: 24 * 60 * 60 * 1000,
name: 'randomName',
resave: false,
saveUninitialized: true,
secret: 'randomSecret',
cookie: {
sameSite: true,
secure: false
}
})
);

app.get('/', function(req, res) {

req.session.userValues = true;
console.log(req.session);

res.render('index', { stripePublicKey: process.env.STRIPE_PUBLIC_KEY });
});

app.get('/success', function(req, res) {

console.log(req.session);

res.render('success');
});

app.get('/fail', function(req, res) {

console.log(req.session);

res.render('fail');
});

app.post('/create-checkout-session', bodyParser.raw({ type: 'application/json' }), async function(req, res) {

console.log(req.session);

const session = await stripe.checkout.sessions.create({
submit_type: 'auto',
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'name of product',
description: 'description of product'
},
unit_amount: 100
},
quantity: 1,
}
],
locale: 'en',
mode: 'payment',
success_url: 'http://localhost:8080/success',
cancel_url: 'http://localhost:8080/fail'
});

res.json({ id: session.id });
});

app.listen(8080, function() {
console.log('listening on port 8080');
});
index.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upgrade</title>
<script>var stripePublicKey = '<%- stripePublicKey %>';</script>
<script defer src="https://js.stripe.com/v3/"></script>
<script defer src="checkout.js"></script>
</head>
<body>

<button id="checkout-button">upgrade premium</button>

</body>
</html>
checkout.js
var stripe = Stripe(stripePublicKey);

var checkoutButton = document.getElementById('checkout-button');

checkoutButton.addEventListener('click', checkoutSequence);

function checkoutSequence() {

fetch('/create-checkout-session', {
method: 'POST',
})
.then(function(response) {
return response.json();
})
.then(function(session) {
console.log(session);
return stripe.redirectToCheckout({ sessionId: session.id });
})
.then(function(result) {
if (result.error) {
alert(result.error.message);
}
})
.catch(function(error) {
console.error('Error:', error);
});
}

最佳答案

经过6个小时的测试,我发现了问题。 cookie.sameSite必须设置为 lax而不是 true .显然,当 Stripe 走上成功之路时 express-session确定这是来自外部站点并重置 cookie。

app.use(
session({
maxAge: 24 * 60 * 60 * 1000,
name: 'randomName',
resave: false,
saveUninitialized: true,
secret: 'randomSecret',
cookie: {
sameSite: 'lax',
secure: false
}
})
);

关于node.js - 如何在成功购买后阻止 Stripe 覆盖客户的 session cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63800563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com