Meteor从服务器端方法重定向客户端

时间:2018-04-07 03:51:34

标签: reactjs meteor paypal-subscriptions react-router-dom

Meteor 1.6,React,React Router

与Paypal结算协议接口

客户端onClick事件:

subPayPal(){
  Meteor.call('paypal.getToken', this.props.user_id, (error, response) => {
    if(error) { 
      console.warn(error);
    }else{
      console.log(response);
    }
  }
}

我在服务器而不是客户端上调用该方法,因为我处理特权信息(用户名,密码,令牌等)。这里没问题。

服务器方法:

Meteor.methods({
   'paypal.getToken': function getOauthToken(uid){
      // simplified a bit
      // check if current token valid
      // set vars here, then go get token
      axios.post(ppAPI, data, config)
       .then( function(response) {
          // save oAuth token
       })
       .catch( function(error){
         // stuff here
       })

      // prepare another axios.post(), with oauth Token, to get a payment token 
      // and approval_url and execute_url from paypal

     // call axios.post() and use data in response:

     // with the approval_url in this reponse, I need to redirect
     // the browser to the approval_url on paypal.com so that the user
     // can sign into paypal, and ok the subscription agreement.

    // once user 'ok' in paypal, the browser comes back to my site,
    // where I render a 'cart' with a final 'ok, purchase' button.
    return approval_url;
   }
})

因此,一旦我获得了approval_url,我就可以将其发送回客户端,当客户端"看到"然后,它可以将React Router调用到paypal.com网站。

问题

客户端的onClick方法显然是异步的,只要我点击初始onClick(),console.log会为undefined输出process,这非常有意义。

试用1

所以,我尝试在客户端尝试使Meteor.apply同步,等待服务器端方法返回approval_url无效:

subPayPal(){
  Meteor.apply('paypal.getToken', [{uid:user_id}], {
    onResultReceived: (error, response) => {
      if(error) console.warn(error.reason);
      if(response) console.log('server response', response);
    }
  });
} 

我也试过Meteor.call('paypal.getToken').then({ console.log(response) }).catch...无效,因为它仍然是异步的。

我也在服务器端方法中尝试过try / catch块,但无济于事。无论我尝试什么,服务器端代码总是按预期运行(除了Meteor.call().then().catch()只是普通的失败)。我似乎无法承诺,或退回它。

试用2

下一个想法是不关心客户端上的响应,如果我可以获得定义了approval_url的服务器端方法,以某种方式调用客户端上的React Router并推送唯一的{{ 1}}它,但这对我来说没有多大意义如何连接它。

思考1

我想我可以使用一些"临时"数据库集合,它是被动的,因此当服务器端方法完成时,它将更新(或插入/创建)文档,并且当客户端看到'然后,它可以调用React Router将浏览器重定向到paypal.com。集合文档必须保存approval_url URI,然后将其传递给客户端。不太确定我是如何连接客户端告诉路由器何时看到approval_url"出现"。

... SOOOOO

有什么想法吗?是否有多个解决方案(如果有,哪个最好?)。

我在某处读到应用程序应该注销用户,然后路由器可以重定向到paypal,但这并没有帮助,因为paypal.com重定向回到我身边,我不想要用户必须重新登录。

感谢。

0 个答案:

没有答案
相关问题