Paypal在快速结账时突然不会调用“onAuthorize”方法

时间:2017-12-07 14:29:35

标签: javascript node.js rest paypal express-checkout

我今天的问题是我有一个带快速结账的Web应用程序来执行。 我使用客户端 - 服务器架构来避免安全问题。 所以我的客户端代码如下:

paypal.Button.render({

        env : 'sandbox',
        commit: true, 
        style: {
            size: 'medium',
            color: 'gold',
            shape: 'pill',
            label: 'checkout'
        },
        locale: 'en_US',
        payment: function() {
            nickname = $("#nickname").val();
            amount = $("#amount").val();
            description = $("#description").val();
            data = {
                "nickname" : nickname,
                "amount" : amount,
                "description" : description
            };
            return new paypal.Promise(function(resolve, reject) {

                // Call your server side to get the Payment ID from step 3, then pass it to the resolve callback
                jQuery.post("/newPayment", data).done(function(data){
                    console.log("HEI I GOT THE PAYMENT JSON = " + data);
                    parsed_data = JSON.parse(data);
                    resolve(parsed_data['id']);
                });
            });
        },

        onAuthorize: function(data, actions) {
            console.log("JSON = " + JSON.stringify(actions.payment.getTransactions()[0]));
            console.log("TRANSACTION =  " + actions.payment.getTransactions()[0])
            console.log("PAYMENT SUCCESSFUL");
            return actions.payment.execute();
        },
        onError: function(data, actions){
            console.log("ERRORRRRRR");
            $("#warning").show();
        }

    }, '#paypal-button');

我的服务器端代码:

app.post("/newPayment",function(req,res){
    console.log("RECEIVING POST WITH AMOUNT = " + req.body.amount);

            //CONFIGURE PAYMENY - PAYPAL PHASE 1
  var first_config = {
      'mode': 'sandbox',
      'client_id': '<MY CLIENT ID>',
      'client_secret': '<MY SECRET>'
  };

  paypal.configure(first_config);

  console.log("AMOUNT AUTHORIZED = " + req.body.amount);

  //CREATING PAYMENT
  PAYMENT = {
      "intent": "sale",
      "payer": {
          "payment_method": "paypal"
      },
      "redirect_urls": {
          "return_url": "http://return.url",
          "cancel_url": "http://cancel.url"
      },
      "transactions": [{
          "amount": {
              "currency": "EUR",
              "total": req.body.amount
          },
          "description": "This is the payment description."
      }]
  };


  //CREATING PAYMENT AND SENDING IT TO THE CLIENT
  paypal.payment.create(PAYMENT, function (error, payment) {
      if (error) {
          throw error;
      } else {
          console.log("Create Payment Response");
          res.send(JSON.stringify(payment));
      }
  });

现在,paypal窗口打开,我可以插入所有数据来执行测试付款。 但是,当我确认付款和Paypal服务应该调用我的“onAuthorize”方法时,而不是“onError”会启动......

可能是什么问题? 重定向网址? 事实上我应该在它之后使用.then()执行paypal.configure()以确保操作流程?

我真的在试图使用Paypal服务爆炸

编辑: 在cloudno.de上推送我的代码,付款正确执行,但在本地运行它不起作用......我不知道该怎么说ahah

1 个答案:

答案 0 :(得分:0)

actions.payment.getTransactions不是一个功能。您需要致电actions.payment.get().then(function(result) { ... })