forked from paylike/php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapter.php
More file actions
87 lines (81 loc) · 2.73 KB
/
Adapter.php
File metadata and controls
87 lines (81 loc) · 2.73 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
<?php
namespace Paylike;
/**
* Class Adapter
* @package Paylike
* The adapter class taking care of the calls to the api.
*
* The purpose of this is to abstract the requests
* so that this can be changed depending on the environment.
*
* @version 1.0.0
*/
if ( ! class_exists( 'Paylike\\Adapter' ) ) {
class Adapter {
public $apiUrl = 'https://api.paylike.io';
private $apiKey;
/**
* Adapter constructor.
*
* @param $privateApiKey
*/
public function __construct( $privateApiKey ) {
if ( $privateApiKey ) {
$this->setApiKey( $privateApiKey );
} else {
trigger_error( 'Private Key is missing!', E_USER_ERROR );
return null;
}
}
/**
* @param $key
* set the api key.
*/
public function setApiKey( $key ) {
$this->apiKey = $key;
}
/**
* @param $url this is required, do not use the full url,
* only prepend the params eg: transactions/' . $transactionId . '/captures'
* @param $data this is optional
* Actual call to the api via curl.
*
* @return bool|mixed
*/
public function request( $url, $data = null, $httpVerb = 'post' ) {
$url = $this->apiUrl . '/' . $url;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json'
) );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2 );
curl_setopt( $ch, CURLOPT_USERPWD, ":" . $this->apiKey );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
switch ( $httpVerb ) {
case 'post':
curl_setopt( $ch, CURLOPT_POST, true );
if ( $data ) {
$encoded = json_encode($data);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $encoded );
}
break;
case 'get':
// can add args here for future use
break;
}
$result = curl_exec( $ch );
$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
$output = json_decode( $result, true );
if ( $httpCode >= 200 || $httpCode <= 299 ) {
return $output;
} else {
return false;
}
}
}
}