Test Failed
Push — develop ( 292f49...be266a )
by Remco
06:18
created

Client::send_request()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.5039

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 7
b 0
f 0
nc 4
nop 2
dl 0
loc 47
ccs 13
cts 19
cp 0.6842
crap 4.5039
rs 9.52
1
<?php
2
/**
3
 * Adyen client
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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 Pronamic\WordPress\Pay\Facades\Http;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Facades\Http 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...
14
15
/**
16
 * Adyen client
17
 *
18
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.5
22
 * @since   1.0.0
23
 */
24
class Client {
25
	/**
26
	 * Config.
27
	 *
28
	 * @var Config
29
	 */
30
	private $config;
31
32
	/**
33
	 * Constructs and initializes an Adyen client object.
34
	 *
35 13
	 * @param Config $config Adyen config.
36 13
	 */
37 13
	public function __construct( Config $config ) {
38
		$this->config = $config;
39
	}
40
41
	/**
42
	 * Send request with the specified action and parameters
43
	 *
44
	 * @param string  $method  Adyen API method.
45
	 * @param Request $request Request object.
46
	 * @return object
47 11
	 * @throws \Exception Throws exception when error occurs.
48
	 */
49 11
	private function send_request( $method, $request ) {
50
		// Request.
51 10
		$url = $this->config->get_api_url( $method );
52 10
53
		$response = Http::request(
54 10
			$url,
55
			array(
56 10
				'method'  => 'POST',
57 10
				'headers' => array(
58
					'X-API-key'    => $this->config->get_api_key(),
59 10
					'Content-Type' => 'application/json',
60
				),
61
				'body'    => \wp_json_encode( $request->get_json() ),
62
			)
63 10
		);
64
65
		$data = $response->json();
66
67
		// Object.
68 10
		if ( ! \is_object( $data ) ) {
69
			throw new \Exception(
70
				\sprintf(
71 10
					'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".',
72 10
					$response_code,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response_code does not exist. Did you maybe mean $response?
Loading history...
73
					$response_message,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response_message does not exist. Did you maybe mean $response?
Loading history...
74
					$body
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $body seems to be never defined.
Loading history...
75
				),
76
				\intval( $response_code )
77
			);
78
		}
79
80
		// Error.
81
		if ( isset( $data->error ) ) {
82
			$error = Error::from_object( $data->error );
83 10
84
			throw $error;
85
		}
86
87
		// Service Exception.
88
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
89
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
90
			$service_exception = ServiceException::from_object( $data );
91
92
			throw $service_exception;
93
		}
94 10
95
		return $data;
96
	}
97 10
98
	/**
99 10
	 * Create payment.
100
	 *
101
	 * @param PaymentRequest $request Payment request.
102
	 *
103
	 * @return PaymentResponse
104
	 *
105
	 * @throws \Exception Throws error if request fails.
106
	 */
107
	public function create_payment( PaymentRequest $request ) {
108
		$data = $this->send_request( 'v51/payments', $request );
109
110
		return PaymentResponse::from_object( $data );
111
	}
112
113 10
	/**
114
	 * Submit additional payment details.
115
	 *
116
	 * @param PaymentDetailsRequest $request Payment request.
117
	 *
118
	 * @return PaymentResponse
119
	 *
120
	 * @throws \Exception Throws error if request fails.
121
	 */
122
	public function request_payment_details( PaymentDetailsRequest $request ) {
123
		$data = $this->send_request( 'v51/payments/details', $request );
124
125
		return PaymentResponse::from_object( $data );
126 10
	}
127 1
128
	/**
129 1
	 * Create payment session.
130
	 *
131
	 * @param PaymentSessionRequest $request Payment session request.
132
	 *
133
	 * @return PaymentSessionResponse
134 9
	 *
135 5
	 * @throws \Exception Throws error if request fails.
136
	 */
137 5
	public function create_payment_session( PaymentSessionRequest $request ) {
138
		$data = $this->send_request( 'v41/paymentSession', $request );
139
140 4
		return PaymentSessionResponse::from_object( $data );
141
	}
142
143
	/**
144
	 * Get payment result.
145
	 *
146
	 * @param PaymentResultRequest $request Payment result request.
147
	 *
148
	 * @return PaymentResultResponse
149
	 *
150
	 * @throws \Exception Throws error if request fails.
151
	 */
152 1
	public function get_payment_result( PaymentResultRequest $request ) {
153 1
		$data = $this->send_request( 'v41/payments/result', $request );
154
155 1
		return PaymentResultResponse::from_object( $data );
156
	}
157
158
	/**
159
	 * Get payment methods.
160
	 *
161
	 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/paymentMethods
162
	 * @link https://docs.adyen.com/checkout/drop-in-web#step-1-get-available-payment-methods
163
	 *
164
	 * @param PaymentMethodsRequest $request Payment methods request.
165
	 *
166
	 * @return PaymentMethodsResponse
167
	 * @throws \Exception Throws error if request fails.
168
	 */
169
	public function get_payment_methods( PaymentMethodsRequest $request ) {
170
		$data = $this->send_request( 'v51/paymentMethods', $request );
171
172
		return PaymentMethodsResponse::from_object( $data );
173
	}
174
}
175