最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何从服务器发送这段数据并在客户端使用 Javascript?

网站源码admin39浏览0评论

如何从服务器发送这段数据并在客户端使用 Javascript?

如何从服务器发送这段数据并在客户端使用 Javascript?

好吧,我想要完成的是使用 Stripe 的 API 和 Javascript:

创建订阅

提交付款

提交表单时,completeSignUp 函数执行并运行服务器代码。我不知道该怎么做(因为这是使用“then”函数——我最初没有写这段代码)是将 clientSecret 发送到客户端,然后在客户端上获取 clientSecret,这样我就可以使用它向 Stripe 提交付款。条带代码使用“await”函数来获取数据,但我不知道如何在使用“then”函数启动进程时实现它。

这是我当前的代码:

客户文件

async completeSignUp() {
  this.processingSigningUp = true;
  
  const { token, error } = await createToken();
  if (error) {
    Message.error(error.message);
    this.processingSigningUp = false;
    return;
  }
  
  const newUser = Object.assign({ stripeTokenId: token.id,
    accountType: this.accountType(),
    subscriptionType: this.subscriptionType }, this.form);
    
  this.$store.dispatch(AUTH_REGISTER, newUser).then(() => {
  
    this.email = '';
    this.password = '';
    this.passwordCheck = '';
    this.processingSigningUp = false;
    this.$destroy();
    
    this.$router.push('/downloads');
    
  }).catch((er) => {
  
    this.processingSigningUp = false;
    if (er === 'card_already_used_for_trial') {
      this.skipTrialRequired = true;
      Message.error('You are not eligible for the trial');
      this.isEverythingValid();
    } else {
      Message.error(er);
    }
    
  });
},

服务器文件

SubscriptionSchema.statics.createNew = async function (customer, plan, skipTrial, callback) {

const stripeSubscription = await stripe.subscriptions.create({
  customer: customer.stripeId,
  billing: 'charge_automatically',
  payment_behavior: 'default_incomplete',
  payment_settings: { save_default_payment_method: 'on_subscription' },
  items: [{ plan: plan.stripeId }],
  trial_from_plan: !skipTrial,
  expand: ['latest_invoice.payment_intent'],
});

res.send({
  clientSecret: stripeSubscription.latest_invoice.payment_intent.client_secret,
});

const subscription = {
  belongsTo: customer.id,
  plan: plan.nickname,
  stripeId: stripeSubscription.id,
  status: stripeSubscription.status,
  cancel_at_period_end: stripeSubscription.cancel_at_period_end,
  currentPeriodEnd: new Date(stripeSubscription.current_period_end * 1000),
  currentPeriodStart: new Date(stripeSubscription.current_period_start * 1000),
};

return (new this(subscription)).save(callback);

}
回答如下:
发布评论

评论列表(0)

  1. 暂无评论