<?php
namespace App\Models;
use App\Models\Card;
use App\Models\Admin\Admin;
use App\Constants\GlobalConst;
use Illuminate\Support\Facades\DB;
use App\Constants\PaymentGatewayConst;
use App\Models\Admin\PaymentGatewayCurrency;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Transaction extends Model
{
use HasFactory;
protected $casts = [
'id' => 'integer',
'trx_id' => 'string',
'type' => 'string',
'user_id' => 'integer',
'user_wallet_id' => 'integer',
'card_id' => 'integer',
'payment_gateway_currency_id' => 'integer',
'payment_type' => 'string',
'payable_charge' => 'integer',
'request_amount' => 'decimal:8',
'fixed_charge' => 'decimal:8',
'percent_charge' => 'decimal:8',
'total_charge' => 'decimal:8',
'total_payable' => 'decimal:8',
'request_currency' => 'string',
'available_balance' => 'decimal:8',
'payment_currency' => 'string',
'remark' => 'string',
'details' => 'object',
'attribute' => 'string',
'status' => 'integer',
'reject_reason' => 'string',
'callback_ref' => 'integer',
];
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo(User::class);
}
public function card_method(){
return $this->belongsTo(Card::class,'card_id');
}
public function admin() {
return $this->belongsTo(Admin::class,'user_id');
}
public function getRouteKeyName()
{
return "trx_id";
}
public function user_wallet()
{
return $this->belongsTo(UserWallet::class, 'user_wallet_id');
}
public function payment_gateway()
{
return $this->belongsTo(PaymentGatewayCurrency::class,'payment_gateway_currency_id');
}
public function getCreatorAttribute() {
if($this->user_type == GlobalConst::USER) {
return $this->user;
}else if($this->user_type == GlobalConst::ADMIN) {
return $this->admin;
}
}
public function receiver_info() {
return $this->belongsTo(User::class,'receiver_id');
}
public function getReceiverAttribute() {
if($this->receiver_type == GlobalConst::USER) {
return $this->receiver_info;
}
}
public function getCreatorWalletAttribute() {
if($this->user_type == GlobalConst::USER) {
return $this->user_wallet;
}else if($this->user_type == GlobalConst::ADMIN) { // if user type ADMIN wallet_id is user wallet id. Because admin has no wallet.
return $this->user_wallet;
}
}
public function getStringStatusAttribute() {
$status = $this->status;
$data = [
'class' => "",
'value' => "",
];
if($status == PaymentGatewayConst::STATUSSUCCESS) {
$data = [
'class' => "badge badge--success",
'value' => "Success",
];
}else if($status == PaymentGatewayConst::STATUSPENDING) {
$data = [
'class' => "badge badge--warning",
'value' => "Pending",
];
}else if($status == PaymentGatewayConst::STATUSHOLD) {
$data = [
'class' => "badge badge--warning",
'value' => "Hold",
];
}else if($status == PaymentGatewayConst::STATUSREJECTED) {
$data = [
'class' => "badge badge--danger",
'value' => "Rejected",
];
}else if($status == PaymentGatewayConst::STATUSWAITING) {
$data = [
'class' => "badge badge--danger",
'value' => "Waiting",
];
}else if($status == PaymentGatewayConst::STATUSTEST) {
$data = [
'class' => "badge badge--danger",
'value' => "Test card detected. Please use a live card to proceed.",
];
}
return (object) $data;
}
public function scopePending($query) {
return $query->where('status',PaymentGatewayConst::STATUSPENDING);
}
public function scopeComplete($query) {
return $query->where('status',PaymentGatewayConst::STATUSSUCCESS);
}
public function scopeReject($query) {
return $query->where('status',PaymentGatewayConst::STATUSREJECTED);
}
public function scopeAddMoney($query) {
return $query->where('type',PaymentGatewayConst::TYPEADDMONEY);
}
public function scopeChartData($query) {
return $query->select([
DB::raw("DATE(created_at) as date"),
DB::raw('COUNT(*) as total')
])
->groupBy('date')
->pluck('total');
}
public function scopeThisMonth($query) {
return $query->whereBetween('created_at',[now()->startOfMonth(),now()->endOfMonth()]);
}
public function scopeThisYear($query) {
return $query->whereBetween('created_at',[now()->startOfYear(),now()->endOfYear()]);
}
public function scopeYearChartData($query) {
return $query->select([
DB::raw('sum(total_charge) as total, YEAR(created_at) as year, MONTH(created_at) as month'),
])->groupBy('year','month')->pluck('total','month');
}
public function scopeAuth($query) {
return $query->where('user_id',auth()->user()->id);
}
public function scopeMoneyTransfer($query) {
return $query->where('type',PaymentGatewayConst::TYPETRANSFERMONEY);
}
public function scopeSearch($query,$data) {
return $query->where("trx_id","like","%".$data."%");
}
}
Get access token to initiates payment transaction.
generate-token
| Parameter | Type | Comments |
|---|---|---|
| client_id | string | Enter merchant API client/primary key |
| secret_id | string | Enter merchant API secret key |
| env | string | Enter merchant API environment |
| merchant_id | string | Enter merchant API merchant id |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url. 'v1/generate-token', [
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'client_id' => '$client_id',
'secret_id' => 'secret_id',
'env' => 'env',
'merchant_id' => 'merchant_id',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Successfully token is generated"
]
},
"data": {
"token":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9PSIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtnVkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2xoU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtcG9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRiQ0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3JzbnIyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYzkzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid credentials."
]
},
"data": null,
"type": "error"
}