-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBlikPayment.php
79 lines (73 loc) · 2.79 KB
/
BlikPayment.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
<?php
namespace Tpay\Example\TransactionsApi;
use Tpay\Example\ExamplesConfig;
use Tpay\OpenApi\Api\TpayApi;
use Tpay\OpenApi\Forms\PaymentForms;
use Tpay\OpenApi\Utilities\TpayException;
final class BlikPayment extends ExamplesConfig
{
public function __construct()
{
parent::__construct();
if (isset($_POST['blik_code'])) {
$this->processPayment($_POST['blik_code']);
} else {
$PaymentForms = new PaymentForms('en');
echo $PaymentForms->getBlikCodeForm('BlikPayment.php');
}
}
protected function processPayment($blikCode)
{
$TpayApi = new TpayApi(self::MERCHANT_CLIENT_ID, self::MERCHANT_CLIENT_SECRET, true, 'read');
$transaction = $TpayApi->transactions()->createTransaction($this->getRequestBody());
if (isset($transaction['transactionId'])) {
$blikPaymentFields = [
'groupId' => 150,
'method' => 'transfer',
'blikPaymentData' => [
'blikToken' => $blikCode,
'type' => 0,
],
];
$result = $TpayApi->transactions()
->createPaymentByTransactionId($blikPaymentFields, $transaction['transactionId']);
if (isset($result['result']) && 'success' === $result['result']) {
// The BLIK code was valid, now the customer needs to confirm payment on his mobile app
// Redirect client to thank you page and wait for asynchronous POST payment notification
// Do not mark your order as paid here!
echo 'BLIK code is valid';
} else {
// The BLIK code was incorrect, redirect to transaction panel to try again
header('Location: '.$transaction['transactionPaymentUrl']);
}
} else {
// Code error handling @see POST /transactions HTTP 400 response details
throw new TpayException('Unable to create transaction. Response: '.json_encode($transaction));
}
}
protected function getRequestBody()
{
return [
'amount' => 0.10,
'description' => 'test transaction',
'hiddenDescription' => 'order_213',
'payer' => [
'email' => 'customer@example.com',
'name' => 'John Doe',
],
'callbacks' => [
'notification' => [
'url' => 'https://example.com/notification',
],
'payerUrls' => [
'success' => 'https://example.com/success',
'error' => 'https://example.com/error',
],
],
'pay' => [
'groupId' => 150,
],
];
}
}
new BlikPayment();