{"id":43,"date":"2015-12-15T14:31:15","date_gmt":"2015-12-15T14:31:15","guid":{"rendered":"https:\/\/easydigitaldownloads.com\/development\/?p=43"},"modified":"2021-12-15T15:32:28","modified_gmt":"2021-12-15T21:32:28","slug":"introducing-edd_payment","status":"publish","type":"post","link":"https:\/\/easydigitaldownloads.com\/development\/2015\/12\/15\/introducing-edd_payment\/","title":{"rendered":"Introducing EDD_Payment"},"content":{"rendered":"<p>In the <a href=\"https:\/\/easydigitaldownloads.com\/development\/2015\/12\/03\/version-2-5-beta-1\/\">recent weeks<\/a> <a href=\"https:\/\/easydigitaldownloads.com\/development\/2015\/12\/11\/version-2-5-beta-2\/\">we&#8217;ve told you about<\/a> the upcoming Easy Digital Downloads 2.5 release. Version 2.5, as stated, has been in development for quite some\u00a0time, and most of that is due to one major enhancement being introduced: The EDD_Payment class.<\/p>\n<h3>Background<\/h3>\n<p>A centralized way of interacting and manipulating a payment in EDD has been something we&#8217;ve needed for a while. Prior to 2.5 and EDD_Payment, to get or set any data for a payment you had to do a series of calls to the <code>edd_get\/update_payment_meta()<\/code> functions, which was simply a wrapper for <code>get\/update_post_meta()<\/code>. Not only was getting and updating payments lengthy, but creating them was especially painstaking. While we provided <code>edd_insert_payment()<\/code> as a helper function, we relied on you (the developers), to provide us with a properly crafted array of data to create a payment.\u00a0The introduction of EDD_Payment is our first step in giving you a better experience in payment management.<\/p>\n<h3>A Quick Example<\/h3>\n<p>So before getting into too much detail, let&#8217;s take a quick look at how you would have inserted a payment prior to EDD 2.5:<br \/>\nhttps:\/\/gist.github.com\/cklosowski\/29b6c4b245355cfc32cd<\/p>\n<p>You can see in this, that much of the code is used in creating a properly formatted set of arrays to nest inside the <code>$payment_data<\/code> array.<\/p>\n<p>Here&#8217;s the same type of payment created, with EDD_Payment:<br \/>\nhttps:\/\/gist.github.com\/cklosowski\/0df45b1d476c71357ee7<\/p>\n<p>You can quickly identify that it&#8217;s much more friendly to interact with. No more creating arrays of data, no more price determination. EDD_Payment even handles the the inserting of the payment if it detects you are creating a new payment.<\/p>\n<h3>Backwards compatible<\/h3>\n<p>One of the primary efforts of <code>EDD_Payment<\/code> was making it backwards compatible. If you use the <code>edd_insert_payment()<\/code> function or any of the helper functions to get or update payment meta, we&#8217;ve updated those functions to use <code>EDD_Payment<\/code>, so you don&#8217;t need to worry. Along with that, all the filters that existed in the helper functions have been moved into <code>EDD_Payment<\/code> so that when you call <code>new EDD_Payment( 123 )<\/code>, anything that uses those filters will be executed as well, and applied to the class you just instantiated.<\/p>\n<h3>More practical use cases<\/h3>\n<p>The above example is a pretty in depth example of creating a payment, so let&#8217;s dive into some smaller examples of ways to use the <code>EDD_Payment<\/code>.<\/p>\n<h4>Creating a payment<\/h4>\n<pre>\r\n$payment = new EDD_Payment(); \/\/ Instantiates a payment object\r\n$payment->add_download( 12 ); \/\/ Adds download id 12 to the payment at it's default price\r\n$payment->email = 'lisa@example.org'; \/\/ Email is required for a payment\r\n$payment->status = 'complete'; \/\/ Completes the payment\r\n$payment->save(); \/\/ Writes to the database any changes that were made\r\n<\/pre>\n<h4>Editing an existing payment<\/h4>\n<pre>\r\n$payment = new EDD_Payment( 42 ); \/\/ Loads the information for Payment ID 42\r\n$payment->email = 'john@example.org'; \/\/ Changes the email address on the payment\r\n$payment->save();\r\n<\/pre>\n<h4>Changing a payment status<\/h4>\n<pre>\r\n$payment = new EDD_Payment( 1337 );\r\n$payment->status = 'revoked';\r\n$payment->save();\r\n<\/pre>\n<h4>Adding a variable priced download<\/h4>\n<pre>\r\n$payment = new EDD_Payment( 1942 );\r\n$args = array(\r\n    'price_id' => 1, \/\/ Variable price ID\r\n    'item_price' => 0.00, \/\/ Makes the item free\r\n);\r\n$payment->add_download( 23, $args ); \/\/ Adds Download ID 23, variable price 1 to the payment\r\n$payment->save();\r\n<\/pre>\n<h4>Adding a download with options<\/h4>\n<pre>\r\n$payment = new EDD_Payment( 2008 );\r\n$args = array(\r\n    'item_price' => 4.00,\r\n);\r\n$options = array(\r\n    'is_renewal' => true,\r\n);\r\n$payment->add_download( 28, $args, $options );\r\n$payment->save();\r\n<\/pre>\n<h4>Removing a download<\/h4>\n<pre>\r\n$payment = new EDD_Payment( 2008 );\r\n$payment->remove_download( 23 );\r\n$payment->save();\r\n<\/pre>\n<h4>Removing a download with variable pricing<\/h4>\n<pre>\r\n$payment = new EDD_Payment( 2008 );\r\n$args = array(\r\n    'price_id' => 1,\r\n);\r\n$payment->remove_download( 28, $args );\r\n$payment->save();\r\n<\/pre>\n<h3>EDD_Payment as a Final<\/h3>\n<p>If you dig into the Class, the first thing you&#8217;ll notice is that we&#8217;ve marked the <code>EDD_Payment<\/code> class as a <code>final<\/code>, which means you cannot extend it via your own class&#8230;yet. This is to help us really solidify it as a class prior to you, the developers, taking it to the next level and doing awesome stuff with <code>EDD_Payment<\/code>. Our intentions are to remove the <code>final<\/code> designation prior to the 2.6 release, but as always that could change depending on what we run into.<\/p>\n<h3>Testing<\/h3>\n<p>As with all beta releases, we encourage testing this out in your development environments and letting us know what you think of the class, and where it could use improvement. It&#8217;s been a long haul getting this ready for you to use. So please leave us some feedback in the comments or over on the <a href=\"https:\/\/github.com\/easydigitaldownloads\/Easy-Digital-Downloads\/issues\/2667\" target=\"_blank\" rel=\"noopener\">Github issue we have for this enhancement<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the recent weeks we&#8217;ve told you about the upcoming Easy Digital Downloads 2.5 release. Version 2.5, as stated, has been in development for quite some\u00a0time, and most of that is due to one major&#8230;<\/p>\n<p class=\"continue-reading\"><a class=\"more-link\" href=\"https:\/\/easydigitaldownloads.com\/development\/2015\/12\/15\/introducing-edd_payment\/\">Continue reading &rarr;<\/a><\/p>\n","protected":false},"author":650,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-core"],"_links":{"self":[{"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/users\/650"}],"replies":[{"embeddable":true,"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":1,"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/posts\/43\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easydigitaldownloads.com\/development\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}