2025-11-06 13:41:06 +08:00

239 lines
7.5 KiB
PHP

<?php
namespace App\Libraries;
use App\Models\LogPaymentTransactions;
use App\Models\Orders;
use App\Models\TopupModel;
class Fiuu
{
protected $endpoint;
protected $merchantId;
protected $verifyKey;
public function __construct()
{
$this->endpoint = FIUU_SANDBOX_ENDPOINT;
$this->merchantId = FIUU_MERCHANT_ID;
$this->verifyKey = FIUU_VERIFY_KEY;
helper('general');
helper('order');
}
// public function createPayment($orderId, $amount)
// {
// $vcode = md5($amount . $this->merchantId . $orderId . $this->verifyKey);
// $query = http_build_query([
// 'merchantid' => $this->merchantId,
// 'orderid' => $orderId,
// 'amount' => $amount,
// 'vcode' => $vcode,
// ]);
// $redirectUrl = 'https://sandbox-payment.fiuu.com/RMS/pay/' . $this->merchantId . '/?' . $query;
// return [
// 'status' => 'success',
// 'vcode' => $vcode,
// 'redirect_url' => $redirectUrl,
// ];
// }
public function createPayment($order_so, $amount, $customer = [])
{
$vcode = md5($amount . $this->merchantId . $order_so . $this->verifyKey);
$fields = [
'orderid' => $order_so,
'currency' => 'MYR',
'amount' => $amount,
'vcode' => $vcode,
'bill_name' => 'John Doe',
'bill_email' => 'johndoe@example.com',
'bill_mobile' => '60123456789',
'bill_desc' => 'Test Payment',
'returnurl' => 'https://uspizza.ipsgroup.com.my/screens/payment/loading_payment?type=order',
'callbackurl' => 'https://icom.ipsgroup.com.my/api/payment/fiuu/notification'
];
$query = http_build_query($fields);
$redirectUrl = $this->endpoint . '?' . $query;
return [
'status' => 'success',
'vcode' => $vcode,
'redirect_url' => $redirectUrl,
];
}
public function createTopup($order_so, $amount, $customer = [])
{
$vcode = md5($amount . $this->merchantId . $order_so . $this->verifyKey);
$fields = [
'orderid' => $order_so,
'currency' => 'MYR',
'amount' => $amount,
'vcode' => $vcode,
'bill_name' => 'John Doe',
'bill_email' => 'johndoe@example.com',
'bill_mobile' => '60123456789',
'bill_desc' => 'Test Payment',
'returnurl' => 'https://uspizza.ipsgroup.com.my/screens/payment/loading_payment?type=topup',
'callbackurl' => 'https://icom.ipsgroup.com.my/api/topup/fiuu/notification'
];
$query = http_build_query($fields);
$redirectUrl = $this->endpoint . '?' . $query;
return [
'status' => 'success',
'vcode' => $vcode,
'redirect_url' => $redirectUrl,
];
}
public function validateToken($data){
$tranID = $data['tranID'];
$order_so = $data['orderid'];
$status = $data['status'];
$domain = $data['domain'];
$amount = $data['amount'];
$currency = $data['currency'];
$appcode = $data['appcode'];
$paydate = $data['paydate'];
$skey = $data['skey'];
$pre_skey = md5($tranID.$order_so.$status.$domain.$amount.$currency);
$myskey = md5($paydate.$domain.$pre_skey.$appcode.$this->verifyKey);
return $skey == $myskey;
}
public function paymentNotification($data){
$tranID = $data['tranID'];
$order_so = $data['orderid'];
$status = $data['status'];
$domain = $data['domain'];
$amount = $data['amount'];
$currency = $data['currency'];
$appcode = $data['appcode'];
$paydate = $data['paydate'];
$skey = $data['skey'];
$log_payment_transactions = new LogPaymentTransactions();
$log_payment_transactions->insert([
'order_id' => 0,
'order_type' => 'order',
'url' => 'notification',
'request' => json_encode($data),
'respond' => '',
'result' => $skey.'-check-token',
'status' => $status,
]);
$is_valid = $this->validateToken($data);
if (!$is_valid) {
$result = 'invalid-token'; // Invalid SKEY, ignore or log as fraud attempt
}
$orders = new Orders();
$order = $orders->where('order_so', $order_so)->first();
$order_id = $order['id'];
if($status == '00' || $status == '0'){
if(completeOrder($order_id)){
$result = 'success-payment';
}else{
$result = 'failed-payment';
}
}else{
$explode_order_so = explode('-', $order_so);
$order_so_number = $explode_order_so[1] ?? 0;
$order_so_number++;
$order_so = $explode_order_so[0].'-'.$order_so_number;
$orders->update($order_id, ['order_so' => $order_so]);
$result = 'failed-payment';
}
$log_payment_transactions = new LogPaymentTransactions();
$log_payment_transactions->insert([
'order_id' => $order_id,
'order_type' => 'order',
'url' => 'notification',
'request' => json_encode($data),
'respond' => json_encode($result),
'result' => $skey.'-'.$is_valid,
'status' => $status,
]);
return $result;
// if($status == '00'){
// //send notification to fiuu server
// send_api_request('POST', );
// }
}
public function topupNotification($data)
{
$tranID = $data['tranID'];
$topup_number = $data['orderid'];
$status = $data['status'];
$domain = $data['domain'];
$amount = $data['amount'];
$currency = $data['currency'];
$appcode = $data['appcode'];
$paydate = $data['paydate'];
$skey = $data['skey'];
$log_payment_transactions = new LogPaymentTransactions();
$log_payment_transactions->insert([
'order_id' => 0,
'order_type' => 'topup',
'url' => 'topup-notification',
'request' => json_encode($data),
'respond' => '',
'result' => $skey.'-check-token',
'status' => $status,
]);
$is_valid = $this->validateToken($data);
if (!$is_valid) {
$result = 'invalid-token';
}
$topups = new TopupModel();
$topup = $topups->where('topup_number', $topup_number)->first();
if (!$topup) {
$result = 'topup-not-found';
} else {
if ($status == '00' || $status == '0') {
$topups->update($topup['id'], ['status' => 'Success']);
$result = 'success-topup';
} else {
$topups->update($topup['id'], ['status' => 'Failed']);
$result = 'failed-topup';
}
}
$log_payment_transactions->insert([
'order_id' => $topup['id'] ?? 0,
'order_type' => 'topup',
'url' => 'topup-notification',
'request' => json_encode($data),
'respond' => json_encode($result),
'result' => $skey.'-'.$is_valid,
'status' => $status,
]);
return $result;
}
}