带Stripe API的JSON / EJSON错误-发票Webhook事件

时间:2018-12-18 05:19:33

标签: javascript node.js json meteor webhooks

使用附加信息进行更新-12月18日

当我进一步测试时,似乎错误被隔离到“ this.response.end(json);”请参阅以下注释:

条形非发票事件: a)“ this.response.end(EJSON.stringify(obj,{indent:true})));”作品与回报200 b)“ this.response.end(EJSON.stringify(obj));”本地服务器上没有错误,带“无法连接”并且不返回代码200的条状仪表板上的错误

条状发票事件: a)“ this.response.end(EJSON.stringify(obj,{indent:true})));”引发错误-请参阅下面的详细信息。 b)“ this.response.end(EJSON.stringify(obj));”本地服务器上没有错误,带“无法连接”并且不返回代码200的条状仪表板上的错误

任何见识将不胜感激。

我在Stripe API上遇到一些问题,正在寻求帮助或见解。我正在使用ngrok在本地测试Stripe Webhooks,并且一切正常,直到我对任何Stripe“发票”事件类型(例如invoice.payment_succeeded)发出请求为止。当我对任何“发票”事件类型进行测试时,都会遇到几个错误:

  1. 我正在运行的应用程序中断(即要求我在终端中键入meteor run以重新启动应用程序)

  2. 我在终端中收到此服务器端错误消息:

///error message start///
events.js:183
     throw er; // Unhandled 'error' event
     ^

Error: write after end
   at write_ (_http_outgoing.js:622:15)
   at ServerResponse.write (_http_outgoing.js:617:10)
   at IncomingMessage.ondata (_stream_readable.js:639:20)
   at emitOne (events.js:116:13)
   at IncomingMessage.emit (events.js:211:7)
   at IncomingMessage.Readable.read (_stream_readable.js:475:10)
   at flow (_stream_readable.js:846:34)
   at resume_ (_stream_readable.js:828:3)
   at _combinedTickCallback (internal/process/next_tick.js:138:11)
   at process._tickCallback (internal/process/next_tick.js:180:9)
///error message end///
  1. 在我的Stripe仪表板上,我收到一个错误:'Test webhook error: Unable to connect'

同样,只有当我对任何Stripe 'invoice'事件类型(例如invoice.payment_succeeded)发出请求时,这种情况才会中断。

我联系了Stripe,看我是否还有其他需要考虑的地方,但他们说一切都很好。

最后一点,要使用ngrok来运行'ngrok http 3000'

这样说,我的服务器端webhook代码在下面。如果有人对导致此错误的原因有任何见识,那么任何见解的观点都将不胜感激。

///server side webhook code start///
Router.route('/webhooks/stripe', { where: 'server' })
   .get(function () {
       console.log('getter');
       this.response.end('closing...');
   })
   .post(function () {
       console.log('post function initiated');

       // stores payload as string
       var obj = this.request.body;

       console.log("print obj var");
       console.log(obj);

       // saves as indented string to send as response
       var json = EJSON.stringify(obj, {indent: true});

       this.response.writeHead(200, {
           'Content-Length': json.length,
           'Content-Type': 'application/json'
       });

       this.response.end(json);
   })
   .put(function () {
       console.log('put');
   });

///server side webhook code end///

1 个答案:

答案 0 :(得分:0)

我能够解决此问题。作为参考和后代,我删除了下面的注释代码,并手动添加了我的状态代码(即this.response.statusCode = 200;)。这样就可以解决问题。

///code snippet start///
var json = EJSON.stringify(obj, {indent: true});

   //this is the code i removed
       // this.response.writeHead(200, {
       //     'Content-Length': json.length,
       //     'Content-Type': 'application/json'
       // });

   //this is the code i added to send a manual status code
       this.response.statusCode = 200;

       this.response.end(json);
///code snippet end///