Test Failed
Push — master ( 272cf6...4fb2a3 )
by Remco
21:31 queued 14:28
created

Integration::get_gateway()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Mollie integration.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use Pronamic\WordPress\Pay\Gateways\Common\AbstractIntegration;
14
use Pronamic\WordPress\Pay\Payments\Payment;
15
use WP_User;
16
17
/**
18
 * Title: Mollie integration
19
 * Description:
20
 * Copyright: 2005-2019 Pronamic
21 6
 * Company: Pronamic
22 6
 *
23 6
 * @author  Remco Tolsma
24 6
 * @version 2.0.0
25 6
 * @since   1.0.0
26 6
 */
27 6
class Integration extends AbstractIntegration {
28
	/**
29
	 * Construct and intialize Mollie integration.
30 6
	 */
31
	public function __construct() {
32 6
		$this->id            = 'mollie';
33 6
		$this->name          = 'Mollie';
34
		$this->url           = 'http://www.mollie.com/en/';
35
		$this->product_url   = __( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' );
36 6
		$this->dashboard_url = 'https://www.mollie.com/dashboard/';
37
		$this->register_url  = 'https://www.mollie.com/nl/signup/665327';
0 ignored issues
show
Bug Best Practice introduced by
The property register_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
		$this->provider      = 'mollie';
39
		$this->supports      = array(
40
			'payment_status_request',
41
			'webhook',
42
			'webhook_log',
43
			'webhook_no_config',
44
		);
45
46
		// Actions.
47
		$function = array( __NAMESPACE__ . '\Listener', 'listen' );
48 6
49 6
		if ( ! has_action( 'wp_loaded', $function ) ) {
50
			add_action( 'wp_loaded', $function );
51
		}
52
53
		if ( is_admin() ) {
54
			$function = array( __CLASS__, 'user_profile' );
55
56 1
			if ( ! has_action( 'show_user_profile', $function ) ) {
57 1
				add_action( 'show_user_profile', $function );
58
			}
59
60
			if ( ! has_action( 'edit_user_profile', $function ) ) {
61
				add_action( 'edit_user_profile', $function );
62
			}
63
		}
64
65 1
		add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 );
66 1
	}
67
68
	/**
69
	 * Get settings fields.
70
	 *
71
	 * @return array
72
	 */
73
	public function get_settings_fields() {
74
		$fields = array();
75
76 1
		// API Key.
77 1
		$fields[] = array(
78
			'section'  => 'general',
79 1
			'filter'   => FILTER_SANITIZE_STRING,
80
			'meta_key' => '_pronamic_gateway_mollie_api_key',
81 1
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
82
			'type'     => 'text',
83
			'classes'  => array( 'regular-text', 'code' ),
84
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
85
		);
86
87
		// Webhook.
88
		$fields[] = array(
89
			'section'  => 'feedback',
90
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
91
			'type'     => 'text',
92
			'classes'  => array( 'large-text', 'code' ),
93
			'value'    => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ),
94
			'readonly' => true,
95
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
96
		);
97
98
		return $fields;
99
	}
100
101 1
	/**
102 1
	 * Save post.
103 1
	 *
104 1
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
105
	 *
106
	 * @param int $post_id Post ID.
107
	 */
108
	public function save_post( $post_id ) {
109
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
110
111
		if ( ! is_string( $api_key ) ) {
112
			return;
113
		}
114
115
		$api_key_prefix = substr( $api_key, 0, 4 );
116
117
		switch ( $api_key_prefix ) {
118
			case 'live':
119
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
120
121
				return;
122
			case 'test':
123
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
124
125
				return;
126
		}
127
	}
128
129
	/**
130
	 * User profile.
131
	 *
132
	 * @param WP_User $user WordPress user.
133
	 *
134
	 * @since 1.1.6
135
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
136
	 */
137
	public static function user_profile( $user ) {
138
		include __DIR__ . '/../views/html-admin-user-profile.php';
139
	}
140
141
	/**
142
	 * Payment provider URL.
143
	 *
144
	 * @param string  $url     Payment provider URL.
145
	 * @param Payment $payment Payment.
146
	 *
147
	 * @return string
148
	 */
149
	public function payment_provider_url( $url, Payment $payment ) {
150
		return sprintf(
151
			'https://www.mollie.com/dashboard/payments/%s',
152
			$payment->get_transaction_id()
153
		);
154
	}
155
	/**
156
	 * Get configuration by post ID.
157
	 *
158
	 * @param int $post_id Post ID.
159
	 *
160
	 * @return Config
161
	 */
162
	public function get_config( $post_id ) {
163
		$config = new Config();
164
165
		$config->id      = intval( $post_id );
166
		$config->api_key = $this->get_meta( $post_id, 'mollie_api_key' );
167
		$config->mode    = $this->get_meta( $post_id, 'mode' );
168
169
		return $config;
170
	}
171
172
	/**
173
	 * Get gateway.
174
	 *
175
	 * @param int $post_id Post ID.
176
	 * @return Gateway
177
	 */
178
	public function get_gateway( $post_id ) {
179
		return new Gateway( $this->get_config( $post_id ) );
180
	}
181
}
182