-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.php
More file actions
84 lines (60 loc) · 1.72 KB
/
client.php
File metadata and controls
84 lines (60 loc) · 1.72 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
<?php
class Paylike {
private $key;
// subsystems
private $transactions;
public function __construct( $key ){
$this->key = $key;
}
public function setKey( $key ){
$this->key = $key;
}
public function getKey(){
return $this->key;
}
public function __get( $name ){
switch ($name) {
case 'transactions':
if (!$this->transactions)
$this->transactions = new PaylikeTransactions($this);
return $this->transactions;
default:
throw new BadPropertyException($this, $name);
}
}
}
class PaylikeTransactions extends PaylikeSubsystem {
public function fetch( $transactionId ){
return $this->request('GET', '/transactions/'.$transactionId);
}
public function capture( $transactionId, $opts ){
return $this->request('POST', '/transactions/'.$transactionId.'/captures', $opts);
}
public function refund( $transactionId, $opts ){
return $this->request('POST', '/transactions/'.$transactionId.'/refunds', $opts);
}
}
class PaylikeSubsystem {
private $paylike;
public function __construct( $paylike ){
$this->paylike = $paylike;
}
protected function request( $verb, $path, $data = null ){
$c = curl_init();
curl_setopt($c, CURLOPT_URL, 'https://api.paylike.io'.$path);
if ($this->paylike->getKey() !== null)
curl_setopt($c, CURLOPT_USERPWD, ':'.$this->paylike->getKey());
if (in_array($verb, [ 'POST', 'PUT', 'PATCH' ]))
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
if (in_array($verb, [ 'GET', 'POST' ]))
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$raw = curl_exec($c);
$code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
if ($code < 200 || $code > 299)
return false;
if ($code === 204) // No Content
return true;
return json_decode($raw);
}
}