Failed Conditions
Push — develop ( 746dec...d770ee )
by Remco
07:30
created

Integration::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
ccs 0
cts 8
cp 0
crap 6
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\IDealBasic;
4
5
use Pronamic\WordPress\Pay\Gateways\IDeal\AbstractIntegration;
6
7
/**
8
 * Title: Integration
9
 * Description:
10
 * Copyright: 2005-2021 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author  Remco Tolsma
14
 * @version 2.1.1
15
 * @since   1.0.0
16
 */
17
class Integration extends AbstractIntegration {
18
	/**
19
	 * REST route namespace.
20
	 *
21
	 * @var string
22
	 */
23
	const REST_ROUTE_NAMESPACE = 'pronamic-pay/ideal-basic/v1';
24
25
	/**
26
	 * Construct iDEAL Basic integration.
27
	 *
28
	 * @param array $args Arguments.
29
	 */
30
	public function __construct( $args = array() ) {
31
		$args = wp_parse_args(
32
			$args,
33
			array(
34
				'id'                => 'ideal-basic',
35
				'name'              => 'iDEAL Basic',
36
				'url'               => \__( 'https://www.ideal.nl/en/', 'pronamic_ideal' ),
37
				'product_url'       => \__( 'https://www.ideal.nl/en/', 'pronamic_ideal' ),
38
				'manual_url'        => null,
39
				'dashboard_url'     => null,
40
				'provider'          => null,
41
				'acquirer_url'      => null,
42
				'acquirer_test_url' => null,
43
				'deprecated'        => false,
44
				'supports'          => array(
45
					'webhook',
46
					'webhook_log',
47
				),
48
			)
49
		);
50
51
		parent::__construct( $args );
52
53
		// Acquirer URL.
54
		$this->acquirer_url      = $args['acquirer_url'];
55
		$this->acquirer_test_url = $args['acquirer_test_url'];
56
	}
57
58
	/**
59
	 * Setup gateway integration.
60
	 *
61
	 * @return void
62
	 */
63
	public function setup() {
64
		// Check if dependencies are met and integration is active.
65
		if ( ! $this->is_active() ) {
66
			return;
67
		}
68
69
		// Notification controller.
70
		$notification_controller = new NotificationController();
71
72
		$notification_controller->setup();
73
	}
74
75
	/**
76
	 * Get settings fields.
77
	 *
78
	 * @return array<int, array<string, callable|int|string|bool|array<int|string,int|string>>>
79
	 */
80
	public function get_settings_fields() {
81
		$fields = parent::get_settings_fields();
82
83
		// Hash Key
84
		$fields[] = array(
85
			'section'  => 'general',
86
			'filter'   => FILTER_SANITIZE_STRING,
87
			'meta_key' => '_pronamic_gateway_ideal_hash_key',
88
			'title'    => __( 'Hash Key', 'pronamic_ideal' ),
89
			'type'     => 'text',
90
			'classes'  => array( 'regular-text', 'code' ),
91
			'tooltip'  => __( 'Hash key (also known as: key or secret key) as mentioned in the payment provider dashboard.', 'pronamic_ideal' ),
92
			'methods'  => array( 'ideal-basic' ),
93
		);
94
95
		// XML Notification URL.
96
		$fields[] = array(
97
			'section'  => 'feedback',
98
			/* translators: Translate 'XML notification URL' the same as in the iDEAL Basic dashboard. */
99
			'title'    => _x( 'XML Notification URL', 'iDEAL Basic dashboard', 'pronamic_ideal' ),
100
			'type'     => 'text',
101
			'classes'  => array( 'regular-text', 'code' ),
102
			'value'    => \rest_url( self::REST_ROUTE_NAMESPACE . '/notification' ),
103
			'methods'  => array( 'ideal-basic' ),
104
			'readonly' => true,
105
			'size'     => 200,
106
			/* translators: Translate 'XML notification URL' the same as in the iDEAL Basic dashboard. */
107
			'tooltip'  => _x(
108
				'Copy the XML notification URL to the payment provider dashboard to receive automatic transaction status updates.',
109
				'iDEAL Basic dashboard',
110
				'pronamic_ideal'
111
			),
112
		);
113
114
		// Return fields.
115
		return $fields;
116
	}
117
118
	public function get_config( $post_id ) {
119
		$mode = get_post_meta( $post_id, '_pronamic_gateway_mode', true );
120
121
		$config = new Config();
122
123
		$config->url = $this->acquirer_url;
124
125
		if ( 'test' === $mode && null !== $this->acquirer_test_url ) {
126
			$config->url = $this->acquirer_test_url;
127
		}
128
129
		$config->merchant_id = get_post_meta( $post_id, '_pronamic_gateway_ideal_merchant_id', true );
130
		$config->sub_id      = get_post_meta( $post_id, '_pronamic_gateway_ideal_sub_id', true );
131
		$config->hash_key    = get_post_meta( $post_id, '_pronamic_gateway_ideal_hash_key', true );
132
		$config->purchase_id = get_post_meta( $post_id, '_pronamic_gateway_ideal_purchase_id', true );
133
134
		return $config;
135
	}
136
137
	/**
138
	 * Get gateway.
139
	 *
140
	 * @param int $post_id Post ID.
141
	 * @return Gateway
142
	 */
143
	public function get_gateway( $post_id ) {
144
		return new Gateway( $this->get_config( $post_id ) );
145
	}
146
}
147