-
Notifications
You must be signed in to change notification settings - Fork 4
/
Response.php
executable file
·125 lines (111 loc) · 4.02 KB
/
Response.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
<?php
namespace Gajus\Strading;
/**
* @link https://github.com/gajus/strading for the canonical source repository
* @license https://github.com/gajus/strading/blob/master/LICENSE BSD 3-Clause
*/
class Response {
private
/**
* @var SimpleXMLElement
*/
$xml,
/**
* @var string
*/
$type,
/**
* @var array
*/
$transaction,
/**
* @var array
*/
$error,
/**
* @var string
*/
$redirect_url;
/**
* @param SimpleXMLElement $xml Response body.
* @param Request $request Request that produced the response.
*/
public function __construct (\SimpleXMLElement $xml, Request $request) {
$this->xml = $xml;
// PHP 5.3 does not allow array access to the method response.
$response = $this->xml->xpath('/responseblock/response');
$response = $response[0];
$this->type = $response->attributes();
$this->type = (string) $this->type['type'];
$request_block = $this->xml->xpath('/responseblock');
$this->transaction['request_reference'] = (string) $request_block[0]->requestreference;
$this->transaction['transaction_type'] = empty($response->billing->payment['type']) ? null : (string) $response->billing->payment['type'];
$this->transaction['transaction_reference'] = empty($response->transactionreference) ? null : (string) $response->transactionreference;
$this->transaction['timestamp'] = empty($response->timestamp) ? null : (string) $response->timestamp;
$this->transaction['parent_transaction_reference'] = empty($response->operation->parenttransactionreference) ? null : (string) $response->operation->parenttransactionreference;
$this->transaction['authcode'] = empty($response->authcode) ? null : (string) $response->authcode;
$this->transaction['amount'] = empty($response->billing->amount) ? null : (string) $response->billing->amount/100;
$this->transaction['paypal_token'] = empty($response->paypal->token) ? null : (string) $response->paypal->token;
if ($this->getType() === 'ERROR') {
$this->error = array(
'code' => (string) $response->error->code,
'message' => empty($response->error->message) ? null : (string) $response->error->message,
'data' => empty($response->error->data) ? null : (string) $response->error->data
);
}
if (!empty($response->paypal->redirecturl)) {
$this->redirect_url = (string) $response->paypal->redirecturl;
}
}
/**
* Transaction abstracts access to the most generic information about the response:
*
* - request_reference
* - transaction_type
* - transaction_reference
* - timestamp
* - parent_transaction_reference
* - authcode
* - amount
* - paypal_token
*
* Presence of this data will depend on the type of the response you receive, e.g.
* only PayPal order request will include "paypal_token" parameter.
*
* @return array
*/
public function getTransaction () {
return $this->transaction;
}
/**
* This information is available when response type is "ERROR".
*
* @return null|Gajus\Strading\Error
*/
public function getError () {
if ($this->getType() !== 'ERROR') {
return;
}
return new Error($this->error['code'], $this->error['message'], $this->error['data']);
}
/**
* This information is available in response to the "paypal/order" request.
*
* @return null|string URL to redirect the client to.
*/
public function getRedirectUrl () {
return $this->redirect_url;
}
/**
* @return string Response type.
*/
public function getType () {
return $this->type;
}
/**
* @return string Raw XML response.
*/
public function getXML () {
return $this->xml->asXML();
}
}