This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiqPayCheckoutUrlCreator.php
136 lines (108 loc) · 3.28 KB
/
LiqPayCheckoutUrlCreator.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
<?
class LiqPayCheckoutUrlCreator {
/**
* @param array $apiParams
* $apiParams = [
* public_key => string,
* private_key => string
* ]
*
* @throws \Exception
*/
static function create(array $apiParams, array $paymentParams) {
$self = new self($apiParams, $paymentParams);
return $self->doCreate();
}
/**
* @param array $apiParams
* $apiParams = [
* public_key => string,
* private_key => string
* ]
*/
private function __construct(array $apiParams, array $paymentParams) {
$this->apiParams = $apiParams;
$this->paymentParams = self::preparePaymentParams(
$apiParams,
$paymentParams
);
}
/**
* @throws \Exception
*/
private function doCreate() {
$response = $this->makeApiRequest();
return self::parseApiResponse($response);
}
private function makeApiRequest() {
$body = $this->createApiRequestBody();
return self::doMakeApiRequest($body);
}
private function createApiRequestBody() {
$data = $this->createData();
$signature = $this->createSignature($data);
$body = [
'data' => $data,
'signature' => $signature
];
return http_build_query($body);
}
private function createData() {
$paramsStr = json_encode($this->paymentParams);
return base64_encode($paramsStr);
}
/**
* @param string $data
*/
private function createSignature($data) {
$privateKey = $this->apiParams['private_key'];
$raw = $privateKey . $data . $privateKey;
return base64_encode(sha1($raw, 1));
}
/**
* @param string $response
* @return string
* @throws \Exception
*/
private static function parseApiResponse($response) {
preg_match(
'/Location: (https:.+)\b/',
$response,
$matches
);
$url = $matches[1];
if ($url)
return $url;
throw new \Exception('Failed to look for URL.');
}
/**
* @param string $body
* @return string
*/
private static function doMakeApiRequest($body) {
$url = 'https://www.liqpay.ua/api/3/checkout';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
private static function preparePaymentParams(array $apiParams, array $paymentParams) {
$shred = [
'public_key' => $apiParams['public_key']
];
return array_merge($shred, $paymentParams);
}
private $apiParams = [];
private $paymentParams = [];
}