/
AbstractAstropay.php
111 lines (102 loc) · 3.27 KB
/
AbstractAstropay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* HiPay Fullservice Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Apache 2.0 Licence
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* http://www.apache.org/licenses/LICENSE-2.0
*
* @copyright Copyright (c) 2016 - HiPay
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0 Licence
*
*/
namespace HiPay\FullserviceMagento\Model\Method\Astropay;
use HiPay\FullserviceMagento\Model\Method\AbstractMethodAPI;
use Magento\Framework\Exception\LocalizedException;
/**
* Abstract Model payment method
*
* @package HiPay\FullserviceMagento
* @author Kassim Belghait <kassim@sirateck.com>
* @copyright Copyright (c) 2016 - HiPay
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0 Licence
* @link https://github.com/hipay/hipay-fullservice-sdk-magento2
*/
class AbstractAstropay extends AbstractMethodAPI
{
/**
* Use per Astropay' smethods payment
*/
const IDENTIFICATION_CPF = 'cpf';
const IDENTIFICATION_CPN = 'cpn';
/**
* Extra informations
*
* @var array
*/
protected $_additionalInformationKeys = ['nationalIdentification', 'cc_type'];
/**
* Assign data to info model instance
*
* @param \Magento\Framework\DataObject $additionalData
* @return $this
* @throws LocalizedException
*/
public function _assignAdditionalInformation(\Magento\Framework\DataObject $additionalData)
{
parent::_assignAdditionalInformation($additionalData);
$info = $this->getInfoInstance();
$info->setCcType($additionalData->getCcType());
return $this;
}
/**
* Validate payment method information object
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function validate()
{
/*
* calling parent validate function
*/
parent::validate();
$info = $this->getInfoInstance();
if(!$info->getCcType()){
return $this;
}
$nationalIdentificationNumber = $info->getAdditionalInformation('nationalIdentification');
switch ($this->_typeIdentification) {
case self::IDENTIFICATION_CPF:
if (!preg_match(
"/(\d{2}[.]?\d{3}[.]?\d{3}[\/]?\d{4}[-]?\d{2})|(\d{3}[.]?\d{3}[.]?\d{3}[-]?\d{2})$/",
$nationalIdentificationNumber
)
) {
throw new \Magento\Framework\Exception\LocalizedException(
__('CPF is not correct, please enter a valid CPF.')
);
}
break;
case self::IDENTIFICATION_CPN:
if (!preg_match("/^[a-zA-Z]{4}\d{6}[a-zA-Z]{6}\d{2}$/", $nationalIdentificationNumber)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('CPN is not correct, please enter a valid CPN.')
);
}
}
return $this;
}
/*
* Get type identification required for payment
*
* @return string cpf|cpn
*/
public function getTypeIdentification()
{
return $this->_typeIdentification;
}
}