How can I get already saved credit/debit card details to use in my custom module from checkout page before place order.
-
you need to check vault functionality in Magento.– CharlieCommented Jul 25, 2018 at 14:51
-
what is your exact requirement.– Narendra VyasCommented Mar 25, 2019 at 5:40
-
@Yogita : Have able to achive your requirment. Actually I also want that. I have saved card details in db but now I want to use that card details for order place.– Balwant SinghCommented Jan 31, 2020 at 20:01
2 Answers
I was able to load a customer's saved card info from their customerId
by using the following:
<?php
namespace Company\Mondule\Controller\Adminhtml\Order;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Vault\Api\PaymentTokenManagementInterface;
class Connector extends Action
{
protected $customerRepositoryFactory;
protected $paymentTokenManagement;
public function __construct(
Context $context,
PageFactory $pageFactory,
CustomerRepositoryInterface $customerRepositoryInterface,
PaymentTokenManagementInterface $paymentTokenManagement,
array $data = []
) {
$this->customerRepositoryInterface = $customerRepositoryInterface;
$this->paymentTokenManagement = $paymentTokenManagement;
parent::__construct($context, $data);
}
public function execute()
{
// CUSTOMER
$customerId = '1';
$customer = $this->customerRepositoryInterface->getById($customerId);
// Get Saved CC
$cardList = $this->paymentTokenManagement->getListByCustomerId($customerId);
foreach($cardList as $card) {
if ($card->getIsActive()) {
echo $card->getData();
}
}
}
}
I should note that this is just a snippet from our implementation. Without seeing any of your code, you may need to alter it some depending on your application.
No, you can retrieve only last four credit card digits from an order in Magento's flow for security reasons.
use event (checkout_onepage_controller_success_action
) and observer to get order details
//***if orderid=1001;***
$orderId=1001;
$order = $this->order->load($orderId);
$expmonth=$order->getPayment()->getCcExpMonth(); //get expirymonth*
$expyear=$order->getPayment()->getCcExpYear(); //get expiryyear*
$cclast4=$order->getPayment()->getCcLast4(); //get expiry last four digit from card*