We’ll look how developer’s create a payment method in Cs-Cart store to provide a smooth checkout experience for customers.
To begin, you should choose a payment gateway that fits your business needs. Once you select a payment method, you must configure it by providing necessary details, such as the API key, secret key, and other relevant information. You may also need to enable certain settings, such as test mode, refunds, or recurring payments. After you configure the payment method, you should test it to ensure it is working correctly. In the following sections, we will provide more detail on each step.
File structure to create a payment method in CS-Cart
app |_Addons |_your_addon_name |_func.php |_addon.xml |_payments |_script.php Design |_backend |_templates |_addons |_your_addon_name |_views |_orders |_components |_payments |_processor.tpl
You must know how to create a New addon in CS-Cart.
Add a function that is called from your func.php file during the installation of addon, as seen below:
/** * Function runs on installation of add-on * * @return void */ function fn_Stripe_Refund_install() { $data = array( 'processor' => 'Stripe Payment Gateway', 'processor_script' => 'wk_stripe.php', 'processor_template' => 'addons/stripe_refund/views/orders/components/payments/cc_stripe.tpl', 'admin_template' => 'wk_stripe.tpl', 'callback' => 'N', 'type' => 'P', 'addon' => 'stripe_refund' ); db_query('INSERT INTO ?:payment_processors ?e', $data); }
A processor is included which is selected on the payments page. If you want to use the credit card fields on the checkout page, use “cc.tpl” in place of “cc_stripe.tpl”. Click on Install on the addons page to install your addon.
Let’s go on to incorporating the payment feature into addon when it has been installed.
Then, while running the function in func.php, create a new file with the name specified under processor script within the “payments” folder created in addons directory, for example, “app/addons/stripe refund/payments.”
This file will be used in creating your payment method.
Create a new file by going to “design/backend/templates/addons/{your_addon_name}/ views/payments/components/cc_processors/processor.tpl directory. When choosing a payment processor or creating a new payment method, such as API keys, tokens, payment modes, order status after payment, etc., and insert the HTML you wish to display there. For example
processor.tpl
<div class="control-group"> <label class="control-label">{__("mode")}:</label> <div class="controls"> <label class="radio inline"> <input type="radio" name="processor_params[test_mode]" value="Y" {if $processor_data.processor_params.test_mode == "Y"}checked{/if}> {__("test")} </label> <label class="radio inline"> <input type="radio" name="processor_params[test_mode]" value="N" {if $processor_data.processor_params.test_mode == "N" || empty($processor_data.processor_params.test_mode)}checked{/if}> {__("live")} </label> </div> </div> <div class="control-group"> <label class="control-label">{__("login_id")}:</label> <div class="controls"> <input type="text" name="processor_params[login_id]" class="input-large" value="{$processor_data.processor_params.login_id|escape}" /> </div> </div> <div class="control-group"> <label class="control-label">{__("transaction_key")}:</label> <div class="controls"> <input type="text" name="processor_params[transaction_key]" class="input-large" value="{$processor_data.processor_params.transaction_key|escape}" /> </div> </div> <div class="control-group"> <label class="control-label">{__("description")}:</label> <div class="controls"> <input type="text" name="processor_params[description]" class="input-large" value="{$processor_data.processor_params.description|escape}" /> </div> </div> <div class="control-group"> <label class="control-label">{__("sort_order")}:</label> <div class="controls"> <input type="text" name="processor_params[sort_order]" class="input-mini" value="{$processor_data.processor_params.sort_order|default:'0'}" /> </div> </div>
Now you are all set to create a new payment method, select the processor, and fill up all the required details under configure tab.
The main part left is to configure the payment file which we have created under the “payments” folder. That file will be called when a customer clicks on “Place Order”.
script.php
<?php use Tygh\Registry; if (!defined('BOOTSTRAP')) { die('Access denied'); } // Load the order details from the payment gateway callback $order_id = $_REQUEST['order_id']; $order_info = fn_get_order_info($order_id); // Verify the payment gateway callback $payment_method_id = $order_info['payment_method']['payment_id']; $processor_data = fn_get_payment_method_data($payment_method_id); if ($processor_data['processor_params']['merchant_id'] != $_REQUEST['merchant_id']) { fn_set_notification('E', __('error'), __('payment_verification_failed')); fn_order_placement_routines('route', $order_info['order_id'], false, false, true); exit; } // Update the order status based on the payment gateway callback if ($_REQUEST['status'] == 'success') { $result = fn_update_order_payment_info($order_id, $_REQUEST['transaction_id'], $_REQUEST['amount'], $_REQUEST['currency'], time()); if ($result === true) { $order_status = 'P'; // Payment successful } else { $order_status = 'F'; // Payment failed } } else { $order_status = 'O'; // Order open } // Update the order status in the database fn_update_order_status($order_id, $order_status, '', false); // Send a notification email to the customer fn_send_notification($order_info, 'C'); // Return a success response to the payment gateway echo 'OK';
If your API includes a return URL, it should be of the format like this – fn_url("payment_notification.return?payment={payment_file_name}&order_id=$order_id", AREA, 'current');
Now, basic payment method in CS-cart is created and is ready to be used by the customers.
So, that was much about the process to create a payment method in CS-cart. If you need custom CS-Cart Development services then feel free to reach us and also explore our exclusive range of CS-Cart Addons.
!!Have a Great Day Ahead!!
Be the first to comment.