首页 文章

Angular POST路由未连接到Nodejs以进行Stripe集成

提问于
浏览
0

我正在我的网络应用程序中集成Stripe结帐 . 我已经在角度前端实现了Stripe checkout,并且还创建了一个后端,该后端应该接收条带检出传递的令牌 . 提交条带结帐表单后,我的POST http请求不会将数据传递给后端 . 虽然我从Stripe获得200状态,但我的nodejs没有响应 .

这是我的表单调用的checkout方法 .

openCheckout() {
    let total = (this.donation * 100);
    let handler = (<any>window).StripeCheckout.configure({
      key: 'key_test',
      locale: 'auto',
      token: (token: any) => {
        const transaction = new Charge(total, token.id);
        console.log('From navbar nonObject ' + token.id + ' ' + total);
        console.log(transaction + ' From navbar');
        this.keyService.charge(transaction);
      }
    });
    handler.open({
      name: 'Delaware March for Jesus',
      description: 'Donation',
      amount: total
    });
    this.donation = 0;
    this.donationEmail = '';
  }

这是我的服务代码,它实现了收费并将令牌传递给后端 .

charge(transaction: Charge) {
    const body = JSON.stringify(transaction);
    const headers = new Headers({'Content-type': 'application/json'});
      return this.http.post(this.keysUrlDev + '/charge', body, { headers: headers })
          .map((response: Response) => response.json())
          .catch((error: Response) => Observable.throw(error.json()));
  }

我为交易构建的简单角度模型 .

export class Charge {
  constructor(public amount: number,
              public token: string) {}
}

我在nodejs上的POST路由获取令牌并将其传递给条带库charge.create方法 .

router.post('/charge', function(req, res, next) {
  var amount = req.body.amount;
  var token = req.body.token;
  stripe.charges.create({
    amount: amount,
    currency: 'usd',
    description: 'Delaware March For Jesus Donation',
    source: token
  }, function(err, charge) {
    if (err) {
      console.log(req.body.amount + ' From POST' + req.body.token);
      return res.status(500).json({
        title: 'An error occured',
        error: err
      });
    }
    res.status(201).json({
      message: 'Charged successfully',
      obj: charge
    });
  });
});

我从前端获得了令牌并成功通过Postman发送了POST请求 . 其中记录成功的事务并将其显示在我的条带帐户中 . 但是,当通过角度发送请求时,没有发生这种情况 .

我已经使用console.log来跟踪代码停止的位置,我可以在keyService计费方法中检索令牌和金额 . 所以它必须是http.post不能正常工作 .

1 回答

  • 1

    我几乎完全具有相同类型的问题,并且使这对我有用的更改是删除InMemoryWebApiModule.forRoot(InMemoryDataService),并将API构建到我的快速后端中,作为简单的剪切和粘贴 . (我最初使用了Angular'Heroes'教程,并在那里实现了内存中的API . )

    如果我广泛地理解这个问题,那就是这个内存中的API取代了Angular的HTTP的常规功能 . 一旦发布它再次正常运行 - 虽然我不能在技术上更具体 .

    我希望它对你也有用 .

相关问题