Your IP : 216.73.216.79


Current Path : /var/www/html/ebookingdbkl/common/models/
Upload File :
Current File : /var/www/html/ebookingdbkl/common/models/PaymentDetails.php

<?php

namespace common\models;

use Yii;
use \common\components\payment\provider\Epic;

/**
 * This is the model class for table "payment_details".
 *
 * @property int $payment_details_id
 * @property string|null $module
 * @property int $payment_fk
 * @property int|null $type
 * @property string|null $jenis_pelanggan
 * @property string|null $receipt_no
 * @property float|null $receipt_amount
 * @property string|null $receipt_date
 * @property string|null $kod_hasil
 * @property string|null $nama_pemohon
 * @property string|null $nationality
 * @property string|null $jenis_pengenalan
 * @property string|null $no_pengenalan
 * @property string|null $email
 * @property string|null $address_1
 * @property string|null $address_2
 * @property string|null $address_3
 * @property string|null $poskod
 * @property string|null $city
 * @property string|null $phone_number
 * @property string|null $account_no
 * @property string|null $no_bil
 * @property float|null $bil_amount
 * @property string|null $bil_date
 * @property int|null $epic_trans_id
 * @property string|null $unique_id
 * @property string|null $process_code
 * @property string|null $request_id
 * @property string|null $sp_code
 * @property string|null $payment_time
 * @property string|null $pay_mode FPX = individual, FPX1 = Corporate, MIGS = Credit card
 * @property string|null $module_id
 * @property string|null $checksum
 */
class PaymentDetails extends \yii\db\ActiveRecord
{

    /** nationality mas code **/
    const MAS_CITIZEN_CODE = 'MAS';

    const JENIS_EPIC = 5;

    const PROCESS_CODE = '44'; //44 according to doc

    public $t_module_id;

    public $t_charge_type;

    public $t_charge_category;

    public $t_location;

