 One of the most interesting things is when you come to your WooCommerce shop, add a product and they're able to actually go view it, then check if you need to update. Unproceed to checking out. Fill in your order form and then when you come to the path for payment you choose your payment that is available and you place your order and your order is received but many countries that are available will find that there's an issue with how most of these payments are received. Some countries don't accept credit cards, others don't have PayPal which are actually some of the commonest ways of managing your payments as comes by default to WooCommerce. So today I'm going to show you how to create your own WooCommerce payment plugin that you can hook it in with your kind of payment that's acceptable in your own country. I'll teach you the basics then you'll be able to apply those and make them better and even more stable for you. When you're ready please release that plugin on the WordPress repo so that all other people can benefit from it. So let's jump into the code. I have created a small folder in my plugins folder and I'm going to just drop it in my editor and it's actually called new payment for WooCommerce and that's what I'm going to call my file so I'll just notice I don't just say WooCommerce new payment because there are actually restrictions in how you use this name which is WooCommerce because it's trademarked. So the first thing that I'm going to do here is actually open up PHP and then I'll start my plugin. I have a little snippet that helps me start up these plugins and so I'll call it new payment for WooCommerce and then I'll add my author link which for now is just that so now we are ready to actually jump in. So I'll save this and then I just go to my plugins section and then just activate it then we can start seeing the difference of what's happening inside our plugin. So I'll just activate that. Now first things first the payment gateway system that you see is actually an extension of the Woo Settings API. If you want to see this you would need to come to WooCommerce and then just look at the developer section that is WooCommerce for developers then you would look at the payment gateways. So you look up something like custom payment gateways and then you'd go to the payment gateway API. They showcase how you can start to build this however it's a little bit complex that's why I just want to break it down and give you a live demo. But when you go to the core gateways for example the caching delivery that we see you find out that your own class of the payment gateway is going to just be an extension of the Woo Settings API. After setting up for our plugin what we're going to do next is actually just activate our plugin and we are good to go. So the first thing that we can do is to check if in our active plugins we have WooCommerce installed. For example right now I actually don't have WooCommerce installed actively it's not activated so we would check to see if that is available so that we end up not failing in the whole process. So we'll do an if statement and then when that passes then we run some code so we'll say if not in array because all our installed plugins are actually saved in an array of form. So we're going to look for if in that array WooCommerce slash WooCommerce with PHP is actually not in there apply some filters so we'll apply filters which is our WordPress function. It actually requires for us to add a tag and for us that tag is going to be active plugins. So we're going to we are looking for the active underscore plugins. Now for the value we need to add something that says get the options and we are getting the active plugins so we're getting get options sorry this is an underscore so get options is a function and then close it up of course and we're going to look for active plugins. So we are looking for that if this is not available for us then what we're going to do is we're going to return who will not allow the plugin to actually process. We're going to start building our class for the gateway but we'll have to check if some particular classes over WooCommerce actually exist. So the first thing that we're going to do is we're going to add an action and this add action we're going to look for one thing so we're going to hook into the plugins loaded so if our plugins are loaded then we're going to look for a function and that function we're going to call node to initialize the new payment then we just need to add a priority of 11 we're not passing any other arguments that's good enough so what we're going to do is create a function and call it node payment in it then we're going to check for our class and then instantiate that class so we're going to look for if the class exists and what class are we looking for we are looking for the payment gateway class so we're looking for the wc payment gateway and that's what we are looking for so if that class exists then we're going to do something and that means we're going to create our new class and it's going to be extending so it will extend it extends the wc pay payment gateway it extends that class and then now we are going to begin to run it so we save this and you won't see anything active that's it but we'll just test our plugin to see if it's still active it should be get optional get optional things actually running into our data base to check so make a correction that's why it's always good to actually test so I'm going to activate this it's still active I'll just activate the wc payment to allow that to go through so that we have our function so after extending that class the next thing we're going to do is we're going to start working on our gateway and we need to just instruct for it to have our construct method now while we're extending the classes you're going to realize we're now working in what we call object-oriented programming so the first thing we're going to do is add a public function that is accessible by all and just say we're going to construct so we add our construct method and in that method in this method we just require to say since we're extending the payment gateway some of these are variables that are going to be accessing already there so we're going to get the id simply which is the id of our the gateway and the id is going to be no payment for now that will be good enough semicolon to terminate it we're going to get the next thing that we need to do is we can add an icon that is the small logo that usually see when you're out on the checkout and you're paying and you see a small icon right at the bottom that says use this payment to pay so so what we'll do is I will apply filters to it so apply filters we'll use the woocamas underscore and nob icon so the next thing that we need to do is passing the value so it will be stored in a plugin in the plugin somewhere like alldo plugins underscore url put a comma there then append maybe assets and then call it icon dot png so we'll have to add that in our assets otherwise that will end up failing and then we need to do it this we're going to append has fields now basically this is going to allow us to add fields inside our woocamas for example when we come here we'll be able to when we click set up we shall be able to see those fields and that we're feeling for the details maybe it's an api key to import and so on so this method is about that so for now we'll say it's false and then we tap another method which is this then we're going to append the method title so the method title for now we shall allow it to be translatable and the method title we're going to call it a nob payment and we shall see where that applies physically and for now we shall just give it a wc nob oh our text domain is actually here so we don't need to create another one the other thing that we need to do is we actually need to tap this and then we shall say method description and the description we're going to have we're also going to have it translatable and then what i'll do is just copy what we have here and just say no nob local payment system that's a that's a method description so we're going to tap the other method that's open for us to in the extension is a meet form fields and that's that we're going to add that inside our own class so if i copy this i'm going to add that field so copy this and then give it this is the new name for this i'll leave this empty so that it doesn't fail we need that method that's what we're going to describe our fields and then i'll duplicate this and add another field that says that in it the settings and that's going to allow us to actually initialize our settings so we need to save this give this a refresh nothing is broken so we are still good after adding that now we are going to stand adding some contents to our in it from fields and those are the fields that we're going to see when we come to add content to our our field so what we're going to do here first of all is we want to enable it so we're going to say this this class form fields that's what we need to tap the function we're going to apply filters that means we're going to basically just edit so we'll add in a filter and we'll say this is going to be the woob nob pair fields it can be anything because at one point we might want to tap into that and edit it and we're going to have an array of actually fields that we're going to add i'll terminate this with a semicolon so that i don't forget and then the first thing we're going to do is we're going to say is it enabled and then we'll add another array to this so we're basically working with a multi-dimensional array and then of course a comma at the end since we're going to add more fields and then we'll add a title to this and say is it enabled enabled or disabled so we'll allow this to also be translatable so let me just copy this and paste it here and then we shall say is it enabled enabled or disabled so if i save this this is an error so if i save this and come back and reload here we don't see anything happen because we haven't yet announced to the commas that we actually have a new payment gateway that's coming so i'm actually going to do that right now so that we can start seeing some of the changes so we're going to create a little function here and it's going to be a filter hook and the hook we are jumping into is actually called a wukamas payment gateways we are actually going to get we're going to add a function now and we're going to call it a noob payment gateway which is a different function so we'll delete delete but let's make this a little more descriptive so we'll add to the noob payment gateway so this is a little more descriptive so let's jump right down to give it a function add this and then go for that function i'm not going to do this particular filter remember filters just edit they don't write any new content so it comes with a it comes with a gateway's argument so this argument needs to be picked and we need to also return it at the end of the day so we pick it and then we return it so gateways if we don't do anything to wait our program will still run but we can also edit it and say we're getting the gateways array we are going to append a new item to week so terminate this we're going to get our class that we've extended which is nablicy load pay and we're going to throw it in here that's the name of the class and let's see what happens when we reload here you'll actually see that now we have our noob payment coming into our commerce which is our new field we have a little bit of error here and it's an error coming from when we are describing our plugin so this is a function sorry and then we're also going to make some corrections to our noob payment which is this here we can see we can see that we can actually enable it or disable it because of this field that we've added that is enabled and then we can save changes have it available we see the description we added new payment local content payment systems right here and then we can actually when we go to manage we see that it is saying yes because we checked it before and that's what this is doing if we go back and we uncheck it and save and we actually go to the setup you'll see that it says no and that means we already a step ahead so the next thing we're going to do is add a proper title add a proper description and instructions to our content area here so we have a title we have enabled this and what we're going to do is we're going to make it better so we're going to change this type instead of having it as a normal field we're actually going to have it as a checkbox so I'll save this reload and we actually have it as a checkbox that is much better you never know how people spell but they never get this wrong so wukamas has made a very good way of applying forms in the plugin so we'll just stop into that and we use it we'll add a label and the label we are going to also have it a translatable for now since we plan to use this plugin in different places so the label will be quite simple we can just say enable or disable no payment and we are seeing that we're actually just working on one field so next time you'll see this makes it easy as we apply the other fields we'll see that working out well and we'll default it to let the shop owner activate this field so we'll default it to no set this so everything works out well you see this no block for payments which is here wukano is adjusted to make it a little more descriptive and better the next thing that we need to do is I'll just duplicate this and then go into the title in the title field we want to have a proper title so give it a no payment gateway and then the title the type is a text we want to add and say add a new title so we we have a title here in this place we would have a label we'll have a description and the description say add a new title for the no payments gateway so we have the description ready default we shall just add it as a something that's translated already you just copy this and as the default and then we'll add a description tool tip and say this tip and then we'll just add that as true and save so let's see what that means when we come here we have the new payments gateway which is our title and it's having that as default this is the default text in there and then we can add a title now this is what add a title for the new payments gateway that customers will see when they are in the checkout page so essentially that's what this title is for it's actually going to go to the end we can see this description but when we come here to this tool tip we're able to see what we are talking about so that's the beauty of adding this description tool tip as true and then adding this description so I can actually move this down so that they match together and they make better sense that way in my code so we have a title we can have also the description so we'll go for the description we already saw a default description when we were the payments place which is this but as we set up we can just change all that away so what we'll do is we have the title and we'll just say this in the description leave it translatable the next thing that we need to do is add a type the type is a text area because it might be really long description is given here we'll leave the tool tip active and then we shall just add a default please remit your payment to the shop to allow for the delivery to be made we can change this word the shop to whatever shop name will be so when we reload this here we actually see that it is available the tool tip is given and the users are plugging can always come and actually make changes to this and well actually so so we've done that a description if we need to have any special instructions can also add this so instructions is the last argument that's available and we can just say so we have the instructions we have a title the title will be instructions make it short instructions it will be a text area will allow a description tool tip to be true I will have a description and then in the description we'll leave this empty the description here instructions that will be added to the thank you page and order email so you might want your clients to add a special message to the order email or to the thank you page and you make provision for this right here so yeah or good to go you can add a default you can say default instructions and if we come back here and reload they are that nothing more so this is one of the first steps we take into making our payment gateway now after doing this we've added our in our form fields here we also need to initialize our settings and after doing that we're going to go for the next a function that is very important in the WooCommerce payment gateway and that is the process payment function it's expected and we shall be adding that to our WooCommerce so now let's actually work on our process payment function so again have a public function and we're going to get the process underscore payments method and this is the one that actually deals with everything about the payments in terms of the open end so we're going to get the order id which is passed as an argument and then when we get that order id in this case is going to be what we use so we use wc get order and then we pass in the order id so our process payments method actually offers us the order id we need this order id to actually get which order we are talking about this function wc get order requires to have the order id it will give us the kind of order that we are actually going to work with which order we're going to pay for at the end of the day so we get the order id the next thing we need to do is now get the order status since we have the order now we just need to get its status so we can actually append a method to that which is a update status so we can immediately update the status of that order by saying uh want it got on hold and what we're going to do is uh i'll copy this translated string here now when it's on hold we're going to throw and say awaiting no payment so immediately a customer orders that order is actually sent to the on hold status inside you've seen that when you go to your orders for example i have some orders here and they're saying processing so we have a number of statuses in our orders we have processing we have on hold uh we have completed we have counsel we've funded you can also create your own method available so we can even just change this to pending payment so that it has the right status all together let's go back to our payment gateway then we need to do this we need to also we need to check on the stock and say we need to reduce the stock levels the order stock depending on how much you bought with that order we need to change the order stock meaning reduce it for the next customer so that we don't have a conflict with someone saying booked my order and they paid for it and yet i don't have it and someone will say ah sorry we ran out of stock because another client paid before you did um so we want to manage those issues with orders and the stock now we're going to go to the wukamas get the general wukamas first and say we're going to get go to the cart and we're going to empty the cart so we empty the cart after that payment and then at the end of the day we are going to return a thank you so return an array and say thank you for purchasing blah blah blah but of course in this array we have the results which is going to be the success so when it is a success then we have a message that we bring and then we we direct so we redirect the person to this class and we get the get return url and that's a function we have and then we'll pass in the order the reason i leave this open is because when we come to our plugins in wukamas and go to settings we actually have a couple of things here that are advanced so we have the different pages that are set as check out and we don't want to conflict with how this actually works if the customer sets it up or if they use the filter of changing the return url we want to leave that as often as possible inside our payment gateway now you realize there's so many steps that are missing here but we know that after we get our order we change the update status we reorder the stock then we empty the cart and then return a success but in between here we are able to add our own payment gateway by tapping into maybe an api and adding a different function so for example before we reduce the order can we maybe tap into a function that uh clear payment with api and then do what is necessary to clear and if this comes out right if this passes in this particular function so we'll have a public function it could even be private if we need to or but we leave it as public so clear good payment api and then when that passes then we can reduce the order then clear the cart and then do what is needed so we can add our own methods in here and that will allow us to to do whatever we need to do that's how we create our our payment gateway so what i'm going to do now is actually go back and activate our our payment we're going to go back to our settings and then i'm going to activate uh the new payments gateway let's try to go and make some orders so cut view cut run to the checkout fill in our pieces we don't have our payment gateway okay so let's set that as on as managed that is saved and let's come back to our checkout reload this and we'll now see that we have our payment which is no it has a broken has a broken image here which we set to go into the plugins folder and it's expected to be in there so we need to correct this very well of course this expects an argument that i didn't put so let's correct this and then cross file and then take away this concatenation mark let's reload this here it is still broken that is fine but at least we know that it's going in the right plugin and the assets folder and the icon so we can always have that here so open an assets folder and then then i'll jump into this and then i'll just drop this and then call it rename this and call it icon the png let's see yep so let's see how this works out i reload you can see we have a small icon there it's working out well the only thing that we don't have here is the name of our plugin which is the payment so wondering what's happening so let's set this reload it's not working because we have not set the user variables so what we're going to do is just come back here and what we're going to do is we're going to get this class then get the title we're going to save it in our database and say this dash get options get option so we're getting this from the database in our WooCommerce and we're going to get the title and pass it in title i'll duplicate this for the different pieces so we have a title we have a description in order to clear up our issue that we are having we need to add an action which is going to update our payment getaways so we come back to our code struct function here which which automatically runs when the class is executed so we're going to add an action that's going to take the hook that we're tapping into and that is a the hook is a WooCommerce updates options getaway so the action is our WooCommerce updates options payment getaways and then the next thing we do is actually we append to with the id so we look for that this and then we get the id this id which we saved here has no payment and then the next thing that we do is we add the array which is the last we're basically saying this is the fear this is the method we want you to run and we say this it is a method inside this class and we shall say process admin options let's save that so we're going to come back here and try to make some changes to no payment delivery to be made default instructions here let's save this and we actually see that all our settings are actually saved and if we come back to our checkout and reload we'll see that we now have our title showing up no payments we have our instructions showing up and when we click order it says error checking processing out after adding that action to allow us save our settings here we need to add one more action that will allow us to say thank you in a proper way so we'll add an action and that hook is the WooCommerce thank you and it also takes the same id that we have here so we're going to basically just append this to our thank you make that so it takes in the WooCommerce thank you then append the id which is our new payment and then after the comma we're going to add the array to signify that we want to tap into a method here and say this and then we shall add a method which is thank you page thank you underscore page so this is a method that now we have to create so let's go down we'll create our functions and we'll just say public function test it here and then do some magic with it so what we're going to do with our thank you function is we're going to say if which is a conditioner then we'll do something so if these instructions if it is true if the instructions are true then what we're going to do is we're going to echo with a wp auto paragraphs and then we shall just say these instructions we'll pass them in take this away save let's try to serve these changes and then go to try our checkout then press the order think there's something wrong with our status so I'll just do this on hold and save this let's try to place our order again after refreshing you can now use this to pay for our goods and allow our customers to do so much more so thank you for watching I hope you enjoyed this video if you have any questions please leave them in the comments I'll just throw this code up on our github and then you can follow along and don't forget to like the video if you liked it and then subscribe if you haven't considered yet cheers