-
Notifications
You must be signed in to change notification settings - Fork 1
/
Safecharge.php
162 lines (139 loc) · 5.24 KB
/
Safecharge.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* PHP 5
*
* @package SafeCharge
*/
/**
* Load common includes
*/
require_once dirname(__FILE__) . '/libs/SafechargeCommon.php';
// Load other classes
require_once dirname(__FILE__) . '/libs/SafechargeRequest.php';
require_once dirname(__FILE__) . '/libs/SafechargeResponse.php';
/**
* SafeCharge Gateway API
*
* @package SafeCharge
* @author Leonid Mamchenkov <leonid@mamchenkov.net>
* @link http://www.safecharge.com
*/
class Safecharge {
// Shortcuts and external exposure
const REQUEST_TYPE_AUTH = SafechargeConstants::REQUEST_TYPE_AUTH;
const REQUEST_TYPE_SETTLE = SafechargeConstants::REQUEST_TYPE_SETTLE;
const REQUEST_TYPE_SALE = SafechargeConstants::REQUEST_TYPE_SALE;
const REQUEST_TYPE_CREDIT = SafechargeConstants::REQUEST_TYPE_CREDIT;
const REQUEST_TYPE_VOID = SafechargeConstants::REQUEST_TYPE_VOID;
const REQUEST_TYPE_AVS = SafechargeConstants::REQUEST_TYPE_AVS;
const RESPONSE_STATUS_APPROVED = SafechargeConstants::RESPONSE_STATUS_APPROVED;
const RESPONSE_STATUS_SUCCESS = SafechargeConstants::RESPONSE_STATUS_SUCCESS;
const RESPONSE_STATUS_DECLINED = SafechargeConstants::RESPONSE_STATUS_DECLINED;
const RESPONSE_STATUS_ERROR = SafechargeConstants::RESPONSE_STATUS_ERROR;
const RESPONSE_STATUS_PENDING = SafechargeConstants::RESPONSE_STATUS_PENDING;
/**
* Settings
*/
protected $settings = array();
/**
* Constructor
*
* Supported settings are:
*
* - username - SafeCharge username. Required for any queries to the gateway
* - password - SafeCharge password. Required for any queries to the gateway
* - timeout - Network operations timeout (in seconds)
* - live - Set to true for Live server, otherwise Test server will be used
* - log - Full path to the log file, if logging is necessary
* - padFrom - When masking credit card numbers in logs, leave so many starting digits
* - padTo - When masking credit card numbers in logs, leave so many ending digits
* - padWith - When masking credit card numbers in logs, use this character for masking
* - instanceId - Some ID of this object intance to keep related queries together
*
* @param array $settings Settings
*/
public function __construct($settings = array()) {
$defaultSettings = array(
'username' => SafechargeConstants::REQUEST_DEFAULT_USERNAME,
'password' => SafechargeConstants::REQUEST_DEFAULT_PASSWORD,
'timeout' => SafechargeConstants::REQUEST_DEFAULT_TIMEOUT,
'live' => SafechargeConstants::REQUEST_DEFAULT_LIVE,
'log' => '',
'padFrom' => SafechargeConstants::DEFAULT_PAD_FROM,
'padTo' => SafechargeConstants::DEFAULT_PAD_TO,
'padWith' => SafechargeConstants::DEFAULT_PAD_WITH,
'instanceId' => rand(1,100000),
);
$this->settings = array_merge($defaultSettings, $settings);
$this->log("Initialized");
}
/**
* Destructor
*/
public function __destruct() {
$this->log("Shutting down");
}
/**
* Send request to the gateway server
*
* @throws Exception
* @param string $type Type of query (Auth, Settle, etc)
* @param array $params Query params
* @return null|object Null on failure, or SafechargeResponse object on success
*/
public function request($type, $params) {
$result = null;
try {
$request = new SafechargeRequest($this->settings);
$queryId = $request->getId();
$this->log("$queryId Starting new [$type] query");
$request->setType($type);
$request->setParameters($params);
$queryUrl = $request->url;
$this->log("$queryId Sending query: $queryUrl");
$request = $request->send();
$this->log("$queryId Parsing response");
$response = new SafechargeResponse;
$result = $response->parse($request);
$this->log("$queryId Result: " . print_r($result, true));
}
catch (NetworkException $e) {
$this->log("$queryId Caught Network Exception: " . $e->getMessage());
throw new Exception("Gateway communications error. Please try again later.");
}
catch (InternalException $e) {
$this->log("$queryId Caught Internal Exception: " . $e->getMessage());
throw new Exception("Internal server error. Please try again later.");
}
catch (ResponseException $e) {
$this->log("$queryId Caught Response Exception: " . $e->getMessage());
throw new Exception("Internal server error. Please try again later.");
}
catch (ValidationException $e) {
$this->log("$queryId Caught Validation Exception: " . $e->getMessage());
throw new Exception("Validation error: " . $e->getMessage() . ". Please correct your data and try again.");
}
catch (CardNumberException $e) {
$this->log("$queryId Caught Card Number Exception: " . $e->getMessage());
throw new Exception("Credit card number is invalid. Please correct and try again.");
}
catch (Exception $e) {
$this->log("$queryId Caught Exception: " . $e->getMessage());
throw new Exception("Internal server error. Please try again later");
}
return $result;
}
/**
* Log message to a file if it was given in settings
*
* @param string $msg Message to log
*/
protected function log($msg) {
if (!empty($this->settings['log'])) {
$now = date('Y-m-d H:i:s');
$logMessage = sprintf("%s : [%s] : %s\n", $now, $this->settings['instanceId'], $msg);
file_put_contents($this->settings['log'], $logMessage, FILE_APPEND | LOCK_EX);
}
}
}
?>