我正在运行第三方支付网关作为覆盖,它返回一个回调onSuccess和onClose,我根据订单状态更新数据库,如下所示:
index.html
successCallback: function (data) {
let userUnderscore = data.user.email.replace(/\./g, '_');
console.log(userUnderscore + ' successCallback');
if (data.checkout.completed === true) {
const now = moment().format('L');
const until = moment().add(30, 'days').calendar();
var data = {
plan: data.product.name,
planID: data.product.id,
completed: data.checkout.completed,
transactionID: data.checkout.id,
userID: data.user.id,
country: data.user.country,
}
firebase.database().ref('/billing/' + userUnderscore).update({
premium: true,
lastCheckout: now,
until: until,
})
firebase.database().ref('/billing/' + userUnderscore).push(data)
}
},
JSX部分基本上只是每个componentDidMount()上的数据库获取
firebase.database().ref(`/billing/${userUnderscore}/`)
.once('value', snapshot => {
var obj = snapshot.val()
// new user - no object yet
if (obj === null) {
this.setState({ premium: false })
}
// order attempt - not completed
else if (obj.premium === false) {
this.setState({ premium: false })
}
// successful transaction - expires on calc
else if (obj.premium === true) {
const now = moment().format('L');
if (now === obj.until) {
this.setState({ premium: false })
}
else {
this.setState({ premium: true, until: obj.until })
}
}
});
我知道这是非常基本的实现,但可以!我的观察很少,我可以将JS代码隐藏在index.html中吗?可以从前端更改状态吗?这种方法有多安全?