-
Notifications
You must be signed in to change notification settings - Fork 27
/
CardGateExtended.php
204 lines (180 loc) · 7.31 KB
/
CardGateExtended.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Tpay\Example;
use Tpay\OriginApi\Dictionaries\FieldsConfigDictionary;
use Tpay\OriginApi\PaymentForms\PaymentCardForms;
use Tpay\OriginApi\Utilities\Util;
include_once 'config.php';
include_once 'loader.php';
final class CardGateExtended extends PaymentCardForms
{
const SUPPORTED_CARD_VENDORS = [
'visa',
'mastercard',
'maestro',
];
public function __construct()
{
// This is pre-configured sandbox access. You should use your own data in production mode.
$this->cardApiKey = 'bda5eda723bf1ae71a82e90a249803d3f852248d';
$this->cardApiPass = 'IhZVgraNcZoWPLgA1yQcGMIzquVWWrWtJ';
$this->cardKeyRSA = 'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0NCk1JR2ZNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRQ2NLRTVZNU1Wemd5a1Z5ODNMS1NTTFlEMEVrU2xadTRVZm1STS8NCmM5L0NtMENuVDM2ekU0L2dMRzBSYzQwODRHNmIzU3l5NVpvZ1kwQXFOVU5vUEptUUZGVyswdXJacU8yNFRCQkxCcU10TTVYSllDaVQNCmVpNkx3RUIyNnpPOFZocW9SK0tiRS92K1l1YlFhNGQ0cWtHU0IzeHBhSUJncllrT2o0aFJDOXk0WXdJREFRQUINCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==';
$this->cardVerificationCode = '6680181602d396e640cb091ea5418171';
$this->cardHashAlg = 'sha512';
parent::__construct();
}
public function init()
{
if (isset($_POST['carddata'])) {
$this->setPaymentParameters();
if (isset($_POST['card_vendor']) && in_array($_POST['card_vendor'], self::SUPPORTED_CARD_VENDORS)) {
$this->saveUserCardVendor($_POST['card_vendor']);
}
// Try to sale with provided card data
$response = $this->makeCardPayment();
// Successful payment by card not protected by 3DS
if (isset($response['result']) && 1 === (int) $response['result']) {
$this->setOrderAsComplete($response);
// Successfully generated 3DS link for payment authorization
} elseif (isset($response['3ds_url'])) {
header('Location: '.$response['3ds_url']);
} else {
// Invalid credit card data
$this->tryToSaleAgain();
}
} elseif (isset($_POST['savedId'])) {
$this->setPaymentParameters();
// Payment by saved card
$this->processSavedCardPayment($_POST['savedId']);
} else {
$userCards = $this->getUserSavedCards($this->getCurrentUserId());
// Show new payment form
echo $this->getOnSiteCardForm('CardGateExtended.php', true, false, $userCards);
}
}
private function setPaymentParameters()
{
$this->setAmount(1)->setCurrency(985)->setOrderID('test payment 123');
$this->setLanguage('en')->setReturnUrls('https://tpay.com', 'https://google.pl');
}
private function saveUserCardVendor($cardVendor)
{
// Code saving the user card vendor name for later use
}
private function makeCardPayment($failOver = false)
{
// If you set the third getOnSiteCardForm() parameter true, you can get client name and email here. Otherwise, you must get those values from your DB.
$clientEmail = '[email protected]';
$clientName = 'John Doe';
$cardData = Util::post('carddata', FieldsConfigDictionary::STRING);
$saveCard = Util::post('card_save', FieldsConfigDictionary::STRING);
Util::log('Secure Sale post params', print_r($_POST, true));
if ('on' === $saveCard) {
$this->setOneTimer(false);
}
return false === $failOver
? $this->registerSale($clientName, $clientEmail, 'test sale', $cardData)
: $this->setCardData(null)->registerSale($clientName, $clientEmail, 'test sale');
}
private function processSavedCardPayment($savedCardId)
{
$exampleCurrentUserId = $this->getCurrentUserId();
if (!is_numeric($savedCardId)) {
Util::log('Invalid saved cardId', 'CardId: '.$savedCardId);
return $this->tryToSaleAgain();
}
$requestedCardId = (int) $savedCardId;
$currentUserCards = $this->getUserSavedCards($exampleCurrentUserId);
$isValid = false;
$cardToken = '';
foreach ($currentUserCards as $card) {
if (($card['cardId']) === $requestedCardId) {
$isValid = true;
$cardToken = $card['cli_auth'];
}
}
if (false === $isValid) {
Util::log(
'Unauthorized payment try',
sprintf('User %s has tried to pay by not owned cardId: %s', $exampleCurrentUserId, $requestedCardId)
);
// Reject current payment try and redirect user to tpay payment panel new card form
return $this->tryToSaleAgain();
}
return $this->payBySavedCard($cardToken);
}
private function payBySavedCard($cardToken)
{
$this->setClientToken($cardToken);
$transaction = $this->presaleMethod('test sale');
if (!isset($transaction['sale_auth'])) {
return $this->tryToSaleAgain();
}
$transaction['sale_auth'];
$result = $this->saleMethod($transaction['sale_auth']);
if (isset($result['status']) && 'correct' === $result['status']) {
return $this->setOrderAsComplete($result);
}
return $this->tryToSaleAgain();
}
private function setOrderAsComplete($params)
{
// Code setting the order status and other details in your system
var_dump($params);
}
private function tryToSaleAgain()
{
// Try to create new transaction and redirect customer to Tpay transaction panel
$response = $this->makeCardPayment(true);
if (isset($response['sale_auth'])) {
header('Location: https://secure.tpay.com/cards/?sale_auth='.$response['sale_auth']);
} else {
echo $response['err_desc'];
}
}
/**
* Returns stored cards by userId as array. Each row contains card details
*
* @param int $userId
*
* @return array
*/
private function getUserSavedCards($userId = 0)
{
// Code getting current logged user cards from your DB. This is only an example of DB.
$exampleDbUsersIdsWithCards = [
0 => [],
2 => [
[
'cardId' => 1,
'vendor' => 'visa',
'shortCode' => '****1111',
'cli_auth' => 't5ca63654a3c44a8fac1dea7f1227b9f5d8dc4af',
],
[
'cardId' => 2,
'vendor' => 'mastercard',
'shortCode' => '****4444',
'cli_auth' => 't5ca636697eebe24b5c2cf02f5d7723f1297f825',
],
],
3 => [
[
'cardId' => 3,
'vendor' => 'visa',
'shortCode' => '****3321',
'cli_auth' => 't5ca6367039f480aa9df557798b47748681f1f05',
],
],
];
if (isset($exampleDbUsersIdsWithCards[$userId])) {
return $exampleDbUsersIdsWithCards[$userId];
}
return [];
}
private function getCurrentUserId()
{
// Code getting the user Id from your system. This is only an example.
return 2;
}
}
(new CardGateExtended())->init();