Issues (2)

src/Integration.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Nocks;
4
5
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
6
use Pronamic\WordPress\Pay\Util;
7
8
/**
9
 * Title: Nocks integration
10
 * Description:
11
 * Copyright: 2005-2020 Pronamic
12
 * Company: Pronamic
13
 *
14
 * @author  Reüel van der Steege
15
 * @version 2.0.0
16
 * @since   1.0.0
17
 */
18
class Integration extends AbstractGatewayIntegration {
19
	/**
20
	 * Construct Nocks integration.
21
	 *
22
	 * @param array $args Arguments.
23
	 */
24
	public function __construct( $args = array() ) {
25
		$args = wp_parse_args(
26
			$args,
27
			array(
28
				'id'            => 'nocks',
29
				'name'          => 'Nocks - Checkout',
30
				'product_url'   => 'https://www.nocks.com/',
31
				'dashboard_url' => 'https://www.nocks.com/',
32
				'provider'      => 'nocks',
33
				'supports'      => array(
34
					'payment_status_request',
35
					'webhook',
36
					'webhook_log',
37
					'webhook_no_config',
38
				),
39
				'deprecated'    => true,
40
			)
41
		);
42
43
		parent::__construct( $args );
44
45
		// Actions
46
		$function = array( __NAMESPACE__ . '\Listener', 'listen' );
47
48
		if ( ! has_action( 'wp_loaded', $function ) ) {
49
			add_action( 'wp_loaded', $function );
50
		}
51
	}
52
53
	/**
54
	 * Get settings fields.
55
	 *
56
	 * @return array
57
	 */
58
	public function get_settings_fields() {
59
		$fields = array();
60
61
		// Access token.
62
		$fields[] = array(
63
			'section'  => 'general',
64
			'filter'   => FILTER_SANITIZE_STRING,
65
			'meta_key' => '_pronamic_gateway_nocks_access_token',
66
			'title'    => _x( 'Access Token', 'nocks', 'pronamic_ideal' ),
67
			'type'     => 'textarea',
68
			'classes'  => array( 'code' ),
69
		);
70
71
		// Merchant profile.
72
		$fields[] = array(
73
			'section'  => 'general',
74
			'filter'   => FILTER_SANITIZE_STRING,
75
			'meta_key' => '_pronamic_gateway_nocks_merchant_profile',
76
			'title'    => _x( 'Merchant Profile', 'nocks', 'pronamic_ideal' ),
77
			'type'     => 'description',
78
			'callback' => array( $this, 'field_merchant_profile' ),
79
		);
80
81
		// Webhook URL.
82
		$fields[] = array(
83
			'section'  => 'feedback',
84
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
85
			'type'     => 'text',
86
			'classes'  => array( 'large-text', 'code' ),
87
			'value'    => add_query_arg( 'nocks_webhook', '', home_url( '/' ) ),
88
			'readonly' => true,
89
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
90
		);
91
92
		return $fields;
93
	}
94
95
	/**
96
	 * Field merchant profile select.
97
	 *
98
	 * @param array $field Settings field.
99
	 */
100
	public function field_merchant_profile( $field ) {
101
		$access_token     = get_post_meta( get_the_ID(), '_pronamic_gateway_nocks_access_token', true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
		$access_token     = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_nocks_access_token', true );
Loading history...
102
		$merchant_profile = get_post_meta( get_the_ID(), '_pronamic_gateway_nocks_merchant_profile', true );
103
104
		if ( ! $access_token ) {
105
			esc_html_e( 'First enter an API Key and save the configuration, to be able to choose from your Nocks merchant profiles.', 'pronamic_ideal' );
106
107
			return;
108
		}
109
110
		$client = new Client();
111
112
		$client->set_access_token( $access_token );
113
114
		// Select merchant profile.
115
		printf( '<select name="%s">', esc_attr( $field['meta_key'] ) );
116
117
		$options = array(
118
			__( '— Select Merchant Profile —', 'pronamic_ideal' ),
119
		);
120
121
		try {
122
			$options = array_merge( $options, $client->get_merchant_profiles() );
123
		} catch ( \Exception $e ) {
124
			// What to do?
125
		}
126
127
		$options = array(
128
			array(
129
				'options' => $options,
130
			),
131
		);
132
133
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134
		echo Util::select_options_grouped( $options, $merchant_profile );
0 ignored issues
show
It seems like $merchant_profile can also be of type false; however, parameter $selected_value of Pronamic\WordPress\Pay\U...elect_options_grouped() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
		echo Util::select_options_grouped( $options, /** @scrutinizer ignore-type */ $merchant_profile );
Loading history...
135
136
		echo '</select>';
137
	}
138
139
	public function get_config( $post_id ) {
140
		$config = new Config();
141
142
		$config->mode             = $this->get_meta( $post_id, '_pronamic_gateway_mode' );
143
		$config->access_token     = $this->get_meta( $post_id, '_pronamic_gateway_nocks_access_token' );
144
		$config->merchant_profile = $this->get_meta( $post_id, '_pronamic_gateway_nocks_merchant_profile' );
145
146
		return $config;
147
	}
148
149
	/**
150
	 * Get gateway.
151
	 *
152
	 * @param int $post_id Post ID.
153
	 * @return Gateway
154
	 */
155
	public function get_gateway( $post_id ) {
156
		return new Gateway( $this->get_config( $post_id ) );
157
	}
158
}
159