    private static function ID_TYPE($id) {
        $type = [
            'MD' => '1',
            'PASSPORT' => '2',
            'ROC' => '3'
        ];

        return isset($type[$id]) ? $type[$id] : 1;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'payment_details';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['payment_fk'], 'required'],
            [['epic_trans_id', 'aset_unique_id', 'unique_id', 'is_create_account', 'retry', 'response_json', 'account_no', 'no_bil'], 'safe'],
            [['epic_trans_id'], 'default', 'value' => ''],
            [['payment_fk', 'jenis_epic'], 'integer'],
            [['receipt_amount', 'bil_amount'], 'number'],
            [['receipt_date', 'bil_date', 'payment_time', 'address_2', 'address_3'], 'safe'],
            [['module'], 'string', 'max' => 25],
            [['jenis_pelanggan', 'module_id'], 'string', 'max' => 2],
            [['receipt_no'], 'string', 'max' => 20],
            [['kod_hasil', 'process_code', 'pay_mode'], 'string', 'max' => 5],
            [['nama_pemohon'], 'string', 'max' => 250],
            [['nationality'], 'string', 'max' => 3],
            [['jenis_pengenalan'], 'string', 'max' => 10],
            [['no_pengenalan'], 'string', 'max' => 30],
            [['email'], 'string', 'max' => 150],
            [['address_1'], 'string', 'max' => 500],
            [['poskod'], 'string', 'max' => 6],
            [['city'], 'string', 'max' => 100],
            [['phone_number'], 'string', 'max' => 15],
            [['request_id'], 'string', 'max' => 24],
            [['sp_code'], 'string', 'max' => 7],
            [['checksum'], 'string', 'max' => 65],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'payment_details_id' => Yii::t('app', 'Payment Details ID'),
            'module' => Yii::t('app', 'Module'),
            'payment_fk' => Yii::t('app', 'Payment Fk'),
            'type' => Yii::t('app', 'Type'),
            'jenis_pelanggan' => Yii::t('app', 'Jenis Pelanggan'),
            'receipt_no' => Yii::t('app', 'Receipt No'),
            'receipt_amount' => Yii::t('app', 'Receipt Amount'),
            'receipt_date' => Yii::t('app', 'Receipt Date'),
            'kod_hasil' => Yii::t('app', 'Kod Hasil'),
            'nama_pemohon' => Yii::t('app', 'Nama Pemohon'),
            'nationality' => Yii::t('app', 'Nationality'),
            'jenis_pengenalan' => Yii::t('app', 'Jenis Pengenalan'),
            'no_pengenalan' => Yii::t('app', 'No Pengenalan'),
            'email' => Yii::t('app', 'Email'),
            'address_1' => Yii::t('app', 'Address 1'),
            'address_2' => Yii::t('app', 'Address 2'),
            'address_3' => Yii::t('app', 'Address 3'),
            'poskod' => Yii::t('app', 'Poskod'),
            'city' => Yii::t('app', 'City'),
            'phone_number' => Yii::t('app', 'Phone Number'),
            'account_no' => Yii::t('app', 'Account No'),
            'no_bil' => Yii::t('app', 'No Bil'),
            'bil_amount' => Yii::t('app', 'Bil Amount'),
            'bil_date' => Yii::t('app', 'Bil Date'),
            'epic_trans_id' => Yii::t('app', 'Epic Trans ID'),
            'unique_id' => Yii::t('app', 'Unique ID'),
            'process_code' => Yii::t('app', 'Process Code'),
            'request_id' => Yii::t('app', 'Request ID'),
            'sp_code' => Yii::t('app', 'Sp Code'),
            'payment_time' => Yii::t('app', 'Payment Time'),
            'pay_mode' => Yii::t('app', 'Pay Mode'),
            'module_id' => Yii::t('app', 'Module ID'),
            'checksum' => Yii::t('app', 'Checksum'),
        ];
    }

    public function processPaymentDetails(string $module, object $pay, $payment_id, $receipt_no=null) {

        $user = \common\models\FrontendUser::findOne($pay->user_fk);
        
        $email = '-';
        if(!empty($pay->email)) {
            $email = $pay->email;
        } else if (!empty($user->email)){
            $email = $user->email;
        }
    
        $data = [
            'JENIS_EPIC' => self::JENIS_EPIC, // for testing
            'JENIS_PELANGGAN' => '1',
            'IS_CREATE_ACCOUNT' => '1',
            'RECEIPT_NO' => $receipt_no,
            'RECEIPT_AMOUNT' => number_format($pay->amount, 2),
            'RECEIPT_DATE' => date('d/m/Y'),
            'KOD_HASIL' => $pay->revenue_code,
            'MODULE_ID' => $this->t_module_id,
            'KATEGORI_CAJ' => $this->t_charge_category,
            'JENIS_CAJ' => $this->t_charge_type,
            'KAWASAN' => $this->t_location,
            'NAMA_PEMOHON' => $user->fullname,
            'NATIONALITY' => (string) $user->nationality == self::MAS_CITIZEN_CODE ? '1' : '2',
            'JENIS_PENGENALAN' => (string) self::ID_TYPE($user->id_type),
            'NO_PENGENALAN' => $user->ic_number,
            'EMAIL' => $email,
            'ADDRESS_1' => self::isEmpty($user->address),
            'ADDRESS_2' => self::isEmpty($user->address_1),
            'ADDRESS_3' => self::isEmpty($user->address_2),
            'POSKOD' => self::isEmpty($user->postcode),
            'CITY' => self::isEmpty($user->city),
            'PHONE_NUMBER' => self::isEmpty($user->mobile_no),
            'ACCOUNT_NO' => $pay->account_no, //todo reset after a month
            'NO_BIL' => $pay->no_bil, //todo reset after a month
            'BIL_AMOUNT' => number_format($pay->amount, 2),
            'BIL_DATE' => date('d/m/Y'),
            'EPIC_TRANS_ID' => $pay->payment_transaction_id,
            // 'UNIQUE_ID' =>  $this->uniqueid($pay->payment_id),
            'UNIQUE_ID' =>  uniqid(),
            'PROCESS_CODE' => self::PROCESS_CODE,
            'SP_CODE' => 'eBKG',
            'PAYMENT_TIME' => date('His'),
            'PAY_MODE' => strtoupper($pay->payment_mode),
            'ASET_UNIQUE_ID' => $pay->payment_type . $payment_id,
            // 'CHECKSUM' =>  substr($pay->checksum,0,60) //cut to 60 char for dream testing maybe changes in the future
            'CHECKSUM' => hash('sha3-224', $pay->transaction_id . strtoupper($pay->payment_mode) . number_format($pay->amount, 2))
        ];

        $pay_details = new self();
        $pay_details->module = $module;
        $pay_details->payment_fk = $payment_id;

        foreach ($data as $key => $value) {
            if($pay_details->hasAttribute(strtolower($key))) {
                $pay_details->{strtolower($key)} = $value;
            }
        }

        $pay_details->status = 2; // failed initial status unless set otherwise
        
        /***************
        *
        *  DREAMS 
        *
        ***************/
        $dreams = (new Epic())->sendToDreams($data);
        
        $body = null;
        if(isset($dreams['body'])) {
            $body = $dreams['body'];
            $pay_details->response_json = json_encode($body);
        }

        if(isset($dreams['success']) && $dreams['success'] == true) {
            if(isset($body->STATUS_CODE) && $body->STATUS_CODE == '00') {
                $pay_details->status = 1;
            }
        }

        if (!$pay_details->save()) {
            throw new \Exception("Cannot save payment details" . json_encode($pay_details->errors));
        }
    }

    public static function uniqueid($payment_id) {
        $uniq = uniqid($payment_id, true);
        // check if unique id is exist
        $uid = self::find()->where(['UNIQUE_ID' => $uniq])->one();

        if(!empty($uid))
            return self::uniqueid($payment_id, true);

        return str_replace('.', '', $uniq);
    }

    public static function isEmpty($value, $default='-') {
        $value = trim($value);

        if(empty($value)) {
            return $default;
        }

        return $value;
    }

    public function sanitize($var, $data, $type='string') {
        if (isset($data[$var])) {
            return \common\components\Sanitize::cast($data[$var], $type);
        }
    }
}