-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckoutRuApi.php
115 lines (99 loc) · 3.07 KB
/
CheckoutRuApi.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
<?php
namespace jisoft\checkoutru;
/**
* Checkout.ru API
* @author JiSoft <[email protected]>
*/
class CheckoutRuApi
{
protected $apiKey = false;
protected $ticket = false;
protected $baseUrl = 'http://platform.checkout.ru';
public function __construct($apiKey,$baseUrl='',$ticket='')
{
$this->apiKey = $apiKey;
if (!empty($ticket))
$this->ticket = $ticket;
if (!empty($baseUrl))
$this->baseUrl = $baseUrl;
}
public function getTicket()
{
if ($this->ticket != false)
return $this->ticket;
$url = $this->baseUrl.'/service/login/ticket/'.$this->apiKey;
$response = $this->send($url);
if (isset($response['ticket']) && !empty($response['ticket'])) {
$this->ticket = $response['ticket'];
return $response['ticket'];
} else {
return false;
}
}
public function setTicket($ticket)
{
$this->ticket = $ticket;
}
public function getBaseUrl()
{
return $this->baseUrl;
}
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}
public function getApiKey()
{
return $this->apiKey;
}
public function getPlacesByQuery($text)
{
$url = $this->baseUrl.'/service/checkout/getPlacesByQuery/?ticket='.$this->ticket.'&place='.$text;
$response = $this->send($url);
if (isset($response['suggestions']) && !empty($response['suggestions'])) {
return $response['suggestions'];
} else {
return false;
}
}
public function createOrder($data)
{
$url = $this->baseUrl.'/service/order/create';
$response = $this->send($url,'post',$data);
if (isset($response['order']) && !empty($response['order'])) {
return $response;
} else {
return false;
}
return $response;
}
protected function send($url,$method='get',$params=[])
{
if ($method=='get' && empty($this->ticket) && substr_count($url,'/service/login/ticket/')==0)
return false;
if ($method=='post') {
if (empty($this->apiKey))
return false;
if (!isset($params['apiKey']))
$params['apiKey'] = $this->apiKey;
}
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
if ($method=='get') {
curl_setopt($curl,CURLOPT_VERBOSE,0);
curl_setopt($curl,CURLOPT_HEADER,0);
}
if ($method=='post'){
curl_setopt($curl, CURLOPT_POST,1);
if (count($params)>0 && is_array($params)) {
$datajson = json_encode($params);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datajson);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
}
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$curlData=curl_exec($curl);
curl_close($curl);
return(json_decode($curlData,true));
}
}