# Base Payment Method

PAYMENT API EXTEND PAYMENT METHOD

The Fluent Forms BasePaymentMethod Class provides developers with a simple way to add new Payment methods easily. The BaseProcessor Class is also required for the payment method background processing. The BasePaymentMethod class mainly works for displaying and storing the payment method settings, while the BaseProcessor class process and finalize the payments.

The API Functions are automatically included when Fluent Forms Pro Booted. The BaseFieldManager class is located in src/Payments/PaymentMethods/BasePaymentMethod.php.

Notice: Please do not initiate this class directly. You should extend this class to add start adding a new payment method

# Example Implementation

# Methods

# __construct()

This is an example of how you can extend the BasePaymentMethod. This key will be used to identify the payment later on for the payment actions.

<?php

use FluentFormPro\Payments\PaymentMethods\BasePaymentMethod;

class MyCustomPaymentMethod extends BasePaymentMethod
{    
     /*
     * Extend your payment method with a payment key identifier
     */
     public function __construct()
     {
         parent::__construct('myCustomPaymentKey');
     }
1
2
3
4
5
6
7
8
9
10
11
12
13

# init()

This is the main function that will run all the required actions for this payment settings and processing. These hooks will be called dynamically during the payment processing & settings after the payment method setup is completed.

 public function init()
    {
        add_filter('fluentform/payment_method_settings_validation_'.$this->key, array($this, 'validateSettings'), 10, 2);

        if(!$this->isEnabled()) {
            return;
        }

        add_filter('fluentform/transaction_data_' . $this->key, array($this, 'modifyTransaction'), 10, 1);

        add_filter(
            'fluentform/available_payment_methods',
            [$this, 'pushPaymentMethodToForm']
        );
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# pushPaymentMethodToForm($methods)

This method will push the payment method with existing $methods to form editor. So it can be added to your fluentforms. Here is the format you need to follow to add the method. The $methods parameter is available with the fluentform/available_payment_methods hook.

/*
* @param $methods - Payment Methods Array data
*/    
public function pushPaymentMethodToForm($methods)
{
    $methods[$this->key] = [
        'title' => __('CustomPayment Method', 'fluentformpro'),
        'enabled' => 'yes',
        'method_value' => $this->key,
        'settings' => [
            'option_label' => [
                 'type' => 'text',
                 'template' => 'inputText',
                 'value' => 'Pay with MyCustomPaymentMethod ',
                 'label' => 'Method Label'
             ]
         ]
    ];
    return $methods;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# modifyTransaction($transaction)

This method modifies a transaction record with the payment method dashboard entry url. Use this to link the transaction with your payment method’s dashboard transaction page using the $transaction->charge_id charge id. The $transaction parameter is available with the fluentform/transaction_data_{$key} hook.

/*
* @param $transaction - Transaction Data array
*/
public function modifyTransaction($transaction)
{
   if ($transaction->charge_id) {
        $transaction->action_url =  'https://dashboard.mypaymentsitedemo.com/app/payments/'.$transaction->charge_id;
    }
    return $transaction;
}
1
2
3
4
5
6
7
8
9
10

# getGlobalFields()

This method will return all the admin settings for the current payment method. You can add your own settings also here. In the end of this page you will see an example with the settings format.

abstract public function getGlobalFields();
1

# getGlobalSettings()

This method should return the saved data from the database. It will be based on the settings that were provided by getGlobalFields() method. You can get your settings like this get_option(‘fluentform_payment_settings_{method_key}’, []);

abstract public function getGlobalSettings();
1

# Further read

# Validate settings of payment method

If you want to validate the payment admin settings use this hook

add_filter('fluentform/payment_method_settings_validation_{method_key}', function ($errors, $settings) {
        if(!($settings['test_api_key']) && !($settings['live_api_key'])) {
             $errors['test_api_key'] = __('API Key is required', 'fluentformpro');
        }
        return $errors;
}, 10, 2);
1
2
3
4
5
6

# All Together

Your ideal implementation should look like this with validation.

class MyCustomPaymentMethod extends BasePaymentMethod
{
    public function __construct()
    {
        parent::__construct('myCustomPaymentKey');
    }

    public function init()
    {  
        add_filter('fluentform/payment_method_settings_validation_' . $this->key, array($this, 'validateSettings'), 10, 2);

        add_filter('fluentform/transaction_data_' . $this->key, array($this, 'modifyTransaction'), 10, 1);

        add_filter(
            'fluentform/available_payment_methods',
            [$this, 'pushPaymentMethodToForm']
        );
    }

    public function pushPaymentMethodToForm($methods)
    {
        $methods[$this->key] = [
            'title' => __('CustomPayment Method', 'fluentformpro'),
            'enabled' => 'yes',
            'method_value' => $this->key,
            'settings' => [
                'option_label' => [
                    'type' => 'text',
                    'template' => 'inputText',
                    'value' => 'Pay with MyCustomPaymentMethod ',
                    'label' => 'Method Label'
                ]
            ]
        ];

        return $methods;
    }

    public function validateSettings($errors, $settings)
    {
        if(!($settings['test_api_key']) && !($settings['live_api_key'])) {
            $errors['test_api_key'] = __('API Key is required', 'fluentformpro');
        }
        return $errors;
    }

    public function modifyTransaction($transaction)
    {
        return $transaction;
    }

    public function getGlobalFields()
    {
        return [
            'label' => 'MyCustom Payment Settings',
            'fields' => [
                [
                    'settings_key' => 'is_active',
                    'type' => 'yes-no-checkbox',
                    'label' => 'Status',
                    'checkbox_label' => 'Enable MyCustom Payment Payment Method'
                ],
                [
                    'settings_key' => 'payment_mode',
                    'type' => 'input-radio',
                    'label' => 'Payment Mode',
                    'options' => [
                        'test' => 'Test Mode',
                        'live' => 'Live Mode'
                    ],
                    'info_help' => 'Select the payment mode. for testing purposes you should select Test Mode otherwise select Live mode.',
                    'check_status' => 'yes'
                ],
                [
                    'settings_key' => 'test_api_key',
                    'type' => 'input-text',
                    'data_type' => 'password',
                    'placeholder' => 'Test API Key',
                    'label' => 'Test API Key',
                    'inline_help' => 'Provide your test api key for your test payments',
                    'check_status' => 'yes'
                ],
                [
                    'settings_key' => 'live_api_key',
                    'type' => 'input-text',
                    'data_type' => 'password',
                    'label' => 'Live API Key',
                    'placeholder' => 'Live API Key',
                    'inline_help' => 'Provide your live api key for your live payments',
                    'check_status' => 'yes'
                ]
            ]
        ];
    }

    public function getGlobalSettings()
    {
        return get_option('fluentform_payment_settings_'.$this->key, []);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

# Final Note

It’s highly recommended to explore our source files and try to understand the procedure. Once you understand it’s very easy to implement your own custom payment method.

If you have any questions please feel free to reach to our support team (opens new window) or ask questions in our facebook community group (opens new window)