I need to Add Credit Card details in to Vault Programmatically (BrainTree) in Magento 2.1.5
Basically what i want is After LoginIn there will be a separate section for Saved Cards . In that Customer is used to Add/edit/delete All his Credit card details.
the Below Code is used to list all the Credit Card saved by the Customer
use MagentoVaultApiPaymentTokenManagementInterface;
use MagentoCustomerModelSession;
...
// Get the customer id (currently logged in user)
$customerId = $this->session->getCustomer()->getId();
// Card list
$cardList = $this->paymentTokenManagement->getListByCustomerId($customerId);
Now what i want is how to Add the Card Details to the Vault ?
Below is the Code to Add card in core php
$result = Braintree_Customer::create(array(
'firstName' => 'first name',
'lastName' => 'last name',
'company' => 'company',
'email' => '[email protected]',
'phone' => '1234567890',
'creditCard' => array(
'cardholderName' => 'xxx xxx',
'number' => '4000 0000 0000 0002 ',
'expirationMonth' => '10',
'expirationYear' => 2020,
'cvv' => '123',
'billingAddress' => array(
'firstName' => 'My First name',
'lastName' => 'My Last name'
)
)
));
But how can i do this same process in magento 2.
Thanks for the help
2
Answers
First, you have to create a payment token from the card data:
Then you can save the payment token:
This is just an example you can start with. In a real world situation, you would also want to check that the token doesn’t already exist, an also try a payment authorisation on the card to make sure it’s actually usable and valid.
In order to check if a payment token exists or not, you can use this:
You can have a look at the core Magento 2 classes mentioned here to know more about the functions available for payment token handling.
Good luck!
Replace objectManager when use in projects