There are several payment methods available itself and. Although sometimes you'll find yourself in the situation where you need something different, either there is no method available for your choice of payment gateway or you want some different logic. In either case, you're left with the only option: To create a new payment method module.
We'll assume that our custom payment method name is "custom". There are at least three or four files and one folder you can create in order to set up the things. Let's check the same in detail.
You need to create three file. each file are required. following are the folder structure.
You can create other one folder and one file. Folder and file are optional.following are the folder structure.
In this folder contain custom.php file. in this file contain all logic of your payment gateway. Our payment gateway send three types of request to your controller. Every request keep two parameter. They are $settingData and $gatewayData. You can get their sample data from Following Url.
Get sample data
Folder structure must be like this:
<?php
class custom {
public $title = 'Custom Payment Gateway';
public $icon = 'assets/payment_gateway/custom.png';
public $website = '';
function __construct($api){ $this->api = $api; }
public function getPaymentGatewayView($settingData,$gatewayData){
$view = APPPATH."payment_gateway/views/custom.php";
require $view;
}
public function setPaymentGatewayRequest($settingData,$gatewayData){}
public function customCallbackFunction($settingData,$gatewayData){}
}
<div class="form-group">
<label class="form-control-label">Some Setting</label>
<input class="form-control" name="name" value="<?= $setting_data["name"] ?>" >
</div>
<div class="form-group">
<label class="control-label" for="input-completed-status">Completed Status</label>
<select name="completed_status_id" id="input-completed-status" class="form-control">
<?php foreach ($order_status as $order_status_id => $name){
if(isset($setting_data["completed_status_id"]))
$selected = ($order_status_id == $setting_data["completed_status_id"]) ? "selected" : "";
else
$selected = ($order_status_id == 1) ? "selected" : ""; ?>
<option <?= $selected ?> value="<?= $order_status_id; ?>"><?= $name ?></option>
<?php } ?>
</select>
</div>
Setting file contain $setting_data variable and this variable contain all saved setting of your payment gateway. Some time you need to ask information from admin for example credential of payment gateway or something else. Also you can manage the status of transaction in project according to the result of the payment. $order_status lists are shown in "Status ID And Title" section. You just need to create a input out system will auto create a setting page and save setting data.
<div class="payment-button-group">
<button type="button" class="btn btn-default" onclick="backCheckout()">Back</button>
<button id="button-confirm" class="btn btn-primary">Confirm</button>
</div>
<script type="text/javascript">
$("#button-confirm").click(function(){
$this = $(this);
$this.prop("disabled",true);
$.ajax({
url:"<?= $gatewayData["payment_confirmation"] ?>",
type:"POST",
dataType:"json",
data:$("[name^="comment"]").serialize(),
beforeSend:function(){$("#button-confirm").btn("loading");},
complete:function(){$("#button-confirm").btn("reset");},
success:function(json){
$container = $("#checkout-confirm");
$container.find(".has-error").removeClass("has-error");
$container.find("span.text-danger").remove()
if(json["errors"]){
$.each(json["errors"]["comment"], function(ii,jj){
$ele = $container.find("#comment_textarea"+ ii);
if($ele){
$ele.parents(".form-group").addClass("has-error");
$ele.after("<span class="text-danger">"+ jj +"</span>");
}
});
}
if(json["success"]){
$.ajax({
url:"<?= $gatewayData["confirm_payment"] ?>",
type:"POST",
dataType:"json",
data:{
payment_gateway: $("input[name="payment_gateway"]:checked").val()
},
beforeSend:function(){$this.btn("loading");},
complete:function(){$this.btn("reset");},
success:function(json){
if(json["redirect"])
window.location = json["redirect"];
if(json["warning"])
alert(json["warning"])
},
});
}
},
});
})
</script>
View file contain getPaymentGatewayView function parameter $settingData and $gatewayData.You can use them in view file. In view file you must send request to $gatewayData["payment_confirmation"] like above example. After get response from that request, you have two choices. In first choice, you can directly redirect user to payment gateway page (ex.using form). In second choice, you can send request to $gatewayData["confirm_payment"], this means that, you call setPaymentGatewayRequest method. Generally view file code structure depends on your payment gateway.
$this->api->confirmPaymentGateway($gatewayData['id'],$status_id,$transaction_id,$payment_status);
You must call this function after payment completed. $gatewayData["id"] must not change. You must get $status_id from $settingData. If you want send it directly, you can select status from "Status ID And Title" section. $transaction_id and $payment_status are optional.
Status ID | Title |
---|---|
0 | Received |
1 | Complete |
2 | Total not match |
3 | Denied |
4 | Expired |
5 | Failed |
7 | Processed |
8 | Refunded |
9 | Reversed |
10 | Voided |
11 | Canceled Reversal |
12 | Waiting For Payment |
13 | Pending |