forked from appbootup/cashfree-payout-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfpayout.inc.php
142 lines (126 loc) · 4.36 KB
/
cfpayout.inc.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
<?php
class CfPayout
{
protected $token;
protected $baseUrl;
public function __construct($authParams) {
if(!empty($authParams))
{
$clientId = $authParams["clientId"];
$clientSecret = $authParams["clientSecret"];
$stage = $authParams["stage"];
if ($stage == "PROD") {
$this->baseUrl = "https://payout-api.cashfree.com/payout/v1";
} else {
$this->baseUrl = "https://payout-gamma.cashfree.com/payout/v1";
}
$headers = [
"X-Client-Id: $clientId",
"X-Client-Secret: $clientSecret"
];
$endpoint = $this->baseUrl."/authorize";
$curlResponse = $this->postCurl($endpoint, $headers);
if ($curlResponse) {
if ($curlResponse["status"] == "SUCCESS") {
$this->token = $curlResponse["data"]["token"];
} else {
throw new Exception("Authorization failed. Reason : ". $curlResponse["message"]);
}
}
}
}
public function addBeneficiary ($beneficiary) {
$response =["status" => "FAILED", "message" => "Authorization failed"];
if ($this->token) {
$endpoint = $this->baseUrl."/addBeneficiary";
$authToken = $this->token;
$headers = [
"Authorization: Bearer $authToken"
];
$curlResponse = $this->postCurl($endpoint, $headers, $beneficiary);
return $curlResponse;
}
return $response;
}
public function removeBeneficiary ($beneId) {
$response =["status" => "FAILED", "message" => "Authorization failed"];
if ($this->token) {
$params = [];
$params["beneId"] = $beneId;
$endpoint = $this->baseUrl."/removeBeneficiary";
$authToken = $this->token;
$headers = [
"Authorization: Bearer $authToken"
];
$curlResponse = $this->postCurl($endpoint, $headers, $params);
return $curlResponse;
}
return $response;
}
public function requestTransfer ($transfer) {
$response =["status" => "FAILED", "message" => "Authorization failed"];
if ($this->token) {
$endpoint = $this->baseUrl."/requestTransfer";
$authToken = $this->token;
$headers = [
"Authorization: Bearer $authToken"
];
$curlResponse = $this->postCurl($endpoint, $headers, $transfer);
return $curlResponse;
}
return $response;
}
public function getBalance () {
$balance =["ledger" => -1, "available" => -1];
if ($this->token) {
$endpoint = $this->baseUrl."/getBalance";
$authToken = $this->token;
$headers = [
"Authorization: Bearer $authToken"
];
$curlResponse = $this->getCurl($endpoint, $headers);
if ($curlResponse["status"] == "SUCCESS") {
$balance["ledger"] = $curlResponse["data"]["balance"];
$balance["available"] = $curlResponse["data"]["availableBalance"];
}
}
return $balance;
}
protected function postCurl ($endpoint, $headers, $params = []) {
$postFields = json_encode($params);
array_push($headers,
'Content-Type: application/json',
'Content-Length: ' . strlen($postFields));
$endpoint = $endpoint."?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$returnData = curl_exec($ch);
curl_close($ch);
if ($returnData != "") {
return json_decode($returnData, true);
}
return NULL;
}
protected function getCurl ($endpoint, $headers) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$returnData = curl_exec($ch);
curl_close($ch);
if ($returnData != "") {
return json_decode($returnData, true);
}
return NULL;
}
function __destruct()
{
$this->token = NULL;
}
}
?>