【问题标题】:Trying to submit form data into database after payment is succeeded, with node js and express?付款成功后尝试将表单数据提交到数据库,使用节点js和快递?
【发布时间】:2021-06-13 23:48:33
【问题描述】:

我有一个项目,我有一个表格,您可以在其中填写名字和姓氏,然后这些名字将上传到我的 MongoDB 数据库,然后放在 p-tags 中的另一个页面上。

我已经成功完成了这项工作,但问题是我需要成功付款才能将表单数据上传到我的数据库。

目前我有提交表单和条纹支付 api 的表单和 javascript。

首先,我有条带 API 的脚本,还有用于将表单数据保存到/从 db 发送的脚本:

    // routes
app.get('/', (req, res) => {
    res.render('index', {
        stripePublishableKey: 'pk_test_51IVfPNLNmuMLNSTmFh4R5GIZMjnTTf4mQGy5UQRTam6vfooJWhyXIrT5tIclvgydZm1EYPfN1VgBBCZAN6Las1Ap007qYaR6zr'
    });
});

// charge route
app.post('/charge', (req,res)=>{
    const amount = 2500;
    
    stripe.customers.create({
        email: req.body.stripeEmail,
        source: req.body.stripeToken
    })
    .then(customer => stripe.charges.create({
        amount,
        description: 'coviDON',
        currency: 'usd',
        customer: customer.id
    }))
    .then(charge => res.render('success'));
});

// the page where the form data will be submitted
app.get('/wall', (req, res) => {
    Covid.find().sort({ createdAt: -1 })
        .then((result)=>{
            res.render('wall', {covids: result})
        })
        .catch((err)=>{
            console.log(err);
        })
});

// save form data to db and post to wallsite
app.post('/wall', (req, res) => {
    const covid = new Covid(req.body);

    covid.save()
        .then((result)=>{
            res.redirect('/wall');
        })
        .catch((err)=>{
            console.log(err);
        })
});

然后,我有放置两个表单的索引页:

<body>
    <div class="create-attribute">
        <form action="/wall" method="POST">
            <label for="first-name">First name</label>
            <input type="text" id="firstName" name="firstName" required>
            <br><br>
            <label for="last-name">Last name</label>
            <input type="text" id="lastName" name="lastName" required>
            <button>Buy</button>
        </form>
    </div>

    <form action="/charge" method="POST">
        <script
        src="https://checkout.stripe.com/checkout.js"
        class="stripe-button"
        data-key= "<%= stripePublishableKey %>"
        data-amount="2500"
        data-name="Web Development Ebook"
        data-description="Ebook written by Brad Traversy"
        data-image="/img/marketplace.png"
        data-locale="auto"
        >
        </script>
        <script>
            // hide default stripe button
            document.getElementsByClassName('stripe-button-el')[0].style.display = 'none';
        </script>
        <button type="submit" class="btn btn-outline-dark text-white btn-lg">Purchase for 25$</button>
    </form>
</body>

所以,我的目标和问题是如何将这两者连接在一起,所以我必须按下条纹按钮并支付费用。付款成功后,我希望处理提交表单,以便将用户输入的数据存储到数据库中,然后在 p-tags 中发布到墙站点??

【问题讨论】:

    标签: javascript node.js mongodb forms stripe-payments


    【解决方案1】:

    首先,您使用的似乎是Legacy Checkout,它已被弃用多年。建议您不要以这种方式集成,而是更喜欢新的Checkout。如果这是一个现有的集成,你应该考虑migrating

    也就是说,如果您想做这样的事情,您需要切换到 custom configuration 并利用 token/source function callbacks

    【讨论】:

      猜你喜欢
      • 2015-12-01
      • 2021-03-02
      • 2012-12-17
      • 2018-06-10
      • 2021-01-15
      • 2020-05-04
      • 2018-05-23
      • 2018-07-07
      • 2021-10-26
      相关资源
      最近更新 更多