{"id":294,"date":"2023-01-21T20:42:44","date_gmt":"2023-01-21T17:42:44","guid":{"rendered":"https:\/\/akaunting.com\/hc\/?post_type=docs&#038;p=294"},"modified":"2023-01-21T20:56:20","modified_gmt":"2023-01-21T17:56:20","slug":"making-a-payment-method","status":"publish","type":"docs","link":"https:\/\/akaunting.com\/hc\/docs\/developers\/making-a-payment-method\/","title":{"rendered":"Making a payment method"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Akaunting is not used only for accounting management but also for getting invoices paid. That said, Akaunting is being used in 150+ countries that accept different payment methods. For example, while in Brazil PagSeguro is used to get paid, eWay is the leader of online payments in Australia. Therefore, there is a separate category for\u00a0<a href=\"https:\/\/akaunting.com\/apps\/categories\/payment-method?utm_source=help_center&amp;utm_medium=documentation&amp;utm_campaign=developers\" target=\"_blank\" rel=\"noreferrer noopener\">payment methods<\/a>\u00a0in Akaunting App Store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Akaunting ships with the famous&nbsp;Omnipay&nbsp;package, which eases the way to accept payments. Furthermore, Akaunting has a specific&nbsp;<a href=\"https:\/\/github.com\/akaunting\/akaunting\/blob\/master\/app\/Traits\/Omnipay.php\" target=\"_blank\" rel=\"noreferrer noopener\">trait<\/a>&nbsp;for Omnipay which sets the invoice details and makes the proper redirects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, create and install a&nbsp;<a href=\"https:\/\/akaunting.com\/hc\/docs\/developers\/modules\/\" target=\"_blank\" rel=\"noreferrer noopener\">module<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"74aaaba8f57c\">Composer<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, let\u2019s add the&nbsp;Omnipay v3&nbsp;gateway&nbsp;to the composer file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n    \"require\": {\n        \"omnipay\/paypal\": \"3.0.*\"\n    },\n    \"replace\": {\n        \"guzzlehttp\/guzzle\": \"*\",\n        \"guzzlehttp\/psr7\": \"*\",\n        \"laravel\/framework\": \"*\",\n        \"omnipay\/common\": \"*\",\n        \"symfony\/http-foundation\": \"*\"\n    },\n    \"scripts\": {\n        \"test\": [\n            \"composer install --prefer-dist --no-interaction --no-scripts --no-suggest --no-progress --no-ansi\"\n        ]\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"604326b2b942\">Settings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The module should offer some settings such as API Key, Secret, etc. to the user\/company that will get the payments. As described in the&nbsp;<a href=\"https:\/\/akaunting.com\/hc\/docs\/developers\/settings\/\" target=\"_blank\" rel=\"noreferrer noopener\">Settings<\/a>&nbsp;documentation, you can do it 2 ways but generally the&nbsp;<code>module.json<\/code>&nbsp;file is more than enough for payment methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n    \"alias\": \"paypal-express\",\n    \"icon\": \"simple-icons-paypal\",\n    \"version\": \"1.0.0\",\n    \"active\": 1,\n    \"providers\": [\n        \"Modules\\\\PaypalExpress\\\\Providers\\\\Event\",\n        \"Modules\\\\PaypalExpress\\\\Providers\\\\Main\"\n    ],\n    \"aliases\": {},\n    \"files\": [],\n    \"requires\": [],\n    \"settings\": [\n        {\n            \"type\": \"text\",\n            \"name\": \"name\",\n            \"title\": \"general.name\",\n            \"attributes\": {\n                \"required\": \"required\"\n            },\n            \"rules\": \"required|string\"\n        },\n        {\n            \"type\": \"text\",\n            \"name\": \"order\",\n            \"title\": \"paypal-express::general.order\",\n            \"attributes\": {},\n            \"rules\": \"nullable|integer\"\n        },\n        {\n            \"type\": \"text\",\n            \"name\": \"username\",\n            \"title\": \"paypal-express::general.username\",\n            \"attributes\": {\n                \"required\": \"required\"\n            },\n            \"rules\": \"required\"\n        },\n        {\n            \"type\": \"text\",\n            \"name\": \"password\",\n            \"title\": \"paypal-express::general.password\",\n            \"attributes\": {\n                \"required\": \"required\"\n            },\n            \"rules\": \"required\"\n        },\n        {\n            \"type\": \"text\",\n            \"name\": \"signature\",\n            \"title\": \"paypal-express::general.signature\",\n            \"attributes\": {\n                \"required\": \"required\"\n            },\n            \"rules\": \"required\"\n        },\n        {\n            \"type\": \"select\",\n            \"name\": \"mode\",\n            \"title\": \"paypal-express::general.mode\",\n            \"values\": {\n                \"live\": \"Live\",\n                \"test\": \"Test\"\n            },\n            \"selected\": \"live\",\n            \"attributes\": {},\n            \"rules\": \"required\"\n        },\n        {\n            \"type\": \"accountSelect\",\n            \"name\": \"account_id\",\n            \"attributes\": {\n                \"required\": \"required\"\n            },\n            \"rules\": \"required\"\n        },\n        {\n            \"type\": \"toggle\",\n            \"name\": \"customer\",\n            \"title\": \"paypal-express::general.customer\",\n            \"attributes\": {}\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aa250dd4c4fc\">Listener<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To get shown in the payment method list when the customer views the invoice in the client&nbsp;<code>portal<\/code>&nbsp;or via a&nbsp;<code>signed<\/code>&nbsp;link, you have to register your module as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"><strong>&lt;?php<\/strong>\n\nnamespace Modules\\PaypalExpress\\Listeners;\n\nuse App\\Events\\Module\\PaymentMethodShowing as Event;\n\nclass ShowAsPaymentMethod\n{\n    public function handle(Event $event): void\n    {\n        $method = setting('paypal-express');\n\n        $method['code'] = 'paypal-express';\n\n        $event-&gt;modules-&gt;payment_methods[] = $method;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3a12192928c7\">Routes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Getting shown in the payment methods list is the first step; the next one is to have the routes set up. The module should have at least 2 route files:&nbsp;<code>portal.php<\/code>&nbsp;for customers logged in and&nbsp;<code>signed.php<\/code>&nbsp;for guests with a shared link. Here you can see the examples:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"d99fc35da676\">portal.php<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"><strong>&lt;?php<\/strong>\n\nuse Illuminate\\Support\\Facades\\Route;\n\n\/**\n * 'portal' middleware and 'portal\/paypal-express' prefix applied to all routes (including names)\n *\n * @see \\App\\Providers\\Route::register\n *\/\n\nRoute::portal('paypal-express', function () {\n    Route::get('invoices\/{invoice}', 'Payment@show')-&gt;name('invoices.show');\n    Route::post('invoices\/{invoice}\/confirm', 'Payment@confirm')-&gt;name('invoices.confirm');\n    Route::get('invoices\/{invoice}\/return', 'Payment@return')-&gt;name('invoices.return');\n    Route::get('invoices\/{invoice}\/cancel', 'Payment@cancel')-&gt;name('invoices.cancel');\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"db096059d167\">signed.php<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"><strong>&lt;?php<\/strong>\n\nuse Illuminate\\Support\\Facades\\Route;\n\n\/**\n * 'signed' middleware and 'signed\/paypal-express' prefix applied to all routes (including names)\n *\n * @see \\App\\Providers\\Route::register\n *\/\n\nRoute::signed('paypal-express', function () {\n    Route::get('invoices\/{invoice}', 'Payment@show')-&gt;name('invoices.show');\n    Route::post('invoices\/{invoice}\/confirm', 'Payment@confirm')-&gt;name('invoices.confirm');\n    Route::get('invoices\/{invoice}\/return', 'Payment@return')-&gt;name('invoices.return');\n    Route::get('invoices\/{invoice}\/cancel', 'Payment@cancel')-&gt;name('invoices.cancel');\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bb62a92795d2\">Controller<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you\u2019ve set up everything, it\u2019s time to create the controller:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"><strong>&lt;?php<\/strong>\n\nnamespace Modules\\PaypalExpress\\Http\\Controllers;\n\nuse App\\Abstracts\\Http\\PaymentController;\nuse App\\Models\\Sale\\Invoice;\nuse App\\Traits\\Omnipay;\nuse Illuminate\\Http\\Request;\n\nclass Payment extends PaymentController\n{\n    use Omnipay;\n\n    public $alias = 'paypal-express';\n\n    public $type = 'redirect';\n\n    public function confirm(Invoice $invoice, Request $request)\n    {\n        $this-&gt;create('PayPal_Express');\n\n        return $this-&gt;purchase($invoice, $request, [\n            'username' =&gt; $this-&gt;setting['username'],\n            'password' =&gt; $this-&gt;setting['password'],\n            'signature' =&gt; $this-&gt;setting['signature'],\n            'testMode' =&gt; ($this-&gt;setting['mode'] == 'test'),\n        ]);\n    }\n\n    public function return(Invoice $invoice, Request $request)\n    {\n        $this-&gt;create('PayPal_Express');\n\n        return $this-&gt;completePurchase($invoice, $request, [\n            'username' =&gt; $this-&gt;setting['username'],\n            'password' =&gt; $this-&gt;setting['password'],\n            'signature' =&gt; $this-&gt;setting['signature'],\n            'testMode' =&gt; ($this-&gt;setting['mode'] == 'test'),\n        ]);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see from the example, it doesn\u2019t extend the general&nbsp;<a href=\"https:\/\/github.com\/akaunting\/akaunting\/blob\/master\/app\/Abstracts\/Http\/Controller.php\" target=\"_blank\" rel=\"noreferrer noopener\">Controller<\/a>&nbsp;but&nbsp;<a href=\"https:\/\/github.com\/akaunting\/akaunting\/blob\/master\/app\/Abstracts\/Http\/PaymentController.php\" target=\"_blank\" rel=\"noreferrer noopener\">PaymentController<\/a>&nbsp;abstract of Akaunting. The&nbsp;<code>confirm<\/code>&nbsp;and&nbsp;<code>return<\/code>&nbsp;functions will first create an instance of Ompnipay and then redirect the customer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As it\u2019s also mentioned in the documentation of Omnipay, there are 2 types of payment methods:&nbsp;<code>hosted<\/code>&nbsp;which embeds the credit card form and&nbsp;<code>redirect<\/code>&nbsp;which redirects the customer to the payment method site to make the payment and redirects them back to Akaunting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The view\/blade part of both credit card and redirect is&nbsp;<a href=\"https:\/\/github.com\/akaunting\/akaunting\/tree\/master\/resources\/views\/components\/payment_method\" target=\"_blank\" rel=\"noreferrer noopener\">built into<\/a>&nbsp;Akaunting, so you don\u2019t have to take any further action unless you don\u2019t want to override them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3b2b7057fa57\">Language<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, the language file in&nbsp;<code>Resources\/lang\/en-GB\/general.php<\/code>&nbsp;should be something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"><strong>&lt;?php<\/strong>\n\nreturn [\n\n    'name'              =&gt; 'PayPal Express',\n    'description'       =&gt; 'Express Checkout for faster PayPal transactions',\n\n    'customer'          =&gt; 'Show to Customer',\n    'username'          =&gt; 'Username',\n    'password'          =&gt; 'Password',\n    'signature'         =&gt; 'Signature',\n    'mode'              =&gt; 'Mode',\n    'order'             =&gt; 'Order',\n\n    'error' =&gt; [\n\n    ],\n];<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Akaunting is not used only for accounting management but also for getting invoices paid. That said, Akaunting is being used in 150+ countries that accept different payment methods. For example, while in Brazil PagSeguro is used to get paid, eWay is the leader of online payments in Australia. Therefore, there is a separate category for\u00a0payment [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":31,"menu_order":13,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-294","docs","type-docs","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":3,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/294\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/294\/revisions\/307"}],"up":[{"embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/31"}],"wp:attachment":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/doc_tag?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}