在回调内部调用函数

时间:2018-07-03 16:37:09

标签: javascript function callback

我正在尝试调用在回调函数中两次被掩埋的函数。我正在使用聚合物+电子+ PayPal-Node-SDK,以便使用PayPal作为支付系统来创建一个简单的商店。

您将在示例中看到,我需要调用的函数是execute()

pay_token_one(){

            var pvalue = this.value;
            var pamount = this.amount;
            var pcost = this.cost;

            pcost = pvalue * pamount; 

            var user = firebase.auth().currentUser;
            var key = this.$.query.ref.push({
              price: pvalue,
              quantity: pamount,
              total: pcost,
            }).getKey();

            var create_payment_json = {
                  "intent": "sale",
                  "payer": {
                      "payment_method": "paypal"
                  },
                  "redirect_urls": {
                      "return_url": "http://127.0.0.1:8081/index.html?key=" + key + "&uid=" + user.uid,
                      "cancel_url": "http://cancel.url"
                  },
                  "transactions": [{
                      "item_list": {
                          "items": [{
                              "name": "Program Token",
                              "sku": "Digital Currency",
                              "price": pvalue,
                              "currency": "USD",
                              "quantity": pamount
                          }]
                      },
                      "amount": {
                          "currency": "USD",
                          "total": pcost
                      },
                      "description": "Application."
                  }]
              };

              paypal.payment.create(create_payment_json, function (error, payment) {
                  if (error) {
                      throw error;
                  } else {
                      console.log("Create Payment Response");
                      console.log(payment);

                      var mywindow = window.open(payment.links[1].href);

                      var timer = setInterval(function() { 
                          if(mywindow.closed) {
                              clearInterval(timer);
                              //we need to call execute();
                              return;
                          }
                      }, 1000);
                  }
              });
          }

          execute(){

              var user = firebase.auth().currentUser;
              var ref1 = firebase.database().ref("vendor/" + user.uid + "/orders");
              var info;

              ref1.child(key).once("value").then(function(snapshot){
                  info = [ snapshot.val().payerid, snapshot.val().total, snapshot.val().payid ];
                  console.log(info);
              });

              var execute_payment_json = {
                  "payer_id": info[0],
                      "transactions": [{
                          "amount": {
                              "currency": "USD",
                              "total": info[1]
                          }
                      }]
              };

              console.log(execute_payment_json);

              paypal.payment.execute(info[2], execute_payment_json, function (error, payment) {
                  if (error) {
                      console.log(error.response);
                      throw error;
                  } else {
                      console.log("Get Payment Response");
                      console.log(JSON.stringify(payment));
                  }
              });
          }

任何帮助将不胜感激。谢谢你一百万。

1 个答案:

答案 0 :(得分:2)

          var context = this;
          paypal.payment.create(create_payment_json, function (error, payment) {
              if (error) {
                  throw error;
              } else {
                  console.log("Create Payment Response");
                  console.log(payment);

                  var mywindow = window.open(payment.links[1].href);

                  var timer = setInterval(function() { 
                      if(mywindow.closed) {
                          clearInterval(timer);
                          context.execute();
                          return;
                      }
                  }, 1000);
              }
          });