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 | * Company: Pronamic |
||
22 | * |
||
23 | * @author Remco Tolsma |
||
24 | * @version 2.0.0 |
||
25 | * @since 1.0.0 |
||
26 | */ |
||
27 | class Integration extends AbstractIntegration { |
||
28 | /** |
||
29 | * Construct and intialize Mollie integration. |
||
30 | */ |
||
31 | 7 | public function __construct() { |
|
32 | 7 | $this->id = 'mollie'; |
|
33 | 7 | $this->name = 'Mollie'; |
|
34 | 7 | $this->url = 'http://www.mollie.com/en/'; |
|
35 | 7 | $this->product_url = __( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ); |
|
36 | 7 | $this->dashboard_url = 'https://www.mollie.com/dashboard/'; |
|
37 | 7 | $this->register_url = 'https://www.mollie.com/nl/signup/665327'; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
38 | 7 | $this->provider = 'mollie'; |
|
39 | 7 | $this->supports = array( |
|
40 | 'payment_status_request', |
||
41 | 'webhook', |
||
42 | 'webhook_log', |
||
43 | 'webhook_no_config', |
||
44 | ); |
||
45 | |||
46 | // Actions. |
||
47 | 7 | $function = array( __NAMESPACE__ . '\Listener', 'listen' ); |
|
48 | |||
49 | 7 | if ( ! has_action( 'wp_loaded', $function ) ) { |
|
50 | 7 | add_action( 'wp_loaded', $function ); |
|
51 | } |
||
52 | |||
53 | 7 | if ( is_admin() ) { |
|
54 | $function = array( __CLASS__, 'user_profile' ); |
||
55 | |||
56 | if ( ! has_action( 'show_user_profile', $function ) ) { |
||
57 | 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 | 7 | add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 ); |
|
66 | 7 | } |
|
67 | |||
68 | /** |
||
69 | * Get settings fields. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 1 | public function get_settings_fields() { |
|
74 | 1 | $fields = array(); |
|
75 | |||
76 | // API Key. |
||
77 | 1 | $fields[] = array( |
|
78 | 1 | 'section' => 'general', |
|
79 | 1 | 'filter' => FILTER_SANITIZE_STRING, |
|
80 | 1 | 'meta_key' => '_pronamic_gateway_mollie_api_key', |
|
81 | 1 | 'title' => _x( 'API Key', 'mollie', 'pronamic_ideal' ), |
|
82 | 1 | 'type' => 'text', |
|
83 | 'classes' => array( 'regular-text', 'code' ), |
||
84 | 1 | 'tooltip' => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ), |
|
85 | ); |
||
86 | |||
87 | // Webhook. |
||
88 | 1 | $fields[] = array( |
|
89 | 1 | 'section' => 'feedback', |
|
90 | 1 | 'title' => __( 'Webhook URL', 'pronamic_ideal' ), |
|
91 | 1 | 'type' => 'text', |
|
92 | 'classes' => array( 'large-text', 'code' ), |
||
93 | 1 | 'value' => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ), |
|
94 | 'readonly' => true, |
||
95 | 1 | 'tooltip' => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ), |
|
96 | ); |
||
97 | |||
98 | 1 | return $fields; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Save post. |
||
103 | * |
||
104 | * @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 | 1 | public function payment_provider_url( $url, Payment $payment ) { |
|
150 | 1 | return sprintf( |
|
151 | 1 | 'https://www.mollie.com/dashboard/payments/%s', |
|
152 | 1 | $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 | 2 | public function get_config( $post_id ) { |
|
163 | 2 | $config = new Config(); |
|
164 | |||
165 | 2 | $config->id = intval( $post_id ); |
|
166 | 2 | $config->api_key = $this->get_meta( $post_id, 'mollie_api_key' ); |
|
167 | 2 | $config->mode = $this->get_meta( $post_id, 'mode' ); |
|
168 | |||
169 | 2 | return $config; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get gateway. |
||
174 | * |
||
175 | * @param int $post_id Post ID. |
||
176 | * @return Gateway |
||
177 | */ |
||
178 | 1 | public function get_gateway( $post_id ) { |
|
179 | 1 | return new Gateway( $this->get_config( $post_id ) ); |
|
180 | } |
||
181 | } |
||
182 |