Angular POST路由未连接到Nodejs以进行Stripe集成 [英] Angular POST route not connecting to Nodejs for Stripe integration

查看:139
本文介绍了Angular POST路由未连接到Nodejs以进行Stripe集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm integrating the Stripe checkout in my web app. I've implemented Stripe checkout on the angular front end and have also created a backend that is supposed to receive the token passed by stripe checkout. Upon submission of the stripe checkout form, my POST http request is not passing data to the backend. Although I get a 200 status from Stripe, I get no response for my nodejs.

这是我的表单调用的结帐方法。

Here is my checkout method invoked by a form.

 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 = '';
  }

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

Here is my Service code that implements the charge and passes the token to the backend.

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()));
  }

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

A simple angular model I constructed for transactions.

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

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

And my POST route on nodejs that takes the token and passes it through the stripe library charge.create method.

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请求。其中记录成功的事务并将其显示在我的条带帐户中。但是,当通过angular发送请求时,没有发生这种情况。

I've gotten the token from the front end and sent a POST request via Postman successfully. Which logs the successful transaction and shows it in my stripe account. But, none of that happens when sending the request via angular.

我使用console.log来跟踪代码停止的位置,我可以检索令牌和金额关键服务收费方法。所以它必须是http.post无法正常工作。

I've used console.log to trace where the code stops and I can retrieve the token and amount in the keyService charge method. So it must be http.post that is not working properly.

推荐答案

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

I was having the same type of issue almost exactly, and the change that made this work for me was to remove the InMemoryWebApiModule.forRoot(InMemoryDataService), and build the API into my express backend as a straightforward cut-and-paste. (I originally used the Angular 'Heroes' tutorial, and the in-memory API was implemented there.)

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

If I understand the issue broadly, it is that this in-memory API took over from the regular functioning of Angular's HTTP. Once released it functioned normally again - though I can't be more technically specific.

我希望它也适用于你。

这篇关于Angular POST路由未连接到Nodejs以进行Stripe集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