· 7 years ago · Jan 19, 2019, 10:48 AM
1<?php
2/**
3 * Created by PhpStorm.
4 * User: pawel.rybus
5 * Date: 22.08.2018
6 * Time: 23:30
7 */
8
9class FreshPaySDK
10{
11
12 protected $baseUrl = 'https://test.freshpay.org/';
13 protected $testUrl = 'https://test.freshpay.org/';
14
15 private $merchant_id;
16 private $secret_key;
17
18 public function __construct()
19 {
20 $this->merchant_id = Configuration::get('FRESHPAY_MERCHANT_ID');
21 $this->secret_key = Configuration::get('FRESHPAY_SECRET_KEY');
22 }
23
24 public function getUrl() {
25 if(Configuration::get('FRESHPAY_TEST_MODE')) {
26 return $this->testUrl;
27 }
28 return $this->baseUrl;
29 }
30
31 public function initializePayment($data) {
32 return $this->sendRequest('POST','payments/initialize',$data);
33 }
34
35 public function login($merchant_id,$secret_key) {
36 return $this->sendRequest('POST','login',[
37 'merchant_id' => $merchant_id,
38 'secret_key' => $secret_key
39 ]);
40 }
41
42 public function cryptoCurrency($data) {
43 return $this->sendRequest('GET','currency',$data);
44 }
45
46 protected function sendRequest($method, $path, $params = null)
47 {
48 // do the actual connection
49 $curl = curl_init();
50
51 // set URL
52 $url = $this->getUrl()."api/".$path;
53 // set api key
54
55 $headers[] = 'Content-Type:application/json';
56
57 if (isset($params['token'])) {
58 $headers[] = 'Authorization:'.$params['token'];
59 }
60
61 unset($params['token']);
62
63 switch ($method) {
64 case 'GET':
65 curl_setopt($curl, CURLOPT_HTTPGET, true);
66 $url = $url."?".http_build_query($params);
67 break;
68 case 'POST':
69 curl_setopt($curl, CURLOPT_POST, true);
70 $headers[] = 'Expect:';
71 break;
72 case 'PUT':
73 if(empty($params))
74 {
75 $headers[] = 'Content-Length: 0';
76 }
77 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
78 break;
79 default:
80 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
81 }
82
83
84 curl_setopt($curl, CURLOPT_URL, $url);
85 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
86
87 // add params
88 if ($method != 'GET') {
89 if (!empty($params))
90 {
91 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
92 }
93 }
94
95
96 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
97
98
99 $response = curl_exec($curl);
100 $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
101//dump($http_code);
102//echo $response;
103 if(curl_exec($curl) === false)
104 {
105 echo 'Curl error: ' . curl_error($curl);
106 }
107
108 // close the connection
109 curl_close($curl);
110
111 // check return format
112
113 $response = json_decode($response);
114
115 return $response;
116 }
117}