Failed Conditions
Push — develop ( 1d71e2...bb0e32 )
by Remco
04:52
created

src/Integration.php (3 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Nocks;
4
5
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
0 ignored issues
show
The type Pronamic\WordPress\Pay\AbstractGatewayIntegration 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...
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
			)
40
		);
41
42
		parent::__construct( $args );
43
44
		// Actions
45
		$function = array( __NAMESPACE__ . '\Listener', 'listen' );
46
47
		if ( ! has_action( 'wp_loaded', $function ) ) {
48
			add_action( 'wp_loaded', $function );
49
		}
50
	}
51
52
	/**
53
	 * Get settings fields.
54
	 *
55
	 * @return array
56
	 */
57
	public function get_settings_fields() {
58
		$fields = array();
59
60
		// Access token.
61
		$fields[] = array(
62
			'section'  => 'general',
63
			'filter'   => FILTER_SANITIZE_STRING,
64
			'meta_key' => '_pronamic_gateway_nocks_access_token',
65
			'title'    => _x( 'Access Token', 'nocks', 'pronamic_ideal' ),
66
			'type'     => 'textarea',
67
			'classes'  => array( 'code' ),
68
		);
69
70
		// Merchant profile.
71
		$fields[] = array(
72
			'section'  => 'general',
73
			'filter'   => FILTER_SANITIZE_STRING,
74
			'meta_key' => '_pronamic_gateway_nocks_merchant_profile',
75
			'title'    => _x( 'Merchant Profile', 'nocks', 'pronamic_ideal' ),
76
			'type'     => 'description',
77
			'callback' => array( $this, 'field_merchant_profile' ),
78
		);
79
80
		// Webhook URL.
81
		$fields[] = array(
82
			'section'  => 'feedback',
83
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
84
			'type'     => 'text',
85
			'classes'  => array( 'large-text', 'code' ),
86
			'value'    => add_query_arg( 'nocks_webhook', '', home_url( '/' ) ),
87
			'readonly' => true,
88
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
89
		);
90
91
		return $fields;
92
	}
93
94
	/**
95
	 * Field merchant profile select.
96
	 *
97
	 * @param array $field Settings field.
98
	 */
99
	public function field_merchant_profile( $field ) {
100
		$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

100
		$access_token     = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_nocks_access_token', true );
Loading history...
101
		$merchant_profile = get_post_meta( get_the_ID(), '_pronamic_gateway_nocks_merchant_profile', true );
102
103
		if ( ! $access_token ) {
104
			esc_html_e( 'First enter an API Key and save the configuration, to be able to choose from your Nocks merchant profiles.', 'pronamic_ideal' );
105
106
			return;
107
		}
108
109
		$client = new Client();
110
111
		$client->set_access_token( $access_token );
112
113
		// Select merchant profile.
114
		printf( '<select name="%s">', esc_attr( $field['meta_key'] ) );
115
116
		$options = array(
117
			__( '— Select Merchant Profile —', 'pronamic_ideal' ),
118
		);
119
120
		try {
121
			$options = array_merge( $options, $client->get_merchant_profiles() );
122
		} catch ( \Exception $e ) {
123
			// What to do?
124
		}
125
126
		$options = array(
127
			array(
128
				'options' => $options,
129
			),
130
		);
131
132
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
133
		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

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