Create a payment form using the Form Calculations feature
RSForm!Pro provides an easy to use built-in calculator so that you can perform operations easily between your form fields. In this article we will provide an example on how to control the total amount sent to the payment gateway using a simple code snippet. We will simulate a hotel room booking form that will allow you to select the room type, duration of stay and extra services for your booking.
You can download this sample form from the link below.
Booking sample form
Step 1: Adding and configuring the form fields
First we will need to create the fields for our form, in this example we will be using:
- one Freetext type field for displaying a list of costs to the user: cost
- one Textbox type field for collecting the user email: email
- one Dropdown type field for selecting the room type: room
- two Date and Time Picker type fields for the check-in and check-out dates: checkin, checkout
- one Checkbox type field for the extra services that will provided during your stay: extras
- one Textbox type field for displaying the total amount to pay: total
- a Submit type button
- the payment specific fields that will redirect us to the payment gateway: Choose Payment,(Payment)PayPal, (Payment)Stripe and Single Product
For the two Date and Time Picker type fields we will need to use the "Date Modifier" attribute so that the check-in date becomes the minimum date selectable in the check-out field with an offset of 1 day. We will also make sure that the current date is the minimum selection available for the check-in field. The Single Product type field should have a value/price of 0, this is only used to send a product information to the payment gateway, we will calculate the total amoun through the form calculator. Lastly we will need to setup the values of the Dropdown and Checkbox fields as per the instructions of the Form calculations article.
Step 2: Creating the calculations using the built-in calculator
After setting up of fields, we will navigate to `Form Properties` > `Calculations` and setup our price equation. We will be using the built-in days difference calculator as well, the equation will be:
total = (RSFormPro.Calculations.toDays({checkout:value}-{checkin:value}*{room:value})+{extras:value}
This will calculate the number of days between the two selected calendar dates, multiply this with the cost of the selected room, then add the selected extras cost.
Step 3: Sending the calculated total to the payment gateway
For this we will need to use the PHP Scripts section of our form, to overwrite the total amount that needs to be paid for the booking.
The total sent to the payment gateway is stored in a hidden field named rsfp_Total, we will need to overwrite this with the total we calculated using the following code snipet into `PHP Scripts` > `Scripts called on form process`:
$_POST['form']['rsfp_Total'] = number_format($_POST['form']['total'], 2, '.', '');