-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCardGate.php
123 lines (110 loc) · 4.15 KB
/
CardGate.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
<?php
namespace Tpay\Example\TransactionsApi;
use Tpay\Example\ExamplesConfig;
use Tpay\OpenApi\Api\TpayApi;
use Tpay\OpenApi\Forms\PaymentForms;
use Tpay\OpenApi\Model\Fields\FieldTypes;
use Tpay\OpenApi\Utilities\TpayException;
use Tpay\OpenApi\Utilities\Util;
final class CardGate extends ExamplesConfig
{
private $TpayApi;
public function __construct()
{
parent::__construct();
$this->TpayApi = new TpayApi(self::MERCHANT_CLIENT_ID, self::MERCHANT_CLIENT_SECRET, true, 'read');
}
public function init()
{
if (empty($_POST)) {
$PaymentForms = new PaymentForms('en');
// Show new payment form
echo $PaymentForms->getOnSiteCardForm(self::MERCHANT_RSA_KEY, 'CardGate.php', true, false);
} else {
$this->processNewCardPayment();
}
}
private function processNewCardPayment()
{
if (isset($_POST['card_vendor'], $_POST['card_short_code'])) {
$this->saveUserCardDetails($_POST['card_vendor'], $_POST['card_short_code']);
}
$transaction = $this->getNewCardTransaction();
if (!isset($transaction['transactionId'])) {
// Code error handling @see POST /transactions HTTP 400 response details
throw new TpayException('Unable to create transaction. Response: '.json_encode($transaction));
}
// Try to sale with provided card data
$response = $this->makeCardPayment($transaction);
if (!isset($response['result']) || 'failure' === $response['result']) {
header('Location: '.$transaction['transactionPaymentUrl']);
}
if (isset($response['status']) && 'correct' === $response['status']) {
// Successful payment by card not protected by 3DS
$this->setOrderAsComplete($response);
} elseif (isset($response['transactionPaymentUrl'])) {
// Successfully generated 3DS link for payment authorization
header('Location: '.$response['transactionPaymentUrl']);
} else {
// Invalid credit card data
header('Location: '.$transaction['transactionPaymentUrl']);
}
}
private function makeCardPayment($transaction)
{
if (isset($_POST['card_save'])) {
$saveCard = Util::cast($_POST['card_save'], FieldTypes::STRING);
} else {
$saveCard = false;
}
$cardData = Util::cast($_POST['carddata'], FieldTypes::STRING);
$request = [
'groupId' => 103,
'cardPaymentData' => [
'card' => $cardData,
'save' => 'on' === $saveCard,
],
'method' => 'pay_by_link',
];
return $this->TpayApi->transactions()->createPaymentByTransactionId($request, $transaction['transactionId']);
}
private function setOrderAsComplete($response)
{
var_dump($response);
}
private function getNewCardTransaction()
{
// If you set the fourth getOnSiteCardForm() parameter true, you can get client name and email here.
// Otherwise, you must get those values from your DB.
$clientEmail = 'customer@example.com';
$clientName = 'John Doe';
$request = [
'amount' => 0.10,
'description' => 'test transaction',
'hiddenDescription' => 'order_213',
'payer' => [
'email' => $clientEmail,
'name' => $clientName,
],
'lang' => 'pl',
'callbacks' => [
'notification' => [
'url' => 'https://example.com/notification',
],
'payerUrls' => [
'success' => 'https://example.com/success',
'error' => 'https://example.com/error',
],
],
'pay' => [
'groupId' => 103,
],
];
return $this->TpayApi->transactions()->createTransaction($request);
}
private function saveUserCardDetails($cardVendor, $cardShortCode)
{
// Code saving the user card vendor name and short code for later use
}
}
(new CardGate())->init();