# Base Processor

PAYMENT API EXTEND PAYMENT PROCESSOR

The Fluent Forms BaseProcessor class offers developers to process payment methods created from the BasePaymentMethod class.

The API Functions is automatically called when the form has payment the selected payment methods. The BaseFieldManager class is located at the pro version of fluetnforms plugin in src/Payments/PaymentMethods/BaseProcessor.php.

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

# Example Implementation

# Methods

# init()

You need to override this method using the following hooks. You can call it with your custom payment method class extended from the BasePaymentMethod class. At the bottom of this page, you will see an example implementation of this method with all available parameters of these hooks.

/*
* $this->method Should match your payment method key
*/
public function init()
{
    add_action('fluentform/process_payment_' . $this->method, array($this, 'handlePaymentAction'), 10, 6);
    add_action('fluentform/payment_frameless_' . $this->method, array($this, 'handleSessionRedirectBack'));

    add_action('fluentform/ipn_endpoint_' . $this->method, function () {
            //if IPN verification is needed
    });
}
1
2
3
4
5
6
7
8
9
10
11
12

# handlePaymentAction()

This is an abstract class that you have to implement in your own class. This is the most crucial part where the payment should be processed and then the transaction data is inserted into the database.

/* 
* Process and finalize the payment transaction
* 
* 
* @param  int $submissionId  - Form Submission ID
* @param array $submissionData - Form Submission Data Array
* @param object $form - Form Object
* @param array $methodSettings - Payment Method Settings Data Array
* @param boolean $subscriptionItems - Payment has subscription item
* @param int $totalPayable - Total payable amount
*/

public abstract function handlePaymentAction($submissionId, $submissionData, $form, $methodSettings)
1
2
3
4
5
6
7
8
9
10
11
12
13

# setSubmissionId($submissionId)

This method will set the form submission ID property. It should be called from handlePaymentAction where the $submissionId parameter is available. The following methods does not need to be created in your class you can call these methods from the base class.

/*
* Set the currrent submission ID
* @param $submissionId  - Form Submission ID
*/
public function setSubmissionId($submissionId)
1
2
3
4
5

# insertTransaction($data)

This method will insert the transaction data into the database. It should be called from the handlePaymentAction() method or when you need to insert a transaction record after verifying data.

/*
* @param data - Transaction Data array
*/
public function insertTransaction($data)
1
2
3
4

# insertRefund($data)

This method is used to insert refund data.

/*
* @param data - Refund Data array
*/
public function insertRefund($data)
1
2
3
4

# getTransaction($transactionId, $column = ‘id’)

This method is called to get transaction data. You can use this inside handlePaymentAction() method to process the transaction.

/*
* @param transactionId - Transaction ID
* $column - Column Name to match with 
*/
public function getTransaction($transactionId, $column = 'id')
1
2
3
4
5

# getRefund($refundId, $column = ‘id’)

Use this method to get refund data.

/*
* @param @refundId - RefundId ID
* $column - Column Name to match with 
*/
public function getRefund($refundId, $column = 'id')
1
2
3
4
5

# changeSubmissionPaymentStatus($newStatus)

Update the current submission status for example paid or pending. After processing a transaction update the payment status using this method.

/*
*@param $newStatus - New submission status (string)
*/
public function changeSubmissionPaymentStatus($newStatus)
1
2
3
4

# changeTransactionStatus($transactionId, $newStatus)

Use this method to update the status of the transaction.

*@param $transactionId - Transaction ID 
*@param $newStatus - New Status 
*/
public function changeTransactionStatus($transactionId, $newStatus)
1
2
3
4

# recalculatePaidTotal()

Use this method to recalculate the current submission total amount. This method does not need to be created in your class you can call this method from the base class.

public function recalculatePaidTotal()
1

# updateTransaction($transactionId, $data)

This method updates transaction data.

/*
*@param $transactionId - Transaction ID 
*@param $data - Transaction data array 
*/

public function updateTransaction($transactionId, $data)
1
2
3
4
5
6

# handleSessionRedirectBack($data)

This method handles the payment session redirect back.

public function handleSessionRedirectBack($data)
1

# setMetaData()

Submission metadata is saved using this method.

/*
* @param $name - Meta Name
* @param @value - Meta Value
*/
public function setMetaData($name, $value)
1
2
3
4
5

# getReturnData()

This method will return submission data which will be used during the final payment processing.

public function getReturnData()
1

# All Together

Here is an example implementation of the BaseProcessor class.

<?php
 if (!defined('ABSPATH')) {
     exit;
 }

use \FluentFormPro\Payments\PaymentMethods\BaseProcessor;

class MyPaymentProcessor extends BaseProcessor
{
    public $method = 'methodKey';

