1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Mollie profile data store. |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2022 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 profile data store |
15
|
|
|
* Description: |
16
|
|
|
* Copyright: 2005-2022 Pronamic |
17
|
|
|
* Company: Pronamic |
18
|
|
|
* |
19
|
|
|
* @author Remco Tolsma |
20
|
|
|
* @version 2.1.0 |
21
|
|
|
* @since 2.1.0 |
22
|
|
|
*/ |
23
|
|
|
class ProfileDataStore { |
24
|
|
|
/** |
25
|
|
|
* Get or insert profile. |
26
|
|
|
* |
27
|
|
|
* @param Profile $profile Profile. |
28
|
|
|
* @param array<string> $data Data. |
29
|
|
|
* @param array<string> $format Format. |
30
|
|
|
* @return int |
31
|
|
|
* @throws \Exception Throws exception if Mollie profile ID could not be retrieved from existing profile. |
32
|
|
|
*/ |
33
|
|
|
public function get_or_insert_profile( Profile $profile, $data = array(), $format = array() ) { |
34
|
|
|
$profile_data = $this->get_profile( $profile ); |
35
|
|
|
|
36
|
|
|
if ( null !== $profile_data ) { |
37
|
|
|
if ( ! \property_exists( $profile_data, 'id' ) ) { |
38
|
|
|
throw new \Exception( 'Unable to get Mollie profile ID for existing profile.' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $profile_data->id; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->insert_profile( $profile, $data, $format ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get profile data for specified Mollie profile. |
49
|
|
|
* |
50
|
|
|
* @param Profile $profile Profile. |
51
|
|
|
* @return object|null |
52
|
|
|
*/ |
53
|
|
|
public function get_profile( Profile $profile ) { |
54
|
|
|
global $wpdb; |
55
|
|
|
|
56
|
|
|
$id = $profile->get_id(); |
57
|
|
|
|
58
|
|
|
if ( null === $id ) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$data = $wpdb->get_row( |
63
|
|
|
$wpdb->prepare( |
64
|
|
|
" |
65
|
|
|
SELECT |
66
|
|
|
* |
67
|
|
|
FROM |
68
|
|
|
$wpdb->pronamic_pay_mollie_profiles |
69
|
|
|
WHERE |
70
|
|
|
mollie_id = %s |
71
|
|
|
LIMIT |
72
|
|
|
1 |
73
|
|
|
; |
74
|
|
|
", |
75
|
|
|
$id |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $data; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Insert Mollie profile. |
84
|
|
|
* |
85
|
|
|
* @param Profile $profile Profile. |
86
|
|
|
* @param array<string> $data Data. |
87
|
|
|
* @param array<string> $format Format. |
88
|
|
|
* @return int |
89
|
|
|
* @throws \Exception Throws exception on error. |
90
|
|
|
*/ |
91
|
|
|
public function insert_profile( Profile $profile, $data = array(), $format = array() ) { |
92
|
|
|
global $wpdb; |
93
|
|
|
|
94
|
|
|
$mollie_id = $profile->get_id(); |
95
|
|
|
|
96
|
|
|
if ( empty( $mollie_id ) ) { |
97
|
|
|
throw new \Exception( 'Can not insert Mollie profile with empty ID.' ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$data['mollie_id'] = $mollie_id; |
101
|
|
|
$format['mollie_id'] = '%s'; |
102
|
|
|
|
103
|
|
|
$data['email'] = $profile->get_email(); |
104
|
|
|
$format['email'] = '%s'; |
105
|
|
|
|
106
|
|
|
$data['name'] = $profile->get_name(); |
107
|
|
|
$format['name'] = '%s'; |
108
|
|
|
|
109
|
|
|
$result = $wpdb->insert( |
110
|
|
|
$wpdb->pronamic_pay_mollie_profiles, |
111
|
|
|
$data, |
112
|
|
|
$format |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
if ( false === $result ) { |
116
|
|
|
throw new \Exception( |
117
|
|
|
sprintf( |
118
|
|
|
'Could not insert Mollie profile ID: %s, error: %s.', |
119
|
|
|
$mollie_id, |
120
|
|
|
$wpdb->last_error |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$id = $wpdb->insert_id; |
126
|
|
|
|
127
|
|
|
return $id; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Update Mollie profile. |
132
|
|
|
* |
133
|
|
|
* @param Profile $profile Profile. |
134
|
|
|
* @param array<string> $data Data. |
135
|
|
|
* @param array<string> $format Format. |
136
|
|
|
* @return int The number of rows updated. |
137
|
|
|
* @throws \Exception Throws exception on error. |
138
|
|
|
*/ |
139
|
|
|
public function update_profile( Profile $profile, $data = array(), $format = array() ) { |
140
|
|
|
global $wpdb; |
141
|
|
|
|
142
|
|
|
$mollie_id = $profile->get_id(); |
143
|
|
|
|
144
|
|
|
if ( empty( $mollie_id ) ) { |
145
|
|
|
throw new \Exception( 'Can not update Mollie profile with empty ID.' ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$data['email'] = $profile->get_email(); |
149
|
|
|
$format['email'] = '%s'; |
150
|
|
|
|
151
|
|
|
$data['name'] = $profile->get_name(); |
152
|
|
|
$format['name'] = '%s'; |
153
|
|
|
|
154
|
|
|
$result = $wpdb->update( |
155
|
|
|
$wpdb->pronamic_pay_mollie_profiles, |
156
|
|
|
$data, |
157
|
|
|
array( |
158
|
|
|
'mollie_id' => $mollie_id, |
159
|
|
|
), |
160
|
|
|
$format, |
161
|
|
|
array( |
162
|
|
|
'mollie_id' => '%s', |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
if ( false === $result ) { |
167
|
|
|
throw new \Exception( |
168
|
|
|
sprintf( |
169
|
|
|
'Could not update Mollie profile ID: %s, error: %s.', |
170
|
|
|
$mollie_id, |
171
|
|
|
$wpdb->last_error |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $result; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Save Mollie profile. |
181
|
|
|
* |
182
|
|
|
* @param Profile $profile Profile. |
183
|
|
|
* @param array<string> $data Data. |
184
|
|
|
* @param array<string> $format Format. |
185
|
|
|
* @return int |
186
|
|
|
* @throws \Exception Throws exception if unable to update existing profile. |
187
|
|
|
*/ |
188
|
|
|
public function save_profile( Profile $profile, $data = array(), $format = array() ) { |
189
|
|
|
$profile_data = $this->get_profile( $profile ); |
190
|
|
|
|
191
|
|
|
if ( null !== $profile_data ) { |
192
|
|
|
if ( ! \property_exists( $profile_data, 'id' ) ) { |
193
|
|
|
throw new \Exception( 'Can not update Mollie profile without ID.' ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$this->update_profile( $profile, $data, $format ); |
197
|
|
|
|
198
|
|
|
return $profile_data->id; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->insert_profile( $profile, $data, $format ); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|