Created
September 3, 2014 18:22
-
-
Save cameri/72a22d45a0cf3ba41593 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('AppModel', 'Model'); | |
/** | |
* CardnetTransaction Model | |
* | |
*/ | |
class CardnetTransaction extends AppModel { | |
/** | |
* Use table | |
* | |
* @var mixed False or table name | |
*/ | |
public $useTable = false; | |
/* | |
'currency_code' => '214', // DOP | |
'acquiring_institution_code' => '349', | |
'merchant_type' => '5440', | |
'merchant_number' => '349000000', | |
'merchant_terminal' => '123242374000', | |
'return_url' => 'https://www.maspormenos.do/orders/payment', | |
'cancel_url' => 'https://www.maspormenos.do/orders/cancel', | |
'page_languaje' => 'ESP', | |
'merchant_name' => 'COMERCIO PARA REALIZAR PRUEBAS DO', | |
'avs' => '33024 1000 ST JOHN PLACE PEMBROKE PINES FLORIDA', | |
*/ | |
/** | |
* Validation rules | |
* | |
* @var array | |
*/ | |
public $validate = array( | |
'transaction_type' => array( | |
'inList' => array( | |
'rule' => array('inList', array('0200', '2240', '0500', '0510')), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'currency_code' => array( | |
'inList' => array( | |
'rule' => array('inList', array('214', '840')), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'acquiring_institution_code' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'merchant_type' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'merchant_number' => array( | |
'numeric_sp_padded' => array( | |
'rule' => array('custom' , '@^[0-9]+\s*$@'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
'between' => array( | |
'rule' => array('between' , 15, 15), | |
//'allowEmpty' => false, | |
//'required' => false, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'merchant_terminal' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
'between' => array( | |
'rule' => array('between' , 12, 12), | |
//'allowEmpty' => false, | |
// 'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'return_url' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'cancel_url' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'page_languaje' => array( | |
'inList' => array( | |
'rule' => array('inList', array('ENG', 'ESP')), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'orden_id' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'transaction_id' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
'between' => array( | |
'rule' => array('between', 6, 6), | |
) | |
), | |
'amount' => array( | |
'zero_padded_numeric' => array( | |
'rule' => array('custom', '@0*[1-9]+@'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
'between' => array( | |
'rule' => array('between' , 12, 12), | |
//'allowEmpty' => false, | |
// 'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'tax' => array( | |
'zero_padded_numeric' => array( | |
'rule' => array('custom', '@(0*[1-9]+|0{12})@'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
'between' => array( | |
'rule' => array('between' , 12, 12), | |
//'allowEmpty' => false, | |
// 'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'merchant_name' => array( | |
'between' => array( | |
'rule' => array('between', 40, 40), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
// 'avs' => array( | |
// 'notEmpty' => array( | |
// 'rule' => array('notEmpty'), | |
// //'allowEmpty' => false, | |
// 'required' => true, | |
// //'last' => false, // Stop validation after this rule | |
// //'on' => 'create', // Limit validation to 'create' or 'update'g operations | |
// ), | |
// ), | |
// 'key_encription_key2' => array( | |
// 'notEmpty' => array( | |
// 'rule' => array('notEmpty'), | |
// //'allowEmpty' => false, | |
// //'required' => true, | |
// //'last' => false, // Stop validation after this rule | |
// //'on' => 'create', // Limit validation to 'create' or 'update' operations | |
// ), | |
// ), | |
// 'key_encription_key' => array( | |
// 'notEmpty' => array( | |
// 'rule' => array('notEmpty'), | |
// //'allowEmpty' => false, | |
// 'required' => true, | |
// //'last' => false, // Stop validation after this rule | |
// //'on' => 'create', // Limit validation to 'create' or 'update' operations | |
// ), | |
// ), | |
'ip_client' => array( | |
'ip' => array( | |
'rule' => array('ip', 'IPv4'), | |
//'allowEmpty' => false, | |
'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'credit_card_number' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'allowEmpty' => false, | |
//'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'authorization_code' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
//'allowEmpty' => false, | |
//'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'retrieval_reference_number' => array( | |
'numeric' => array( | |
'rule' => array('numeric'), | |
//'allowEmpty' => false, | |
//'required' => true, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
); | |
public function setAmount($amount) { | |
$this->data['CardnetTransaction']['amount'] = self::_encodeMoney($amount); | |
} | |
public function getAmount($amount) { | |
return self::_decodeMoney($this->data['CardnetTransaction']['amount']); | |
} | |
public function setTax($tax) { | |
$this->data['CardnetTransaction']['tax'] = self::_encodeMoney($tax); | |
} | |
public function getTax($tax) { | |
return self::_decodeMoney($this->data['CardnetTransaction']['tax']); | |
} | |
private static function _encodeMoney($amount) { | |
return str_pad(number_format($amount,2,'',''), 12, '0', STR_PAD_LEFT); | |
} | |
private static function _decodeMoney($money) { | |
$money = ltrim($money, '0'); // trim leading zeros | |
return (float) $money / 100.0; | |
} | |
public function refreshKeyEncryptionKey() { | |
if (isset($this->data['CardnetTransaction']['merchant_type']) | |
&& isset($this->data['CardnetTransaction']['merchant_number']) | |
&& isset($this->data['CardnetTransaction']['merchant_terminal']) | |
&& isset($this->data['CardnetTransaction']['transaction_id']) | |
&& isset($this->data['CardnetTransaction']['amount']) | |
&& isset($this->data['CardnetTransaction']['tax'])) { | |
$this->data['CardnetTransaction']['key_encription_key'] = md5($this->data['CardnetTransaction']['merchant_type'] | |
. $this->data['CardnetTransaction']['merchant_number'] | |
. $this->data['CardnetTransaction']['merchant_terminal'] | |
. $this->data['CardnetTransaction']['transaction_id'] | |
. $this->data['CardnetTransaction']['amount'] | |
. $this->data['CardnetTransaction']['tax']); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment