Failed Conditions
Push — develop ( 89f61e...ae8897 )
by Reüel
06:06
created

src/PaymentsResultController.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Payments result controller
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 JsonSchema\Exception\ValidationException;
14
use Pronamic\WordPress\Pay\Plugin;
15
use WP_Error;
16
use WP_REST_Request;
17
18
/**
19
 * Payments result controller
20
 *
21
 * @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.3
25
 * @since   1.0.0
26
 */
27
class PaymentsResultController {
28
	/**
29
	 * Setup.
30
	 *
31
	 * @return void
32
	 */
33 5
	public function setup() {
34 5
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
35 5
	}
36
37
	/**
38
	 * REST API init.
39
	 *
40
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
41
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
42
	 *
43
	 * @return void
44
	 */
45 10
	public function rest_api_init() {
46 10
		register_rest_route(
47 10
			Integration::REST_ROUTE_NAMESPACE,
48 10
			'/payments/result/(?P<config_id>\d+)',
49
			array(
50 10
				'methods'  => 'POST',
51 10
				'callback' => array( $this, 'rest_api_adyen_payments_result' ),
52
				'args'     => array(
53
					'config_id'  => array(
54 10
						'description' => __( 'Gateway configuration ID.', 'pronamic_ideal' ),
55 10
						'type'        => 'integer',
56
					),
57
					'payload'    => array(
58 10
						'description' => __( 'Payload.', 'pronamic_ideal' ),
59 10
						'type'        => 'string',
60
					),
61
					'resultCode' => array(
62 10
						'description' => __( 'Result code.', 'pronamic_ideal' ),
63 10
						'type'        => 'string',
64
					),
65
					'resultText' => array(
66 10
						'description' => __( 'Result text.', 'pronamic_ideal' ),
67 10
						'type'        => 'string',
68
					),
69
				),
70
			)
71
		);
72 10
	}
73
74
	/**
75
	 * REST API Adyen payments result handler.
76
	 *
77
	 * @param WP_REST_Request $request Request.
78
	 * @return object
79
	 */
80 2
	public function rest_api_adyen_payments_result( WP_REST_Request $request ) {
81 2
		$config_id = $request->get_param( 'config_id' );
82
83 2
		if ( null === $config_id ) {
84
			return new WP_Error(
85
				'pronamic-pay-adyen-no-gateway-configuration-id',
86
				__( 'No gateway configuration ID given in `config_id` parameter.', 'pronamic_ideal' )
87
			);
88
		}
89
90 2
		$payload = $request->get_param( 'payload' );
91
92 2
		if ( null === $payload ) {
93 1
			return new WP_Error(
94 1
				'pronamic-pay-adyen-no-payload',
95 1
				__( 'No payload given in `payload` parameter.', 'pronamic_ideal' )
96
			);
97
		}
98
99
		// Gateway.
100 1
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
101
102 1
		if ( empty( $gateway ) ) {
103
			return new WP_Error(
104
				'pronamic-pay-adyen-gateway-not-found',
105
				sprintf(
106
					/* translators: %s: Gateway configuration ID */
107
					__( 'Could not found gateway with ID `%s`.', 'pronamic_ideal' ),
108
					$config_id
109
				),
110
				$config_id
111
			);
112
		}
113
114 1
		if ( ! isset( $gateway->client ) ) {
115
			return new WP_Error(
116
				'pronamic-pay-adyen-client-not-found',
117
				sprintf(
118
					/* translators: %s: Gateway configuration ID */
119
					__( 'Could not found client in gateway with ID `%s`.', 'pronamic_ideal' ),
120
					$config_id
121
				),
122
				$config_id
123
			);
124
		}
125
126
		// Client.
127 1
		$payment_result_request = new PaymentResultRequest( $payload );
128
129 1
		$payment_result_response = $gateway->client->get_payment_result( $payment_result_request );
130
131
		$merchant_reference = $payment_result_response->get_merchant_reference();
132
133
		// Payment.
134
		$payment = get_pronamic_payment( $merchant_reference );
135
136
		if ( empty( $payment ) ) {
137
			return new WP_Error(
138
				'pronamic-pay-adyen-payment-not-found',
139
				sprintf(
140
					/* translators: %s: Adyen merchant reference */
141
					__( 'Could not found payment with ID `%s`.', 'pronamic_ideal' ),
142
					$merchant_reference
143
				),
144
				$payment_result_response
145
			);
146
		}
147
148
		PaymentResultHelper::update_payment( $payment, $payment_result_response );
149
150
		// Return payment result response.
151
		return $payment_result_response->get_json();
152
	}
153
}
154