Completed
Push — master ( 83a557...8e0143 )
by Remco
11:52 queued 05:23
created

PaymentsResultController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 69.77%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
c 3
b 0
f 0
dl 0
loc 110
ccs 30
cts 43
cp 0.6977
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1
A rest_api_init() 0 23 1
A rest_api_adyen_payments_result() 0 57 4
1
<?php
2
/**
3
 * Payments result controller
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 JsonSchema\Exception\ValidationException;
14
use Pronamic\WordPress\Pay\Core\Statuses as PaymentStatus;
15
use Pronamic\WordPress\Pay\Plugin;
16
use WP_Error;
17
use WP_REST_Request;
18
19
/**
20
 * Payments result controller
21
 *
22
 * @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete
23
 *
24
 * @author  Remco Tolsma
25
 * @version 1.0.0
26
 * @since   1.0.0
27
 */
28
class PaymentsResultController {
29
	/**
30
	 * Setup.
31
	 *
32
	 * @return void
33
	 */
34 9
	public function setup() {
35 9
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
36 9
	}
37
38
	/**
39
	 * REST API init.
40
	 *
41
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
42
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
43
	 *
44
	 * @return void
45
	 */
46 10
	public function rest_api_init() {
47 10
		register_rest_route(
48 10
			Integration::REST_ROUTE_NAMESPACE,
49 10
			'/payments/result/(?P<config_id>\d+)',
50
			array(
51 10
				'methods'  => 'POST',
52 10
				'callback' => array( $this, 'rest_api_adyen_payments_result' ),
53
				'args'     => array(
54
					'config_id'  => array(
55 10
						'description' => __( 'Gateway configuration ID.', 'pronamic_ideal' ),
56 10
						'type'        => 'integer',
57
					),
58
					'payload'    => array(
59 10
						'description' => __( 'Payload.', 'pronamic_ideal' ),
60 10
						'type'        => 'string',
61
					),
62
					'resultCode' => array(
63 10
						'description' => __( 'Result code.', 'pronamic_ideal' ),
64 10
						'type'        => 'string',
65
					),
66
					'resultText' => array(
67 10
						'description' => __( 'Result text.', 'pronamic_ideal' ),
68 10
						'type'        => 'string',
69
					),
70
				),
71
			)
72
		);
73 10
	}
74
75
	/**
76
	 * REST API Adyen payments result handler.
77
	 *
78
	 * @param WP_REST_Request $request Request.
79
	 * @return object
80
	 */
81 2
	public function rest_api_adyen_payments_result( WP_REST_Request $request ) {
82 2
		$config_id = $request->get_param( 'config_id' );
83 2
		$payload   = $request->get_param( 'payload' );
84
85
		// Gateway.
86 2
		$gateway = Plugin::get_gateway( $config_id );
87
88 2
		if ( empty( $gateway ) ) {
89 1
			return new WP_Error(
90 1
				'pronamic-pay-adyen-gateway-not-found',
91 1
				sprintf(
92
					/* translators: %s: Gateway configuration ID */
93 1
					__( 'Could not found gateway with ID `%s`.', 'pronamic_ideal' ),
94
					$config_id
95
				),
96
				$config_id
97
			);
98
		}
99
100 1
		if ( ! isset( $gateway->client ) ) {
101
			return new WP_Error(
102
				'pronamic-pay-adyen-client-not-found',
103
				sprintf(
104
					/* translators: %s: Gateway configuration ID */
105
					__( 'Could not found client in gateway with ID `%s`.', 'pronamic_ideal' ),
106
					$config_id
107
				),
108
				$config_id
109
			);
110
		}
111
112
		// Client.
113 1
		$payment_result_request = new PaymentResultRequest( $payload );
114
115 1
		$payment_result_response = $gateway->client->get_payment_result( $payment_result_request );
116
117
		$merchant_reference = $payment_result_response->get_merchant_reference();
118
119
		// Payment.
120
		$payment = get_pronamic_payment( $merchant_reference );
121
122
		if ( empty( $payment ) ) {
123
			return new WP_Error(
124
				'pronamic-pay-adyen-payment-not-found',
125
				sprintf(
126
					/* translators: %s: Adyen merchant reference */
127
					__( 'Could not found payment with ID `%s`.', 'pronamic_ideal' ),
128
					$merchant_reference
129
				),
130
				$payment_result_response
131
			);
132
		}
133
134
		PaymentResultHelper::update_payment( $payment, $payment_result_response );
135
136
		// Return payment result response.
137
		return $payment_result_response->get_json();
138
	}
139
}
140