首页 文章

带有patrickml的meteor:braintree代码必须始终在光纤内运行

提问于
浏览
0

此Meteor服务器代码(主要是从其他人复制和粘贴)给出了以下错误:

错误:流星代码必须始终在光纤内运行 . 尝试使用Meteor.bindEnvironment包装传递给非Meteor库的回调 .

它将支付很好地发送到Braintree沙箱,但是一旦完成“看到服务器代码的底部”,我需要在服务器上做一些东西,它是如何修复的?

我试过 'createTransaction: Meteor.wrapAsync(function (nonceFromTheClient, Payment) {...});) 但无济于事 .

//client.account.js
Template.account.onRendered(function () { //6a
  Meteor.call('getClientToken', function (error, clientToken) {
    if (error) {
      console.log(error);
    } else {
      braintree.setup(clientToken, "dropin", {
        container: "payment-form",
        onPaymentMethodReceived: function (response) {
          var nonce = response.nonce;
          Meteor.call('btCreateCustomer', function (error) {
            if (error) {
              throw new Meteor.Error('customer-creation-failed');
            } else {
              let e = document.getElementById("invoice");
              let label = e.options[e.selectedIndex].text;
              let Payment = {
                amount: /\$(.*?) /g.exec(label)[1],
                period: /(.*? .*?) /g.exec(label)[1]
              };
              Meteor.call('createTransaction', nonce, Payment, function (error) {
                if (!error) {
                  //do stuff.
                }
              });
            }
          });
        }
      });
    }
  });
});

//server.main.js
Meteor.methods({
  'getClientToken': function (clientId) {
    let generateToken = Meteor.wrapAsync(gateway.clientToken.generate, gateway.clientToken);
    let options = {};
    if (clientId) {
      options.clientId = clientId;
    }

    let response = generateToken(options);
    return response.clientToken;
  },
  'btCreateCustomer': function () {
    let user = Meteor.user();
    let customerData = {
      email: user.emails[0].address
    };

    gateway.customer.create(customerData, function (error, response) {
      if (!error) {
        Meteor.users.update(user._id, {
          $set: {
            customerId: response.customer.id
          }
        });
      }
    });
  },
  'createTransaction': function (nonceFromTheClient, Payment) {
    let user = Meteor.user();
    gateway.transaction.sale({
      amount: Payment.amount,
      paymentMethodNonce: nonceFromTheClient, // Generated nonce passed from client
      customer: {
        id: user.customerId
      },
      options: {
        submitForSettlement: true, // Payment is submitted for settlement immediately
        storeInVaultOnSuccess: true // Store customer in Braintree's Vault
      }
    }, function (err) {
      if (!err) {
        // ------------- next line fail due to Fiber issue ------------------ 
        console.log('Paid ' + Payment.amount + " for period " + Payment.period);
      }
    });
  }
});
<template name="account">
  <div id="account">
    <form role="form">
      <div class="row">
        <div class="col-md-6 col-xs-12">
          <div id="payment-form"></div>
          <button type="submit" class="btn btn-success">Submit</button>
        </div>
      </div>
    </form>
  </div>
</template>

1 回答

  • 0

    尝试在Meteor.bindEnvironment中包装回调:

    gateway.transaction.sale({/* args */}, Meteor.bindEnvironment(function(err) {
      if (!err) {
        console.log('Nice!');
      }
    }));
    

相关问题