    protected $form;  //Form Object

    public function init()
    {
        add_action('fluentform/process_payment_' . $this->method, array($this, 'handlePaymentAction'), 10, 6);

        add_action('fluentform/payment_frameless_' . $this->method, array($this, 'handleSessionRedirectBack'));
    }

    /*
    * Process and finalize the payment transaction
    * 
    * @param  int $submissionId  - Form Submission ID
    * @param array $submissionData - Form Submission Data Array
    * @param object $form - Form Object
    * @param array $methodSettings - Payment Method Settings Data Array
    * @param boolean $hasSubscription - Payment has subscription item
    * @param int $totalPayable - Total payable amount
    */

    public function handlePaymentAction($submissionId, $submissionData, $form, $methodSettings, $hasSubscription, $totalPayable)
    {
        $this->form = $form;
        $this->setSubmissionId($submissionId);

        /* Your code here : Redirect        
        *  payment method API ,then insert the transaction
        */ Transaction data format

        $this->insertTransaction([
            'transaction_type' => 'onetime',
            'payment_total' => $this->getAmountTotal(),
            'status' => 'pending', //paid or pending
            'currency' => $currency ,
            'payment_mode' => $this->getPaymentMode()
        ]);
        $this->handleRedirect($transaction, $submission, $form, $methodSettings, $hasSubscription);
    }
   
   public function handleRedirect($transaction, $submission, $form, $methodSettings, $hasSubscription)
    {
        $successUrl = add_query_arg(array(
            'fluentform_payment' => $submission->id,
            'payment_method'     => $this->method,
            'transaction_hash'   => $transaction->transaction_hash,
            'type'               => 'success'
        ), site_url('/'));

        $listener_url = add_query_arg(array(
            'fluentform_payment_api_notify' => 1,
            'payment_method'                => $this->method,
            'submission_id'                 => $submission->id,
            'transaction_hash'              => $transaction->transaction_hash,
        ), home_url('index.php'));

        $paymentArgs = array(
            'amount' => [
                'currency' => $transaction->currency,
                'value' => number_format((float) $transaction->payment_total / 100, 2, '.', '')
            ],
            'redirectUrl' => $successUrl,
            'webhookUrl' => $listener_url,
            'metadata' => json_encode([
                'form_id' => $form->id,
            ]),
        );
        $paymentIntent = (new IPN())->makeApiCall('payments', $paymentArgs, $form->id, 'POST');

        if(is_wp_error($paymentIntent)) {
  
            wp_send_json_success([
                'message' => $paymentIntent->get_error_message()
            ], 423);
        }

        wp_send_json_success([
            'nextAction'   => 'payment',
            'actionName'   => 'normalRedirect',
            'redirect_url' => $paymentIntent['redirectURL'],
            'message'      => 'Redirect Message',
            'result'       => [
                'insert_id' => $submission->id
            ]
        ], 200);
    }


    /*
    * This method is called when you are redirected back to your site.
    *
    * @param $data  - $_GET data of redirect back page
    */

    public function handleSessionRedirectBack($data)
    {
        // Here Verify and process your payment redirect back using the payment method API 
        
        $payment = (new MyPaymentAPIDemo())->makeApiCall($data); // Make API call from your payment method 
        $isSuccess = $payment['status'] == 'success';
        $submission = $this->getSubmission();
        $transactionHash = sanitize_text_field($data['transaction_hash']);
        $transaction = $this->getTransaction($transactionHash, 'transaction_hash');
        if($isSuccess) {
             // Handle payment success
             $this->handlePaid($submission, $transaction, $payment)
        } else {
             // Handle payment fail
        }
    }

    public function getPaymentMode()
    {
        return $this->method;
    }
    public function handlePaid($submission, $transaction, $vendorTransaction)
    {
        // Check if actions are fired
        if ($this->getMetaData('is_form_action_fired') == 'yes') {
            return $this->completePaymentSubmission(false);
        }
        $status = 'paid';
        // Let's make the payment as paid
        $updateData = [
            'payment_note' => maybe_serialize($vendorTransaction),
            'charge_id'    => sanitize_text_field($vendorTransaction['id']),
            'payer_email' => $vendorTransaction['email']
        ];

        $this->updateTransaction($transaction->id, $updateData);
        $this->changeSubmissionPaymentStatus($status);
        $this->changeTransactionStatus($transaction->id, $status);
        $this->recalculatePaidTotal();

        $returnData = $this->getReturnData();
        $this->setMetaData('is_form_action_fired', 'yes');

        return $returnData;
    }
}
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

# Final Note

Please check the existing payment processor files to get a more clear concept of this class and implement your custom payment system. There are other methods also to help with possessing a payment. 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)