|
1
|
|
|
<?php |
|
2
|
|
|
namespace Paranoia\Builder\Gvp; |
|
3
|
|
|
|
|
4
|
|
|
use Paranoia\Builder\AbstractRequestBuilder; |
|
5
|
|
|
use Paranoia\Configuration\AbstractConfiguration; |
|
6
|
|
|
use Paranoia\Configuration\Gvp as GvpConfiguration; |
|
7
|
|
|
use Paranoia\Formatter\Gvp\ExpireDateFormatter; |
|
8
|
|
|
use Paranoia\Formatter\IsoNumericCurrencyCodeFormatter; |
|
9
|
|
|
use Paranoia\Formatter\MoneyFormatter; |
|
10
|
|
|
use Paranoia\Formatter\SingleDigitInstallmentFormatter; |
|
11
|
|
|
use Paranoia\Request\Request; |
|
12
|
|
|
use Paranoia\Request\Resource\Card; |
|
13
|
|
|
use Paranoia\Request\Resource\ResourceInterface; |
|
14
|
|
|
|
|
15
|
|
|
abstract class BaseRequestBuilder extends AbstractRequestBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
const API_VERSION = '0.01'; |
|
18
|
|
|
const CARD_HOLDER_PRESENT_CODE_NON_3D = 0; |
|
19
|
|
|
const CARD_HOLDER_PRESENT_CODE_3D = 13; |
|
20
|
|
|
|
|
21
|
|
|
/** @var MoneyFormatter */ |
|
22
|
|
|
protected $amountFormatter; |
|
23
|
|
|
|
|
24
|
|
|
/** @var IsoNumericCurrencyCodeFormatter */ |
|
25
|
|
|
protected $currencyCodeFormatter; |
|
26
|
|
|
|
|
27
|
|
|
/** @var SingleDigitInstallmentFormatter */ |
|
28
|
|
|
protected $installmentFormatter; |
|
29
|
|
|
|
|
30
|
|
|
/** @var ExpireDateFormatter */ |
|
31
|
|
|
protected $expireDateFormatter; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
AbstractConfiguration $configuration, |
|
35
|
|
|
IsoNumericCurrencyCodeFormatter $currencyCodeFormatter, |
|
36
|
|
|
MoneyFormatter $amountFormatter, |
|
37
|
|
|
SingleDigitInstallmentFormatter $installmentFormatter, |
|
38
|
|
|
ExpireDateFormatter $expireDateFormatter |
|
39
|
|
|
) { |
|
40
|
|
|
parent::__construct($configuration); |
|
41
|
|
|
$this->currencyCodeFormatter = $currencyCodeFormatter; |
|
42
|
|
|
$this->amountFormatter = $amountFormatter; |
|
43
|
|
|
$this->installmentFormatter = $installmentFormatter; |
|
44
|
|
|
$this->expireDateFormatter = $expireDateFormatter; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function buildBaseRequest(Request $request) |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var GvpConfiguration $configuration */ |
|
50
|
|
|
$configuration = $this->configuration; |
|
51
|
|
|
return [ |
|
52
|
|
|
'Version' => self::API_VERSION, |
|
53
|
|
|
'Mode' => $configuration->getMode(), |
|
54
|
|
|
'Terminal' => $this->buildTerminal($request), |
|
55
|
|
|
'Order' => $this->buildOrder($request), |
|
56
|
|
|
'Customer' => $this->buildCustomer(), |
|
57
|
|
|
'Transaction' => $this->buildTransaction($request) |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
abstract protected function buildTransaction(Request $request); |
|
62
|
|
|
abstract protected function getCredentialPair(); |
|
63
|
|
|
abstract protected function buildHash(Request $request, $password); |
|
64
|
|
|
|
|
65
|
|
|
protected function buildCustomer() |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
'IPAddress' => '127.0.0.1', |
|
69
|
|
|
'EmailAddress' => '[email protected]' |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function generateSecurityHash($password) |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var GvpConfiguration $configuration */ |
|
76
|
|
|
$configuration = $this->configuration; |
|
77
|
|
|
|
|
78
|
|
|
$tidPrefix = str_repeat('0', 9 - strlen($configuration->getTerminalId())); |
|
79
|
|
|
$terminalId = sprintf('%s%s', $tidPrefix, $configuration->getTerminalId()); |
|
80
|
|
|
return strtoupper(SHA1(sprintf('%s%s', $password, $terminalId))); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function buildTerminal(Request $request) |
|
84
|
|
|
{ |
|
85
|
|
|
/** @var GvpConfiguration $configuration */ |
|
86
|
|
|
$configuration = $this->configuration; |
|
87
|
|
|
|
|
88
|
|
|
list($username, $password) = $this->getCredentialPair(); |
|
89
|
|
|
|
|
90
|
|
|
$hash = $this->buildHash($request, $password); |
|
91
|
|
|
|
|
92
|
|
|
return array( |
|
93
|
|
|
'ProvUserID' => $username, |
|
94
|
|
|
'HashData' => $hash, |
|
95
|
|
|
'UserID' => $username, |
|
96
|
|
|
'ID' => $configuration->getTerminalId(), |
|
97
|
|
|
'MerchantID' => $configuration->getMerchantId() |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function buildCard(ResourceInterface $card) |
|
102
|
|
|
{ |
|
103
|
|
|
assert($card instanceof Card); |
|
104
|
|
|
|
|
105
|
|
|
/** @var Card $_card */ |
|
106
|
|
|
$_card = $card; |
|
107
|
|
|
|
|
108
|
|
|
$expireMonth = $this->expireDateFormatter->format( |
|
109
|
|
|
[ |
|
110
|
|
|
$_card->getExpireMonth(), |
|
111
|
|
|
$_card->getExpireYear() |
|
112
|
|
|
] |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
return array( |
|
116
|
|
|
'Number' => $_card->getNumber(), |
|
117
|
|
|
'ExpireDate' => $expireMonth, |
|
118
|
|
|
'CVV2' => $_card->getSecurityCode() |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function buildOrder(Request $request) |
|
123
|
|
|
{ |
|
124
|
|
|
return [ |
|
125
|
|
|
'OrderID' => $request->getOrderId(), |
|
126
|
|
|
'GroupID' => null, |
|
127
|
|
|
'Description' => null |
|
128
|
|
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|