How to add new online payment method


You are here:

You can create your own online payment method quite easily by following the steps below:

  1. Create configuration page (HTML form) to display in Settings > Finance > Online Payment Modes
  2. Save user’s configuration information in database
  3. Modify customer_view.blade.php file to display the payment method’s button for customer
  4. Process the payment on button click/form submission,  insert and update payment information in database

Let’s assume you want to add PayPal as your new online payment method. Here’s how you can do it:

Step 1 : Create configuration page for the payment method

First of all you need to add a new tab for your payment method in Settings > Finance > Online Payment Modes , beside “Stripe Checkout”, so that users can click on it and visit the configuration page.

Online Payment Tab

Here’s how the folder structure for payment methods looks like

Open the main.blade.php file and search for  <ul class="nav project-navigation"> and under it paste the following code

<li class="nav-item">
  <a class="nav-link {{ is_active_nav('paypal', $group_name) }}" 
     href="{{ $main_url }}?group=paypal">@lang('form.paypal')
  </a>
</li>

Now look for the following code in the same file

@if($group_name == '')
  @include('payment.modes.online.stripe')
@endif

Modify the above code so that it looks like following

@if($group_name == '')
   @include('payment.modes.online.stripe')
@elseif($group_name == 'paypal')
   @include('payment.modes.online.paypal')          
@endif

Now create a file called paypal.blade.php inside resources/views/payment/modes/online folder and create your html form for configuration settings. Please note that this same file will be used for both adding and updating the data, meaning there is only one view file for each payment method.  You can get the existing values of a payment method from database using the following code, in the view file

$gateway  = (isset($rec->paypal)) ? $rec->paypal : [] ;

Here paypal is the name you used as gateway name when inserting in database (we will discuss this in later section) and $rec is automatically injected from PaymentModeController.php (through which the view file is being loaded). To add a submit button in your form you  can use <?php echo bottom_toolbar(); ?> to automatically add a button which resides inside a sticky bottom toolbar or you can add your own html.

To handle your form submission you need to define a route first (Laravel’s convention). So open `routes/web.php` and look for  ('payment/modes')->group(function (){

Every route for payment modes are grouped together inside ('payment/modes')->group(function (){} Now define your route pointing to your method/function in `PaymentModeController.php` with a route name and use the name for your form’s action value like following:

<form method="POST" action="{{ route('post_payment_modes_online_paypal') }}">

Include the following code just as is (don’t make any changes) inside your form tag.

<input type="hidden" name="payment_mode_id" value="{{ old_set('payment_mode_id', NULL,$gateway) }}">

 

Step 2 : Save configuration information in database on submit

Your payment method information are stored in two tables.

  1. payment_methods
  2. settings (as json_encoded)

But don’t worry there is a function to handle the task. Inside your method in PaymentModeController.php handle the input validation and then use the following code:

// form input values as an array
$posted_data_settings = Input::get('settings'); 
// Save it in database
$response = $this->save_payment_gateway_settings(
    'paypal', 
     $posted_data_settings['paypal_label'], 
    'Description of the method', 
    ($posted_data_settings['paypal_active'] == 0) ? TRUE : NULL , 
    Input::get('payment_mode_id') , 
    $posted_data_settings
);

if($response)
{
   session()->flash('message', __('form.success_update'));
}
// Redirect back to the previous page
return  redirect()->back();      

Let’s look at the parameters of save_payment_gateway_settings()

function save_payment_gateway_settings(
  $gateway,              
  $label,                
  $description,         
  $inactive,             
  $payment_mode_id,      
  $posted_data_settings 

){}

$gateway: name of the payment method by which you want to refer it within your code
$label : name of the payment method that your users will see
$description : description of the method
$inactive : for flagging. if the method is active or inactive. Accepts true/false
$payment_mode_id:  value from the hidden input that you were asked to added above
$posted_data_settings: all the inputs values of the form

Now at this stage you will should be able to see the payment method in Invoices and Expenses options in your system. However you need to set it up in the page where the customers sees the invoice and pay for it so that they can use your new online payment method. In order to do that let’s move to step 3.

Step 3 : Modify customer_view.blade.php file to display the payment method’s button

The invoice folder in resources/view looks like following. Open the customer_view.php file and add your button where you think is suitable.

Invoice Folder

You can get the payment method’s configuration information from database using the following code, in the view file

<?php $paypal = get_payment_gateway_info('paypal') ; ?>

After you have added the button in the page let’s move to the final step.

Step 4 : Process the payment on button click/form submission,  insert and update information in database

At this stage you have to consider the following actions after payment processing is requested:

  1. Process the payment and create a log of API response in `payment_api_responses` table
  2. Handle if processing fails, otherwise Update Invoice and Insert in Payments Table and
  3. Create activity log and send a notification to the customer and to the person who created the invoice

You can perform the above operations easily if you check process_stripe_payment(Request $request) method in InvoiceController.php file as an example. The codes are commented enough and you can easily pick up what is happening.

 

One final word. The purpose of this tutorial is to give you a general idea about how the payment method works and how you can add a new payment method in the system. If you are a developer and you know what you are doing then feel free to implement it in your own way. However, before jumping into writing code go through the code of existing payment methods to get some more idea. Currently Stripe is the only online payment method showing up in the system. However, some code for PayPal is already there. Feel free to play with it.

Thank you very much!