{"id":11061,"date":"2025-03-26T19:00:00","date_gmt":"2025-03-26T19:00:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=11061"},"modified":"2025-07-15T16:33:10","modified_gmt":"2025-07-15T11:03:10","slug":"remove-vault-information-from-magento-2-credit-memos","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/remove-vault-information-from-magento-2-credit-memos\/","title":{"rendered":"Remove Vault Information from Magento 2 Credit Memos: Simple Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Many payment gateways offer a vault feature that allows you to save your customers card details for future transactions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a customer opts to save their card information, it is securely stored for subsequent use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if an order is canceled for any reason, the stored payment data might inadvertently be included when generating a credit memo, which could pose security concerns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can remove that vault information programmatically from the Magento 2 credit memo.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the solution.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Remove Vault Information from Magento 2 Credit Memos<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Start with creating an events.xml file at the<strong> Vendor\/Extension\/etc<\/strong> directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"sales_order_creditmemo_save_after\">\n        &lt;observer name=\"remove_vault_info_on_creditmemo\" instance=\"Vendor\\Extension\\Observer\\RemoveVaultInfo\"\/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, create a<strong> RemoveVaultInfo.php file <\/strong>insid<strong>e Vendor\/Extension\/Observer<\/strong> directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Observer;\n\nuse Magento\\Framework\\Event\\Observer;\nuse Magento\\Framework\\Event\\ObserverInterface;\nuse Magento\\Vault\\Api\\PaymentTokenManagementInterface;\nuse Magento\\Vault\\Api\\PaymentTokenRepositoryInterface;\nuse Magento\\Sales\\Model\\Order\\Creditmemo;\n\nclass RemoveVaultInfo implements ObserverInterface\n{\n    protected $paymentTokenManagement;\n    protected $paymentTokenRepository;\n\n    public function __construct(\n        PaymentTokenManagementInterface $paymentTokenManagement,\n        PaymentTokenRepositoryInterface $paymentTokenRepository\n    ) {\n        $this->paymentTokenManagement = $paymentTokenManagement;\n        $this->paymentTokenRepository = $paymentTokenRepository;\n    }\n\n    public function execute(Observer $observer)\n    {\n        $creditmemo = $observer->getEvent()->getCreditmemo();\n        if ($creditmemo instanceof Creditmemo) {\n            $order = $creditmemo->getOrder();\n            $payment = $order->getPayment();\n            $customerId = $order->getCustomerId();\n            $paymentMethod = $payment->getMethod();\n\n            \/\/ Retrieve the payment token\n            $paymentToken = $this->paymentTokenManagement->getByGatewayToken(\n                $payment->getAdditionalInformation('token'),\n                $paymentMethod,\n                $customerId\n            );\n\n            if ($paymentToken) {\n                \/\/ Delete the payment token\n                $this->paymentTokenRepository->delete($paymentToken);\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using the simple code, you can remove any associated stored payment tokens or vault information from the credit memo.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!&nbsp;<br>Try this code yourself and remove the vault information using this solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many payment gateways offer a vault feature that allows you to save your customers card details for future transactions. When a customer opts to save their card information, it is securely stored for subsequent use. However, if an order is canceled for any reason, the stored payment data might inadvertently be included when generating a [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":11069,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-11061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=11061"}],"version-history":[{"count":6,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11061\/revisions"}],"predecessor-version":[{"id":17819,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11061\/revisions\/17819"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media\/11069"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=11061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=11061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=11061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}