-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenzonaApi.php
146 lines (132 loc) · 4 KB
/
enzonaApi.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
139
140
141
142
143
144
145
146
<?php
/**
* Created by PhpStorm.
* User: Luis
* Date: 3/8/2020
* Time: 2:28 PM
*/
const SANDBOX = 1;
const PODUCTION = 2;
const SCOPE_PAYMENT = 'enzona_business_payment';
const SCOPE_QR = 'enzona_business_qr';
class enzonaApi {
protected $client_key, $client_secret, $stage, $access_token;
function __construct($client_key, $client_secret, $stage = SANDBOX) {
$this->client_key = $client_key;
$this->client_secret = $client_secret;
$this->stage = $stage;
}
public function getAccessToken() {
return $this->access_token;
}
public function requestAccessToken() {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.enzona.net/token",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials&scope=enzona_business_payment,enzona_business_qr",
CURLOPT_HTTPHEADER => [
"authorization: Basic ",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return ["access_token" => "Error", "message" => $err];
}
else {
$ob = json_encode($response);
$this->access_token = $ob->access_token;
return $response;
}
}
public function generatePayment($token, $total, $descripcion, $items, $return_url,$cancel_url) {
$curl = curl_init();
$values = [
"description" => $descripcion,
"currency" => "CUP",
"amount" => [
"total" => number_format($total,2),
"details" => [
"shipping" => number_format(0, 2),
"tax" => number_format(0, 2),
"discount" => number_format(0, 2),
"tip" => number_format(0, 2),
],
],
"items" => $items,
"merchant_op_id" => 123456789123,
"invoice_number" => 1212,
"return_url" => $return_url,
"cancel_url" => $cancel_url,
"terminal_id" => 12121,
];
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.enzona.net/payment/v1.0.0/payments",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($values),
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authorization: Bearer $token",
"cache-control: no-cache",
"content-type: application/json",
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return ["status" => "error", "message" => $err];
}
else {
$obj = json_decode($response);
if(isset($obj->transaction_uuid)){
return ["status" => "ok", "message" => $response];
}
else{
return ["status" => "error", "message" => $err];
}
}
}
public function acceptPayment($token, $transaction_uuid) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.enzona.net/payment/v1.0.0/payments/$transaction_uuid/complete",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{}",
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authorization: Bearer $token",
"cache-control: no-cache",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return ["status" => "error", "message" => $err];
}
else {
return ["status" => "ok", "message" => $response];
}
}
}