PrivatController.php
3.97 KB
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
<?php
/**
* Created by PhpStorm.
* User: stes
* Date: 17.01.18
* Time: 11:45
*/
namespace frontend\controllers;
use GuzzleHttp\Client;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\Controller;
class PrivatController extends Controller
{
public function actionIndex(){
$st = '';
$privat[ 'products' ][ 0 ][ 'name' ] = 'test product';
$privat[ 'products' ][ 0 ][ 'count' ] = 1;
$privat[ 'products' ][ 0 ][ 'price' ] =100;
$st .= $privat[ 'products' ][ 0 ][ 'name' ] . $privat[ 'products' ][ 0 ][ 'count' ] . str_replace(
'.',
'',
$privat[ 'products' ][ 0 ][ 'price' ] . '.00'
);
$url = 'https://payparts2.privatbank.ua/ipp/v2/payment/create';
$password = '310a8890988e478eadb5dc1175ab9297';
$privat[ 'storeId' ] = '9F93C471A19F4DC4B5A1';
$privat[ 'amount' ] = 100;
$privat[ 'partsCount' ] = 10;
$privat[ 'merchantType' ] = 'PP';
$privat[ 'orderId' ] = date('Ymd-His');
$privat[ 'responseUrl' ] = Url::to('/ru/privat/callback', true);
$privat[ 'redirectUrl' ] = Url::to('/site/index', true);
$privat[ 'signature' ] = base64_encode(
sha1(
$password . $privat[ 'storeId' ] . $privat[ 'orderId' ] . str_replace(
".",
"",
$privat[ 'amount' ] . '.00'
) . $privat[ 'partsCount' ] . $privat[ 'merchantType' ] . $privat[ 'responseUrl' ] . $privat[ 'redirectUrl' ] . $st . $password,
true
)
);
$headers = [
'Accept' => 'application/json',
'Accept-Encoding' => 'UTF-8',
'Content-Type' => 'application/json;charset=utf-8',
];
$client = new Client();
$response = $client->post(
$url,
[
'headers' => $headers,
'body' => Json::encode($privat),
]
);
if ($response->getStatusCode() == 200) {
$str = (string) $response->getBody()
->getContents();
$data = Json::decode($str);
if ($data[ 'token' ]) {
header(
'Location: https://payparts2.privatbank.ua/ipp/v2/payment?token=' . $data[ 'token' ]
); // перенаправление на нужную страницу
exit();
}
}
}
public function beforeAction($action)
{
if ($action->id == 'callback') {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
public function actionCallback()
{
if ($_SERVER[ 'REQUEST_METHOD' ] === 'POST') {
$data = file_get_contents('php://input');
$file = \Yii::getAlias('@storage/callback.log');
$myFile = fopen($file, 'a') or die('Unable to open file!');
fwrite($myFile, "\n" . $data);
fclose($myFile);
$response = Json::decode($data);
//$order = OrderFrontend::findOne($response[ 'orderId' ]);
if ($response[ 'paymentState' ] === 'SUCCESS') {
print_r($response); die();
}
// print_r($data);
// die();
} else {
print_r(Url::to('/ru/order/callback', true));
print_r(\Yii::$app->session->get('data'));
}
}
}