-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickpay_helper.php
138 lines (115 loc) · 3.71 KB
/
quickpay_helper.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
137
138
<?php
/**
* Quickpay helper for accessing the Quickpay api.
*
*/
class QuickpayHelper {
protected $connTimeout = 10; // The connection timeout to Quickpay gateway
protected $apiUrl = "https://api.quickpay.net";
protected $apiVersion = 'v10';
protected $apiKey = ""; // Loaded from the configuration
protected $format = "application/json";
protected $synchronized = "?synchronized";
function setApiKey($key) {
$this->apiKey = $key;
}
/**
* Send a request to Quickpay.
*/
function request($resource, $postdata = null, $synchronized="?synchronized") {
if (!function_exists('curl_init')) {
throw Exception('CURL is not installed, please install curl');
}
$curl = curl_init();
$url = $this->apiUrl . "/" . $resource . $synchronized;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->connTimeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode(":" . $this->apiKey), 'Accept-Version: ' . $this->apiVersion, 'Accept: ' . $this->format));
if (!is_null($postdata)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postdata));
}
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($httpCode != 200 && $httpCode!= 201 && $httpCode != 202) {
throw new Exception($response, $httpCode);
}
return $response;
}
function put($resource, $postdata = null) {
$curl = curl_init();
$url = $this->apiUrl . "/" . $resource;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->connTimeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode(":" . $this->apiKey), 'Accept-Version: ' . $this->apiVersion, 'Accept: ' . $this->format));
if (!is_null($postdata)) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postdata));
}
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($httpCode != 200 && $httpCode != 201 && $httpCode != 202) {
throw new Exception($response, $httpCode);
}
return $response;
}
/**
* Create a payment at the quickpay gateway
*/
function qpCreatePayment($data) {
$result = $this->request('payments', $data,"");
$result = json_decode($result);
return $result;
}
/**
* Create a payment link at the quickpay gateway
*/
function qpCreatePaymentLink($id, $array) {
$result = $this->put('payments/' . $id . '/link', $array);
$result = json_decode($result);
return $result;
}
/**
* Capture a payment at the quickpay gateway
*/
function qpCapture($id, $amount, $extras = null) {
$postArray = array();
$postArray['id'] = $id;
$postArray['amount'] = $amount;
if (!is_null($extras)) {
$postArray['extras'] = $extras;
}
$result = $this->request('payments/' . $id . '/capture', $postArray);
$result = json_decode($result);
return $result;
}
/**
* Refund a payment at the quickpay gateway
*/
function qpRefund($id, $amount, $extras = null) {
$postArray = array();
$postArray['id'] = $id;
$postArray['amount'] = $amount;
if (!is_null($extras)) {
$postArray['extras'] = $extras;
}
$result = $this->request('payments/' . $id . '/refund', $postArray);
$result = json_decode($result);
return $result;
}
/**
* Cancel a payment at the quickpay gateway
*/
function qpCancel($id) {
$postArray = array();
$postArray['id'] = $id;
$result = $this->request('payments/' . $id . '/cancel', $postArray);
$result = json_decode($result);
return $result;
}
}
?>