Issues (11)

src/Client.php (1 issue)

Labels
Severity
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
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", HTTP body: "%s".',
72 10
					$response->status(),
73
					$response->body()
74
				),
75
				\intval( $response->status() )
76
			);
77
		}
78
79
		// Error.
80
		if ( isset( $data->error ) ) {
81
			$error = Error::from_object( $data->error );
82
83 10
			throw $error;
84
		}
85
86
		// Service Exception.
87
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
88
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
89
			$service_exception = ServiceException::from_object( $data );
90
91
			throw $service_exception;
92
		}
93
94 10
		return $data;
95
	}
96
97 10
	/**
98
	 * Create payment.
99 10
	 *
100
	 * @param PaymentRequest $request Payment request.
101
	 *
102
	 * @return PaymentResponse
103
	 *
104
	 * @throws \Exception Throws error if request fails.
105
	 */
106
	public function create_payment( PaymentRequest $request ) {
107
		$data = $this->send_request( 'v51/payments', $request );
108
109
		return PaymentResponse::from_object( $data );
110
	}
111
112
	/**
113 10
	 * Submit additional payment details.
114
	 *
115
	 * @param PaymentDetailsRequest $request Payment request.
116
	 *
117
	 * @return PaymentResponse
118
	 *
119
	 * @throws \Exception Throws error if request fails.
120
	 */
121
	public function request_payment_details( PaymentDetailsRequest $request ) {
122
		$data = $this->send_request( 'v51/payments/details', $request );
123
124
		return PaymentResponse::from_object( $data );
125
	}
126 10
127 1
	/**
128
	 * Create payment session.
129 1
	 *
130
	 * @param PaymentSessionRequest $request Payment session request.
131
	 *
132
	 * @return PaymentSessionResponse
133
	 *
134 9
	 * @throws \Exception Throws error if request fails.
135 5
	 */
136
	public function create_payment_session( PaymentSessionRequest $request ) {
137 5
		$data = $this->send_request( 'v41/paymentSession', $request );
138
139
		return PaymentSessionResponse::from_object( $data );
140 4
	}
141
142
	/**
143
	 * Get payment result.
144
	 *
145
	 * @param PaymentResultRequest $request Payment result request.
146
	 *
147
	 * @return PaymentResultResponse
148
	 *
149
	 * @throws \Exception Throws error if request fails.
150
	 */
151
	public function get_payment_result( PaymentResultRequest $request ) {
152 1
		$data = $this->send_request( 'v41/payments/result', $request );
153 1
154
		return PaymentResultResponse::from_object( $data );
155 1
	}
156
157
	/**
158
	 * Get payment methods.
159
	 *
160
	 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/paymentMethods
161
	 * @link https://docs.adyen.com/checkout/drop-in-web#step-1-get-available-payment-methods
162
	 *
163
	 * @param PaymentMethodsRequest $request Payment methods request.
164
	 *
165
	 * @return PaymentMethodsResponse
166
	 * @throws \Exception Throws error if request fails.
167
	 */
168
	public function get_payment_methods( PaymentMethodsRequest $request ) {
169
		$data = $this->send_request( 'v51/paymentMethods', $request );
170
171
		return PaymentMethodsResponse::from_object( $data );
172
	}
173
}
174