{"id":53978,"date":"2016-07-13T17:44:06","date_gmt":"2016-07-13T17:44:06","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=53978"},"modified":"2026-03-11T13:30:13","modified_gmt":"2026-03-11T13:30:13","slug":"create-payment-transaction-in-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/","title":{"rendered":"How to Programmatically create Transaction in Magento 2"},"content":{"rendered":"\n<p>In this article we learn how to create transactions in Magento 2 . A transaction is proof that payment has been done. <\/p>\n\n\n\n<p>In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually. <\/p>\n\n\n\n<p>However, this changes when implementing an online <a href=\"https:\/\/store.webkul.com\/Magento-2\/Payment-Extensions.html\">payment method<\/a> that redirects customers to an external payment gateway.<\/p>\n\n\n\n<p>When the customer completes the payment and is redirected back to the merchant\u2019s website with payment data, the transaction may need to be created manually. <\/p>\n\n\n\n<p>In such cases, developers must use a code snippet to create the transaction programmatically:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">public function createTransaction($order = null, $paymentData = array())\n{\n    try {\n        \/\/get payment object from order object\n        $payment = $order-&gt;getPayment();\n        $payment-&gt;setLastTransId($paymentData&#091;&#039;id&#039;]);\n        $payment-&gt;setTransactionId($paymentData&#091;&#039;id&#039;]);\n        $payment-&gt;setAdditionalInformation(\n            &#091;\\Magento\\Sales\\Model\\Order\\Payment\\Transaction::RAW_DETAILS =&gt; (array) $paymentData]\n        );\n        $formatedPrice = $order-&gt;getBaseCurrency()-&gt;formatTxt(\n            $order-&gt;getGrandTotal()\n        );\n\n        $message = __(&#039;The authorized amount is %1.&#039;, $formatedPrice);\n        \/\/get the object of builder class\n        $trans = $this-&gt;_transactionBuilder;\n        $transaction = $trans-&gt;setPayment($payment)\n        -&gt;setOrder($order)\n        -&gt;setTransactionId($paymentData&#091;&#039;id&#039;])\n        -&gt;setAdditionalInformation(\n            &#091;\\Magento\\Sales\\Model\\Order\\Payment\\Transaction::RAW_DETAILS =&gt; (array) $paymentData]\n        )\n        -&gt;setFailSafe(true)\n        \/\/build method creates the transaction and returns the object\n        -&gt;build(\\Magento\\Sales\\Model\\Order\\Payment\\Transaction::TYPE_CAPTURE);\n\n        $payment-&gt;addTransactionCommentsToOrder(\n            $transaction,\n            $message\n        );\n        $payment-&gt;setParentTransactionId(null);\n        $payment-&gt;save();\n        $order-&gt;save();\n\n        return  $transaction-&gt;save()-&gt;getTransactionId();\n    } catch (Exception $e) {\n        \/\/log errors here\n    }\n}<\/pre>\n\n\n\n<p>In the above code, I have created a function that will accept two params,<br>the first parameter is $order which is of type Magento\\Sales\\Model\\Order,<br>you need to load the order with order id, I suppose you know about this .<\/p>\n\n\n\n<p>And the second param $paymentData is assumed to be an array that is returned by the<br>payment gateway after successful pament.<\/p>\n\n\n\n<p>We assume that the $paymmentData[&#8216;id&#8217;] holds the id of the successful payment.<br>$this-&gt;_transactionBuilder is of type :<br>Magento\\Sales\\Model\\Order\\Payment\\Transaction\\BuilderInterface,<br>you don&#8217;t need to worry about the Interface magento2 already defines it in<br>sales-module di.xml, preference class for the interface.<\/p>\n\n\n\n<p>When the build method is called on the builder class after setting the required<br>params, it automatically creates the transaction object and saves the transaction<br>within the order.<\/p>\n\n\n\n<p>Thats all in this, hope it will help you, try the above code and<br>if you have any issue just comment below \ud83d\ude42 .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we learn how to create transactions in Magento 2 . A transaction is proof that payment has been done. In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually. However, this changes when implementing an online payment method that redirects customers <a href=\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,1],"tags":[],"class_list":["post-53978","post","type-post","status-publish","format-standard","hentry","category-magento2","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Create Transaction In Magento2 Programmatically<\/title>\n<meta name=\"description\" content=\"In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Create Transaction In Magento2 Programmatically\" \/>\n<meta property=\"og:description\" content=\"In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-07-13T17:44:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-11T13:30:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ashutosh Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashutosh Srivastava\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\"},\"author\":{\"name\":\"Ashutosh Srivastava\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\"},\"headline\":\"How to Programmatically create Transaction in Magento 2\",\"datePublished\":\"2016-07-13T17:44:06+00:00\",\"dateModified\":\"2026-03-11T13:30:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\"},\"wordCount\":261,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\",\"name\":\"How To Create Transaction In Magento2 Programmatically\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2016-07-13T17:44:06+00:00\",\"dateModified\":\"2026-03-11T13:30:13+00:00\",\"description\":\"In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Programmatically create Transaction in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\",\"name\":\"Ashutosh Srivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ashutosh Srivastava\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ashutosh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Create Transaction In Magento2 Programmatically","description":"In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/","og_locale":"en_US","og_type":"article","og_title":"How To Create Transaction In Magento2 Programmatically","og_description":"In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually.","og_url":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-07-13T17:44:06+00:00","article_modified_time":"2026-03-11T13:30:13+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ashutosh Srivastava","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ashutosh Srivastava","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/"},"author":{"name":"Ashutosh Srivastava","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970"},"headline":"How to Programmatically create Transaction in Magento 2","datePublished":"2016-07-13T17:44:06+00:00","dateModified":"2026-03-11T13:30:13+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/"},"wordCount":261,"commentCount":9,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/","url":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/","name":"How To Create Transaction In Magento2 Programmatically","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2016-07-13T17:44:06+00:00","dateModified":"2026-03-11T13:30:13+00:00","description":"In Magento, a transaction is automatically created when an invoice is generated, so developers usually don\u2019t need to handle it manually.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-payment-transaction-in-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Programmatically create Transaction in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970","name":"Ashutosh Srivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ashutosh Srivastava"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ashutosh\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/53978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=53978"}],"version-history":[{"count":10,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/53978\/revisions"}],"predecessor-version":[{"id":530108,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/53978\/revisions\/530108"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=53978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=53978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=53978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}