Test Failed
Push — develop ( 873315...27299e )
by Reüel
08:40
created

Client::get_payment_methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Adyen client
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Exception;
14
use WP_Error;
15
16
/**
17
 * Adyen client
18
 *
19
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
class Client {
26
	/**
27
	 * Config.
28
	 *
29
	 * @var Config
30
	 */
31
	private $config;
32
33
	/**
34
	 * Constructs and initializes an Adyen client object.
35
	 *
36
	 * @param Config $config Adyen config.
37
	 */
38 11
	public function __construct( Config $config ) {
39 11
		$this->config = $config;
40 11
	}
41
42
	/**
43
	 * Send request with the specified action and parameters
44
	 *
45
	 * @param string  $method  Adyen API method.
46
	 * @param Request $request Request object.
47
	 * @return object
48
	 * @throws Exception Throws exception when error occurs.
49
	 */
50 10
	private function send_request( $method, $request ) {
51
		// Request.
52 10
		$url = $this->config->get_api_url( $method );
53
54 9
		$response = wp_remote_request(
55 9
			$url,
56
			array(
57 9
				'method'  => 'POST',
58
				'headers' => array(
59 9
					'X-API-key'    => $this->config->get_api_key(),
60 9
					'Content-Type' => 'application/json',
61
				),
62 9
				'body'    => wp_json_encode( $request->get_json() ),
63
			)
64
		);
65
66 9
		if ( $response instanceof WP_Error ) {
67
			throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $response->get_error_message() );
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\GatewayException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
		}
69
70
		// Body.
71 9
		$body = wp_remote_retrieve_body( $response );
72
73 9
		$data = json_decode( $body );
74
75
		// JSON error.
76 9
		$json_error = json_last_error();
77
78 9
		if ( JSON_ERROR_NONE !== $json_error ) {
79 1
			throw new \Pronamic\WordPress\Pay\GatewayException(
80 1
				'adyen',
81
				sprintf( 'JSON: %s', json_last_error_msg() ),
82
				$json_error
83
			);
84
		}
85
86 8
		// Object.
87 1
		if ( ! is_object( $data ) ) {
88
			$code = wp_remote_retrieve_response_code( $response );
89 1
90 1
			throw new \Pronamic\WordPress\Pay\GatewayException(
91 1
				'adyen',
92
				sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ),
93
				intval( $code )
94
			);
95
		}
96 7
97 2
		// Error.
98
		if ( isset( $data->error ) ) {
99 2
			$error = Error::from_object( $data->error );
100
101
			throw $error;
102
		}
103
104 5
		// Service Exception.
105 1
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
106
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
107 1
			$service_exception = ServiceException::from_object( $data );
108
109
			throw $service_exception;
110 4
		}
111
112
		return $data;
113
	}
114
115
	/**
116
	 * Create payment.
117
	 *
118
	 * @param PaymentRequest $request Payment request.
119
	 *
120
	 * @return PaymentResponse
121
	 *
122 1
	 * @throws Exception Throws error if request fails.
123 1
	 */
124
	public function create_payment( PaymentRequest $request ) {
125 1
		$data = $this->send_request( 'payments', $request );
126
127
		return PaymentResponse::from_object( $data );
128
	}
129
130
	/**
131
	 * Create payment session.
132
	 *
133
	 * @param PaymentSessionRequest $request Payment session request.
134
	 *
135
	 * @return PaymentSessionResponse
136
	 *
137 1
	 * @throws Exception Throws error if request fails.
138 1
	 */
139
	public function create_payment_session( PaymentSessionRequest $request ) {
140 1
		$data = $this->send_request( 'paymentSession', $request );
141
142
		return PaymentSessionResponse::from_object( $data );
143
	}
144
145
	/**
146
	 * Get payment result.
147
	 *
148
	 * @param PaymentResultRequest $request Payment result request.
149
	 *
150
	 * @return PaymentResultResponse
151
	 *
152 2
	 * @throws Exception Throws error if request fails.
153 2
	 */
154
	public function get_payment_result( PaymentResultRequest $request ) {
155 1
		$data = $this->send_request( 'payments/result', $request );
156
157
		return PaymentResultResponse::from_object( $data );
158
	}
159
160
	/**
161
	 * Get payment methods.
162
	 *
163
	 * @return PaymentMethodsResponse
164
	 *
165 6
	 * @throws Exception Throws error if request fails.
166 6
	 */
167
	public function get_payment_methods() {
168 6
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
169
170 1
		$data = $this->send_request( 'paymentMethods', $request );
171
172
		return PaymentMethodsResponse::from_object( $data );
173
	}
174
}
175