Issues (11)

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 10
				'permission_callback' => '__return_true',
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
84 2
		if ( null === $config_id ) {
85
			return new WP_Error(
86
				'pronamic-pay-adyen-no-gateway-configuration-id',
87
				__( 'No gateway configuration ID given in `config_id` parameter.', 'pronamic_ideal' )
88
			);
89
		}
90
91 2
		$payload = $request->get_param( 'payload' );
92
93 2
		if ( null === $payload ) {
94 1
			return new WP_Error(
95 1
				'pronamic-pay-adyen-no-payload',
96 1
				__( 'No payload given in `payload` parameter.', 'pronamic_ideal' )
97
			);
98
		}
99
100
		// Gateway.
101 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...
102
103 1
		if ( empty( $gateway ) ) {
104
			return new WP_Error(
105
				'pronamic-pay-adyen-gateway-not-found',
106
				sprintf(
107
					/* translators: %s: Gateway configuration ID */
108
					__( 'Could not found gateway with ID `%s`.', 'pronamic_ideal' ),
109
					$config_id
110
				),
111
				$config_id
112
			);
113
		}
114
115 1
		if ( ! isset( $gateway->client ) ) {
116
			return new WP_Error(
117
				'pronamic-pay-adyen-client-not-found',
118
				sprintf(
119
					/* translators: %s: Gateway configuration ID */
120
					__( 'Could not found client in gateway with ID `%s`.', 'pronamic_ideal' ),
121
					$config_id
122
				),
123
				$config_id
124
			);
125
		}
126
127
		// Client.
128 1
		$payment_result_request = new PaymentResultRequest( $payload );
129
130 1
		$payment_result_response = $gateway->client->get_payment_result( $payment_result_request );
131
132
		$merchant_reference = $payment_result_response->get_merchant_reference();
133
134
		// Payment.
135
		$payment = get_pronamic_payment( $merchant_reference );
136
137
		if ( empty( $payment ) ) {
138
			return new WP_Error(
139
				'pronamic-pay-adyen-payment-not-found',
140
				sprintf(
141
					/* translators: %s: Adyen merchant reference */
142
					__( 'Could not found payment with ID `%s`.', 'pronamic_ideal' ),
143
					$merchant_reference
144
				),
145
				$payment_result_response
146
			);
147
		}
148
149
		PaymentResultHelper::update_payment( $payment, $payment_result_response );
150
151
		// Return payment result response.
152
		return $payment_result_response->get_json();
153
	}
154
}
155