我如何在进行交易 Braintree 时生成客户 ID

How do i generate a customer id while doing transaction Braintree

我的目标是在生成客户 ID 的同时生成交易销售,以便我可以将客户 ID 存储到数据库中

我需要客户 ID 的原因是因为同一个用户不需要再次输入他的 credit/debit 卡

const gateway = braintree.connect({
  environment: braintree.Environment.Sandbox,
  merchantId: '',
  publicKey: '',
  privateKey: ''
});

app.post('/payment', (req, res, next) => {

  gateway.transaction.sale({
    amount: req.body.amount,
    paymentMethodNonce: req.body.nonce,
    options: {
      submitForSettlement: true
    }
  }, function (err, result) {
    if (err) {
      res.json(err)

    }
    if (result.success) {
      console.log('Transaction ID: ' + result.transaction.id);
      res.json({
        transactionId: result.transaction.id
      })
    } else {
      console.error(result.message);
    }
  });

});

完全披露:我在 Braintree 工作。如果您有任何其他问题,请随时联系 support

一种选择是使用 storeInVaultOnSuccess 标志。如果交易成功,付款方式将存储在您的 Braintree Vault 中。

如果您还为 Braintree Vault 中的现有记录传入 customerId,则生成的存储付款方式将与该客户相关联。否则,将为付款方式创建新的客户记录。您可以像这样访问结果对象上的新客户 ID:

gateway.transaction.sale({
  amount: "10.00",
  paymentMethodNonce: "fake-valid-nonce",
  options: {
    submitForSettlement: true,
    storeInVaultOnSuccess: true
  }
}, function (err, result) {
  if (err) {
    // handle err
  }

  if (result.success) {
    console.log('Transaction ID: ' + result.transaction.id);
    console.log('Customer ID: ' + result.transaction.customer.id);
  } else {
    console.error(result.message);
  }
});