|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Mollie customer data store. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2020 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Pay |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Title: Mollie customer data store |
|
15
|
|
|
* Description: |
|
16
|
|
|
* Copyright: 2005-2020 Pronamic |
|
17
|
|
|
* Company: Pronamic |
|
18
|
|
|
* |
|
19
|
|
|
* @author Remco Tolsma |
|
20
|
|
|
* @version 2.1.0 |
|
21
|
|
|
* @since 2.1.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class CustomerDataStore { |
|
24
|
|
|
/** |
|
25
|
|
|
* Get or insert customer. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Customer $customer Customer. |
|
28
|
|
|
* @param array<string> $data Data. |
|
29
|
|
|
* @param array<string> $format Format. |
|
30
|
|
|
* @return int |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function get_or_insert_customer( Customer $customer, $data = array(), $format = array() ) { |
|
33
|
2 |
|
$customer_data = $this->get_customer( $customer ); |
|
34
|
|
|
|
|
35
|
2 |
|
if ( null !== $customer_data ) { |
|
36
|
|
|
return $customer_data->id; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
return $this->insert_customer( $customer, $data, $format ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get customer data for specified Mollie customer. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Customer $customer Mollie customer. |
|
46
|
|
|
* @return object|null |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function get_customer( Customer $customer ) { |
|
49
|
2 |
|
global $wpdb; |
|
50
|
|
|
|
|
51
|
2 |
|
$id = $customer->get_id(); |
|
52
|
|
|
|
|
53
|
2 |
|
if ( null === $id ) { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$data = $wpdb->get_row( |
|
58
|
2 |
|
$wpdb->prepare( |
|
59
|
2 |
|
" |
|
60
|
|
|
SELECT |
|
61
|
|
|
* |
|
62
|
|
|
FROM |
|
63
|
2 |
|
$wpdb->pronamic_pay_mollie_customers |
|
64
|
|
|
WHERE |
|
65
|
|
|
mollie_id = %s |
|
66
|
|
|
LIMIT |
|
67
|
|
|
1 |
|
68
|
|
|
; |
|
69
|
|
|
", |
|
70
|
|
|
$id |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
2 |
|
return $data; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Insert Mollie customer. |
|
79
|
|
|
* |
|
80
|
|
|
* @param Customer $customer Customer. |
|
81
|
|
|
* @param array<string> $data Data. |
|
82
|
|
|
* @param array<string> $format Format. |
|
83
|
|
|
* @return int |
|
84
|
|
|
* @throws \Exception Throws exception on error. |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function insert_customer( Customer $customer, $data = array(), $format = array() ) { |
|
87
|
2 |
|
global $wpdb; |
|
88
|
|
|
|
|
89
|
2 |
|
$mollie_id = $customer->get_id(); |
|
90
|
|
|
|
|
91
|
2 |
|
if ( empty( $mollie_id ) ) { |
|
92
|
|
|
throw new \Exception( 'Can not insert Mollie customer with empty ID.' ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$data['mollie_id'] = $mollie_id; |
|
96
|
2 |
|
$format['mollie_id'] = '%s'; |
|
97
|
|
|
|
|
98
|
2 |
|
$data['test_mode'] = ( 'test' === $customer->get_mode() ); |
|
99
|
2 |
|
$format['test_mode'] = '%d'; |
|
100
|
|
|
|
|
101
|
2 |
|
$data['email'] = $customer->get_email(); |
|
102
|
2 |
|
$format['email'] = '%s'; |
|
103
|
|
|
|
|
104
|
2 |
|
$result = $wpdb->insert( |
|
105
|
2 |
|
$wpdb->pronamic_pay_mollie_customers, |
|
106
|
|
|
$data, |
|
107
|
|
|
$format |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
2 |
|
if ( false === $result ) { |
|
111
|
|
|
throw new \Exception( |
|
112
|
|
|
sprintf( |
|
113
|
|
|
'Could not insert Mollie customer ID: %s, error: %s.', |
|
114
|
|
|
$mollie_id, |
|
115
|
|
|
$wpdb->last_error |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
$id = $wpdb->insert_id; |
|
121
|
|
|
|
|
122
|
2 |
|
return $id; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Update Mollie customer. |
|
127
|
|
|
* |
|
128
|
|
|
* @param Customer $customer Customer. |
|
129
|
|
|
* @param array<string> $data Data. |
|
130
|
|
|
* @param array<string> $format Format. |
|
131
|
|
|
* @return int The number of rows updated. |
|
132
|
|
|
* @throws \Exception Throws exception on error. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function update_customer( Customer $customer, $data = array(), $format = array() ) { |
|
135
|
|
|
global $wpdb; |
|
136
|
|
|
|
|
137
|
|
|
$mollie_id = $customer->get_id(); |
|
138
|
|
|
|
|
139
|
|
|
if ( empty( $mollie_id ) ) { |
|
140
|
|
|
throw new \Exception( 'Can not update Mollie customer with empty ID.' ); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$data['test_mode'] = ( 'test' === $customer->get_mode() ); |
|
144
|
|
|
$format['test_mode'] = '%d'; |
|
145
|
|
|
|
|
146
|
|
|
$data['email'] = $customer->get_email(); |
|
147
|
|
|
$format['email'] = '%s'; |
|
148
|
|
|
|
|
149
|
|
|
$result = $wpdb->update( |
|
150
|
|
|
$wpdb->pronamic_pay_mollie_customers, |
|
151
|
|
|
$data, |
|
152
|
|
|
array( |
|
153
|
|
|
'mollie_id' => $mollie_id, |
|
154
|
|
|
), |
|
155
|
|
|
$format, |
|
156
|
|
|
array( |
|
157
|
|
|
'mollie_id' => '%s', |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
if ( false === $result ) { |
|
162
|
|
|
throw new \Exception( |
|
163
|
|
|
sprintf( |
|
164
|
|
|
'Could not update Mollie customer ID: %s, error: %s.', |
|
165
|
|
|
$mollie_id, |
|
166
|
|
|
$wpdb->last_error |
|
167
|
|
|
) |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $result; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Save Mollie customer. |
|
176
|
|
|
* |
|
177
|
|
|
* @param Customer $customer Customer. |
|
178
|
|
|
* @param array<string> $data Data. |
|
179
|
|
|
* @param array<string> $format Format. |
|
180
|
|
|
* @return int |
|
181
|
|
|
*/ |
|
182
|
|
|
public function save_customer( Customer $customer, $data = array(), $format = array() ) { |
|
183
|
|
|
$customer_data = $this->get_customer( $customer ); |
|
184
|
|
|
|
|
185
|
|
|
if ( null !== $customer_data ) { |
|
186
|
|
|
$this->update_customer( $customer, $data, $format ); |
|
187
|
|
|
|
|
188
|
|
|
return $customer_data->id; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this->insert_customer( $customer, $data, $format ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Connect Mollie customer to WordPress user. |
|
196
|
|
|
* |
|
197
|
|
|
* @param Customer $customer Mollie customer. |
|
198
|
|
|
* @param \WP_User $user WordPress user. |
|
199
|
|
|
* @return int Number of rows affected. |
|
200
|
|
|
* @throws \Exception Throws exception on error. |
|
201
|
|
|
*/ |
|
202
|
2 |
|
public function connect_mollie_customer_to_wp_user( $customer, \WP_User $user ) { |
|
203
|
2 |
|
global $wpdb; |
|
204
|
|
|
|
|
205
|
2 |
|
$result = $wpdb->query( |
|
206
|
2 |
|
$wpdb->prepare( |
|
207
|
|
|
" |
|
208
|
2 |
|
INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users ( |
|
209
|
|
|
customer_id, |
|
210
|
|
|
user_id |
|
211
|
|
|
) |
|
212
|
|
|
SELECT |
|
213
|
|
|
mollie_customer.id AS mollie_customer_id, |
|
214
|
|
|
wp_user.ID AS wp_user_id |
|
215
|
|
|
FROM |
|
216
|
2 |
|
$wpdb->pronamic_pay_mollie_customers AS mollie_customer |
|
217
|
|
|
JOIN |
|
218
|
2 |
|
$wpdb->users AS wp_user |
|
219
|
|
|
WHERE |
|
220
|
|
|
mollie_customer.mollie_id = %s |
|
221
|
|
|
AND |
|
222
|
|
|
wp_user.ID = %d |
|
223
|
|
|
; |
|
224
|
|
|
", |
|
225
|
2 |
|
$customer->get_id(), |
|
226
|
2 |
|
$user->ID |
|
227
|
|
|
) |
|
228
|
|
|
); |
|
229
|
|
|
|
|
230
|
2 |
|
if ( false === $result ) { |
|
231
|
|
|
throw new \Exception( |
|
232
|
|
|
sprintf( |
|
233
|
|
|
'Database error: %s.', |
|
234
|
|
|
$wpdb->last_error |
|
235
|
|
|
) |
|
236
|
|
|
); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
2 |
|
return $result; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|