使用您的用戶模型Boolean
值。
var UserSchema = new Schema({
name: String,
hasPaid: {type: Boolean, default: false} //set this false
});
然後在您的REST API路由中,用戶購買產品;現在設置hasPaid
爲true
// req.user._id is passport config
User.findOneAndUpdate({_id: req.user._id}, {$set: {"hasPaid":istrue}}).exec(function(err) {
if(err) {
throw err;
}else {
req.flash('success', 'Thank you for submitting the form.');
res.render('charge', {
layout: 'dash',
user: req.user,
success: req.flash('success')
});
}
});
現在你可以保持跟蹤你購買的產品授予他們訪問您的網站的其他部分用戶。
Stripe.js自帶Checkout.js,這使得它更容易使用Stripe的服務。
將其複製並粘貼到您的html/jade/handlebars或視圖文件中。這將顯示一個彈出窗體讓用戶輸入他或她的cc信息。
<form action="/charge" method="POST">
<script
src="https://checkout.stripe.com/checkout.js"
class="stripe-button"
data-key="pk_test_bla3hf&kc033"
data-image="/square-image.png"
data-name="Demo Site"
data-description="2 widgets ($20.00)"
data-amount="2000">
</script>
</form>
一旦用戶按下提交,您將在服務器上抓取您將收到一個令牌。從您的REST API的路線裏面,可以向客戶收取:
var token = req.body.stripeToken; // Using Express
// Create a charge: this will charge the user's card
var charge = stripe.charges.create({
amount: 1999, // Amount in cents
currency: "usd",
source: token,
metadata: {
user: req.user._id,
email: req.user.local.email
}
description: "Example charge" //keep track by writing a description or you can use the metadata property Stripe has to offer in the charges object
},function(err, charge) {
if (err && err.type === 'StripeCardError') {
// The card has been declined
}else {
res.redirect('/thanks-for-your-order');
console.log('charge here ' + charge.id); //store the id
console.log('charge here ' + charge.invoice); //store the invoice
console.log('charge here ' + charge.customer); //store the customer id
}
});
您現在可以通過存儲在你希望的任何型號的充電對象的屬性跟蹤每個訂單。