· 6 years ago · Nov 21, 2019, 11:50 PM
1<?php
2require "lib/paylike/init.php";
3require "paylike_config.php";
4
5//add card to the server
6function add_card($public_key, $card, $exp_m, $exp_y, $cvc)
7{
8
9 $url = "https://gateway.paylike.io/cards";
10
11 $post = array(
12 "key" => $public_key,
13 "number" => $card,
14 "expiry[month]" => $exp_m,
15 "expiry[year]" => $exp_y,
16 "code" => $cvc,
17 );
18
19 $ch = curl_init($url);
20 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
21 curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
22
23 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
24 $result = curl_exec($ch);
25
26 curl_close($ch);
27
28 $data = json_decode($result, true);
29
30 if (!empty($data["card"]["id"])) {
31 return array("status" => "true", "card_id" => $data["card"]["id"]);
32 } else {
33 if(empty($data["message"]))
34 {
35 $error = "Cardul este invalid sau a aparut o eroare la adaugarea cardului.";
36 }
37 else
38 {
39 $error = $data["message"]. "<br> A aparut o eroare la adaugarea cardului.";
40 }
41 return array("status" => "false", "error" => $error);
42 }
43
44}
45//create transaction
46function create_transaction($merchant_id, $private_key, $card_id, $amount, $description,
47 $currency)
48{
49
50 $args = array(
51 "cardId" => $card_id,
52 "descriptor" => $description,
53 "currency" => $currency,
54 "amount" => $amount,
55 );
56
57 $paylike = new \Paylike\Paylike($private_key);
58 try {
59 $transactions = $paylike->transactions();
60 $data = $transactions->create($merchant_id, $args);
61 }
62 catch (\Paylike\Exception\NotFound $e) {
63 // The transaction was not found
64 }
65 catch (\Paylike\Exception\InvalidRequest $e) {
66 // Bad (invalid) request - see $e->getJsonBody() for the error
67 }
68 catch (\Paylike\Exception\Forbidden $e) {
69 // You are correctly authenticated but do not have access.
70 }
71 catch (\Paylike\Exception\Unauthorized $e) {
72 // You need to provide credentials (an app's API key)
73 }
74 catch (\Paylike\Exception\Conflict $e) {
75 // Everything you submitted was fine at the time of validation, but something changed in the meantime and came into conflict with this (e.g. double-capture).
76 }
77 catch (\Paylike\Exception\ApiConnection $e) {
78 // Network error on connecting via cURL
79 }
80 catch (\Paylike\Exception\ApiException $e) {
81 // Unknown api error
82 }
83 if (!empty($e)) {
84 $error_body = json_decode($e->http_body, true);
85 }
86
87
88 if(is_null($error_body))
89 {
90 $display_error = "Credit card might be invalid , or might be declined by your bank. Please add a VALID card.";
91 }
92 else
93 {
94 $display_error = $error_body;
95 }
96
97
98 if (!empty($data)) {
99 return array("status" => "true", "transaction_id" => $data);
100 } else {
101 return array("status" => "false", "error" => $display_error);
102 }
103
104
105}
106//get transaction details
107function get_transaction($transaction_id, $private_key)
108{
109 $paylike = new \Paylike\Paylike($private_key);
110 try {
111 $transactions = $paylike->transactions();
112 $data = $transactions->fetch($transaction_id);
113 }
114 catch (\Paylike\Exception\NotFound $e) {
115 // The transaction was not found
116 }
117 catch (\Paylike\Exception\InvalidRequest $e) {
118 // Bad (invalid) request - see $e->getJsonBody() for the error
119 }
120 catch (\Paylike\Exception\Forbidden $e) {
121 // You are correctly authenticated but do not have access.
122 }
123 catch (\Paylike\Exception\Unauthorized $e) {
124 // You need to provide credentials (an app's API key)
125 }
126 catch (\Paylike\Exception\Conflict $e) {
127 // Everything you submitted was fine at the time of validation, but something changed in the meantime and came into conflict with this (e.g. double-capture).
128 }
129 catch (\Paylike\Exception\ApiConnection $e) {
130 // Network error on connecting via cURL
131 }
132 catch (\Paylike\Exception\ApiException $e) {
133 // Unknown api error
134 }
135 if (!empty($e)) {
136 $error_body = json_decode($e->http_body, true);
137 }
138 if (!empty($data)) {
139 return array(
140 "status" => "true",
141 "transaction_id" => $data["id"],
142 "transaction_status" => $data["successful"],
143 "transaction_error" => $data["error"],
144 "merchant_id" => $data["merchantId"],
145 "date" => $data["created"],
146 "captured_amount" => $data["capturedAmount"],
147 "pending_amount" => $data["pendingAmount"],
148 "card" => array(
149 "card_id" => $data["card"]["id"],
150 ),
151 "description" => $data["descriptor"],
152 );
153 } else {
154 return array("status" => "false", "error" => $error_body[0]["message"]);
155 }
156
157
158}
159
160//capture transaction
161
162function capture_transaction($transaction_id,$amount,$description,$private_key)
163{
164 $args = array
165 (
166 "amount" => $amount,
167 "descriptor" => $description,
168 );
169
170
171 $paylike = new \Paylike\Paylike($private_key);
172 try {
173 $transactions = $paylike->transactions();
174 $data = $transactions->capture($transaction_id,$args);
175 }
176 catch (\Paylike\Exception\NotFound $e) {
177 // The transaction was not found
178 }
179 catch (\Paylike\Exception\InvalidRequest $e) {
180 // Bad (invalid) request - see $e->getJsonBody() for the error
181 }
182 catch (\Paylike\Exception\Forbidden $e) {
183 // You are correctly authenticated but do not have access.
184 }
185 catch (\Paylike\Exception\Unauthorized $e) {
186 // You need to provide credentials (an app's API key)
187 }
188 catch (\Paylike\Exception\Conflict $e) {
189 // Everything you submitted was fine at the time of validation, but something changed in the meantime and came into conflict with this (e.g. double-capture).
190 }
191 catch (\Paylike\Exception\ApiConnection $e) {
192 // Network error on connecting via cURL
193 }
194 catch (\Paylike\Exception\ApiException $e) {
195 // Unknown api error
196 }
197 if (!empty($e)) {
198 $error_body = json_decode($e->http_body, true);
199 }
200 if (!empty($data)) {
201 return array(
202 "status" => "true",
203 "transaction_id" => $data["id"],
204 "transaction_status" => $data["successful"],
205 "transaction_error" => $data["error"],
206 "merchant_id" => $data["merchantId"],
207 "date" => $data["created"],
208 "captured_amount" => $data["capturedAmount"],
209 "pending_amount" => $data["pendingAmount"],
210 "card" => array(
211 "card_id" => $data["card"]["id"],
212 ),
213 "description" => $data["descriptor"],
214 );
215 } else {
216 return array("status" => "false", "error" => $error_body[0]["message"]);
217 }
218
219
220}
221
222
223//$amount = "15000";
224//$currency = "DKK";
225//$description = "TestCreateTransaction";
226//$card_id = "5daf44d9fd0c53603c7be1e4";
227//$transaction_id = "5daf5474ac5e8160473b5aa0";
228//examples of usage
229//$create = create_transaction($merchant_id,$private_key,$card_id,$amount,$description,$currency);
230//$get_transaction = get_transaction($transaction_id, $private_key);
231//$capture = capture_transaction($transaction_id,$amount,$description,$private_key);
232
233?>