My goal is to generate a transaction sale while generating the customer id so that I could store the customer id to the database
The reason why i need customer id is because so that the same user doesn't need to enter his credit/debit card again
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);
}
});
});
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
One option would be to use the storeInVaultOnSuccess
flag. If the transaction is successful, then the payment method will be stored in your Braintree Vault.
If you also pass in a customerId
for an existing record in your Braintree Vault, then the resulting stored payment method will be associated with that customer. Otherwise, a new customer record will be created for the payment method. You can access the new customer ID on the result object like this:
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);
}
});