forked from tpay-com/tpay-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardPaymentLinkBuilder.php
117 lines (105 loc) · 3.77 KB
/
CardPaymentLinkBuilder.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
<?php
namespace tpayLibs\examples;
use tpayLibs\src\_class_tpay\PaymentForms\PaymentCardForms;
use tpayLibs\src\_class_tpay\Utilities\TException;
include_once 'config.php';
include_once 'loader.php';
class CardPaymentLinkBuilder extends PaymentCardForms
{
const REQUIRED_FIELDS = [
'name',
'desc',
'email',
'amount',
'currency',
];
const ALLOWED_CURRENCIES = [
'985' => 'PLN',
'826' => 'GBP',
'840' => 'USD',
'978' => 'EUR',
'203' => 'CZK',
'578' => 'NOK',
'208' => 'DKK',
'752' => 'SEK',
'756' => 'CHF',
'980' => 'UAH',
'643' => 'RUB',
'36' => 'AUD',
'348' => 'HUF',
'554' => 'NZD',
'702' => 'SGD',
'682' => 'SAR',
'124' => 'CAD',
'344' => 'HKD',
];
const ALLOWED_LANGUAGES = [
'PL',
'EN',
];
public function __construct()
{
$this->cardApiKey = 'bda5eda723bf1ae71a82e90a249803d3f852248d';
$this->cardApiPass = 'IhZVgraNcZoWPLgA';
$this->cardKeyRSA = 'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0NCk1JR2ZNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRQ2NLRTVZNU1Wemd5a1Z5ODNMS1NTTFlEMEVrU2xadTRVZm1STS8NCmM5L0NtMENuVDM2ekU0L2dMRzBSYzQwODRHNmIzU3l5NVpvZ1kwQXFOVU5vUEptUUZGVyswdXJacU8yNFRCQkxCcU10TTVYSllDaVQNCmVpNkx3RUIyNnpPOFZocW9SK0tiRS92K1l1YlFhNGQ0cWtHU0IzeHBhSUJncllrT2o0aFJDOXk0WXdJREFRQUINCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ';
$this->cardVerificationCode = '6680181602d396e640cb091ea5418171';
$this->cardHashAlg = 'sha1';
parent::__construct();
}
public function init()
{
if (!empty($_POST)) {
$this->getCardPaymentLink();
} else {
$this->showBuilderForm();
}
}
public function showBuilderForm()
{
$data['currencies'] = static::ALLOWED_CURRENCIES;
$data['languages'] = static::ALLOWED_LANGUAGES;
$data['actionPath'] = '';
echo $this->getCardPaymentLinkBuilderForm($data);
}
public function getCardPaymentLink()
{
try {
foreach (static::REQUIRED_FIELDS as $fieldName) {
if (!isset($_POST[$fieldName])) {
throw new TException(sprintf('Required field %s is missing', $fieldName));
}
}
$_POST['amount'] = str_replace(',', '.', $_POST['amount']);
$this
->setAmount((double)$_POST['amount'])
->setCurrency((int)$_POST['currency']);
$this->setNotRequiredFields();
$transaction = $this->registerSale($_POST['name'], $_POST['email'], $_POST['desc']);
if (isset($transaction['sale_auth']) === false) {
echo sprintf('Error generating transaction. Tpay server response: %s', $transaction['err_desc']);
}
$transactionId = $transaction['sale_auth'];
echo sprintf(
'Build successful. Transaction link: https://secure.tpay.com/cards/?sale_auth=%s',
$transactionId
);
} catch (TException $e) {
echo 'Unable to generate transaction. Reason: '.$e->getMessage();
}
}
private function setNotRequiredFields()
{
if (isset($_POST['language']) && !empty($_POST['language'])) {
$this->setLanguage(strtoupper($_POST['language']));
} else {
$this->setLanguage('EN');
}
if (isset($_POST['order_id']) && !empty($_POST['order_id'])) {
$this->setOrderID($_POST['order_id']);
}
if (isset($_POST['return_url_success'], $_POST['return_url_error'])) {
$this->setReturnUrls($_POST['return_url_success'], $_POST['return_url_error']);
}
}
}
(new CardPaymentLinkBuilder)->init();