-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPayvment.php
More file actions
192 lines (157 loc) · 5.45 KB
/
Payvment.php
File metadata and controls
192 lines (157 loc) · 5.45 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* This module implements the basic authentication methods needed
* to interface w/ Payvment's Application
*
* version: 1.0
*
*
* @author mjelks
*/
require_once('BasePayvment.php');
define('PRODUCTION_APPLICATION_ID', '<YOUR_APP_ID>');
define('PRODUCTION_APPLICATION_SECRET', '<YOUR_APP_SECRET>');
define('PRODUCTION_API_CALLBACK', 'https://api.payvment.com');
define('SANDBOX_APPLICATION_ID', '<YOUR_SANDBOX_APP_ID>');
define('SANDBOX_APPLICATION_SECRET', '<YOUR_SANDBOX_APP_SECRET>');
define('SANDBOX_API_CALLBACK', 'https://api-sandbox.payvment.com');
class Payvment extends BasePayvment {
private $_request;
private $_sandbox;
protected $_callbackUrl;
protected $_applicationId;
protected $_redirectUrl;
protected $_payvmentId;
protected $_payvmentToken;
public function __set($name, $value)
{
$this->{$name} = $value;
}
public function __construct($request=false)
{
// we pass in the request varible to allow for dependency injection
// http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass
$this->_request = (!empty($request)) ? (object) $request : (object) $_REQUEST;
$this->_sandbox = (isset($this->_request->sandbox)) ? true : false;
$this->_callbackUrl = ($this->_sandbox) ? SANDBOX_API_CALLBACK : PRODUCTION_API_CALLBACK;
$this->_applicationId = ($this->_sandbox) ? SANDBOX_APPLICATION_ID : PRODUCTION_APPLICATION_ID;
$this->_applicationSecret = ($this->_sandbox) ? SANDBOX_APPLICATION_SECRET : PRODUCTION_APPLICATION_SECRET;
}
public function generateAuthorizationUrl($redirect=true)
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$this->_redirectUrl .= ($this->_sandbox) ? '?sandbox=1' : '';
$authorizeUrl =
$this->_callbackUrl .
"/oauth/authorize?" .
"client_id={$this->_applicationId}&" .
"redirect_uri=" . urlencode($this->_redirectUrl) . "&" .
"state=" . $_SESSION['state'];
// @codeCoverageIgnoreStart
if ($redirect) {
header('Location: ' . $authorizeUrl);
}
// @codeCoverageIgnoreEnd
else {
return $authorizeUrl;
}
// @codeCoverageIgnoreStart
}
// @codeCoverageIgnoreEnd
public function generateTokenUrl()
{
$tokenUrl =
$this->_callbackUrl . "/oauth/accesstoken?" .
"client_id={$this->_applicationId}" .
"&client_secret={$this->_applicationSecret}" .
"&code=" . $this->_request->code;
return $tokenUrl;
}
/**
* Passing in a url or file resource,
* return the xml document
* NOTE: simplexml_load_file returns false if invalid or no xml
*
* @param string $url
* @return mixed (boolean/xml)
*/
public function getXml($url)
{
return simplexml_load_file($url);
}
public function isUserAuthenticated()
{
$authenticated = false;
if (
isset($this->_payvmentId) && is_int($this->_payvmentId) &&
isset($this->_payvmentToken) && !empty($this->_payvmentToken)
)
{
$authenticated = true;
}
return $authenticated;
}
public function generateToken()
{
if($this->_request->state == $_SESSION['state'])
{
//Make request for access token and Payvment ID
$xml = $this->getXml($this->generateTokenUrl());
if (isset($xml->payvment_userid) && isset($xml->token)) {
$this->setPayvmentId($xml->payvment_userid); //Store Payvment ID to your DB
$this->setPayvmentToken($xml->token); //Store access token to your DB
} else {
throw new Exception('Token and/or xml document not returned.');
}
}
else
{
throw new Exception('The state does not match. You may be a victim of CSRF.');
}
return true;
}
/* Payvment API Support */
/**
* This is the REST call for Payvment's orders API
* the default command will pull all orders for a given retailer
*
* @param string $command
* @return string $url
*
*/
public function getOrdersUrl($params="")
{
$url = $this->_callbackUrl . "/rest/orders/?access_token=" .
$this->_payvmentToken;
if (!empty($params)) {
foreach ($params as $key => $val) {
$url .= "&" . urlencode($key) . '=' . urlencode($val);
}
}
return $url;
}
/**
*
* Return all orders for a given retailer --
*
* @param string $format
* @return string $result
*/
public function orders($params=false, $format='xml')
{
$result = false;
// default command is pullOrders (pull all orders from Payvment)
if (!$params) {
$params = array('command' => 'pullOrders');
}
switch ($format) {
case 'xml':
$result = $this->getXml($this->getOrdersUrl($params));
break;
default:
$result = 'Invalid format passed.';
break;
}
return $result;
}
}