Test Setup Failed
Push — develop ( 8cba91...785a27 )
by Remco
04:21
created

src/Client.php (1 issue)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\TargetPay;
4
5
use Pronamic\WordPress\Pay\Core\Util;
6
use Pronamic\WordPress\Pay\Core\XML\Security;
7
use stdClass;
8
9
/**
10
 * Title: TargetPay gateway
11
 * Description:
12
 * Copyright: 2005-2021 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.3
17
 * @since   1.0.0
18
 */
19
class Client {
20
	/**
21
	 * URL for issuers in Dutch language
22
	 *
23
	 * @var string
24
	 */
25
	const URL_ISSUERS_NL = 'https://www.targetpay.com/ideal/issuers-nl.js';
26
27
	/**
28
	 * URL for issuers in English language
29
	 *
30
	 * @var string
31
	 */
32
	const URL_ISSUERS_EN = 'https://www.targetpay.com/ideal/issuers-en.js';
33
34
	/**
35
	 * URL for retrieving issuers in HTL format
36
	 *
37
	 * @var string
38
	 */
39
	const URL_ISSUERS_HTML = 'https://www.targetpay.com/ideal/getissuers.php?format=html';
40
41
	/**
42
	 * URL for retrieving issuers in XML format
43
	 *
44
	 * @var string
45
	 */
46
	const URL_ISSUERS_XML = 'https://www.targetpay.com/ideal/getissuers.php?format=xml';
47
48
	/**
49
	 * URL to start an transaction
50
	 *
51
	 * @var string
52
	 */
53
	const URL_START_TRANSACTION = 'https://www.targetpay.com/ideal/start';
54
55
	/**
56
	 * URL to check an transaction
57
	 *
58
	 * @var string
59
	 */
60
	const URL_CHECK_TRANSACTION = 'https://www.targetpay.com/ideal/check';
61
62
	/**
63
	 * Token used by TargetPay to separate some values
64
	 *
65
	 * @var string
66
	 */
67
	const TOKEN = ' |';
68
69
	/**
70
	 * Status indicator for 'Ok'
71
	 *
72
	 * @var string
73
	 */
74
	const STATUS_OK = '000000';
75
76
	/**
77
	 * Status indicator for 'No layout code'
78
	 *
79
	 * @var string
80
	 */
81
	const STATUS_NO_LAYOUT_CODE = 'TP0001';
82
83
	/**
84
	 * Constructs and initializes an TargetPay client object
85
	 */
86
	public function __construct() {
87
88
	}
89
90
	/**
91
	 * Remote get.
92
	 *
93
	 * @param string $url URL for GET request.
94
	 *
95
	 * @return string
96
	 */
97
	private function remote_get( $url ) {
98
		return Util::remote_get_body( $url, 200 );
0 ignored issues
show
Bug Best Practice introduced by
The expression return Pronamic\WordPres...ote_get_body($url, 200) also could return the type WP_Error which is incompatible with the documented return type string.
Loading history...
99
	}
100
101
	/**
102
	 * Start transaction
103
	 *
104
	 * @param IDealStartParameters $parameters Start parameters.
105
	 *
106
	 * @return stdClass
107
	 */
108
	public function start_transaction( IDealStartParameters $parameters ) {
109
		$url = Util::build_url( self::URL_START_TRANSACTION, $parameters->get_array() );
110
111
		$data = self::remote_get( $url );
112
113
		if ( false !== $data ) {
114
			$status = strtok( $data, self::TOKEN );
115
116
			if ( self::STATUS_OK === $status ) {
117
				$result = new stdClass();
118
119
				$result->status         = $status;
120
				$result->transaction_id = strtok( self::TOKEN );
121
				$result->url            = strtok( self::TOKEN );
122
123
				return $result;
124
			} else {
125
				throw new Error( $data );
126
			}
127
		}
128
	}
129
130
	/**
131
	 * Check status
132
	 *
133
	 * @param string $rtlo
134
	 * @param string $transaction_id
135
	 * @param string $once
136
	 * @param string $test
137
	 *
138
	 * @return null|Status
139
	 */
140
	public function check_status( $rtlo, $transaction_id, $once, $test ) {
141
		$result = null;
142
143
		$url = Util::build_url(
144
			self::URL_CHECK_TRANSACTION,
145
			array(
146
				'rtlo'  => $rtlo,
147
				'trxid' => $transaction_id,
148
				'once'  => Util::boolean_to_numeric( $once ),
149
				'test'  => Util::boolean_to_numeric( $test ),
150
			)
151
		);
152
153
		$data = self::remote_get( $url );
154
155
		if ( false !== $data ) {
156
			$result = StatusStringParser::parse( $data );
157
		}
158
159
		return $result;
160
	}
161
162
	/**
163
	 * Get issuers
164
	 *
165
	 * @return array
166
	 */
167
	public function get_issuers() {
168
		$issuers = false;
169
170
		$url = self::URL_ISSUERS_XML;
171
172
		$data = self::remote_get( $url );
173
174
		if ( false !== $data ) {
175
			$xml = Util::simplexml_load_string( $data );
176
177
			$issuers = array();
178
179
			foreach ( $xml->issuer as $xml_issuer ) {
180
				$id   = Security::filter( $xml_issuer['id'] );
181
				$name = Security::filter( $xml_issuer );
182
183
				$issuers[ $id ] = $name;
184
			}
185
		}
186
187
		return $issuers;
188
	}
189
}
190