1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Payments Data Store Custom Post Type |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2018 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Payments |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Payments; |
12
|
|
|
|
13
|
|
|
use DateTimeZone; |
14
|
|
|
use Pronamic\WordPress\Pay\AbstractDataStoreCPT; |
15
|
|
|
use Pronamic\WordPress\Pay\DateTime; |
16
|
|
|
use Pronamic\WordPress\Pay\Core\Statuses; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Title: Payments data store CPT |
20
|
|
|
* Description: |
21
|
|
|
* Copyright: Copyright (c) 2005 - 2018 |
22
|
|
|
* Company: Pronamic |
23
|
|
|
* |
24
|
|
|
* @see https://woocommerce.com/2017/04/woocommerce-3-0-release/ |
25
|
|
|
* @see https://woocommerce.wordpress.com/2016/10/27/the-new-crud-classes-in-woocommerce-2-7/ |
26
|
|
|
* @author Remco Tolsma |
27
|
|
|
* @version 3.7.0 |
28
|
|
|
* @since 3.7.0 |
29
|
|
|
*/ |
30
|
|
|
class PaymentsDataStoreCPT extends AbstractDataStoreCPT { |
31
|
|
|
/** |
32
|
|
|
* Construct payments data store CPT object. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() { |
35
|
|
|
$this->meta_key_prefix = '_pronamic_payment_'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create payment. |
40
|
|
|
* |
41
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/abstract-wc-order-data-store-cpt.php#L47-L76 |
42
|
|
|
* |
43
|
|
|
* @param Payment $payment The payment to create in this data store. |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function create( Payment $payment ) { |
48
|
|
|
$title = $payment->title; |
49
|
|
|
|
50
|
|
|
if ( empty( $title ) ) { |
51
|
|
|
$title = sprintf( |
52
|
|
|
'Payment – %s', |
53
|
|
|
date_i18n( _x( '@todo', 'Payment title date format parsed by `date_i18n`.', 'pronamic_ideal' ) ) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$result = wp_insert_post( |
58
|
|
|
array( |
59
|
|
|
'post_type' => 'pronamic_payment', |
60
|
|
|
'post_date_gmt' => $payment->date->format( 'Y-m-d H:i:s' ), |
61
|
|
|
'post_title' => $title, |
62
|
|
|
'post_status' => $this->get_post_status( $payment->status ), |
63
|
|
|
'post_author' => $payment->user_id, |
64
|
|
|
), true |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if ( is_wp_error( $result ) ) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$payment->set_id( $result ); |
72
|
|
|
$payment->post = get_post( $result ); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->update_post_meta( $payment ); |
75
|
|
|
|
76
|
|
|
do_action( 'pronamic_pay_new_payment', $payment ); |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Read payment. |
83
|
|
|
* |
84
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/abstracts/abstract-wc-order.php#L85-L111 |
85
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/abstract-wc-order-data-store-cpt.php#L78-L111 |
86
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L81-L136 |
87
|
|
|
* @see https://developer.wordpress.org/reference/functions/get_post/ |
88
|
|
|
* @see https://developer.wordpress.org/reference/classes/wp_post/ |
89
|
|
|
* |
90
|
|
|
* @param Payment $payment The payment to read from this data store. |
91
|
|
|
*/ |
92
|
|
|
public function read( Payment $payment ) { |
93
|
|
|
$payment->post = get_post( $payment->get_id() ); |
94
|
|
|
$payment->title = get_the_title( $payment->get_id() ); |
95
|
|
|
$payment->date = new DateTime( get_post_field( 'post_date_gmt', $payment->get_id(), 'raw' ), new DateTimeZone( 'UTC' ) ); |
96
|
|
|
$payment->user_id = get_post_field( 'post_author', $payment->get_id(), 'raw' ); |
97
|
|
|
|
98
|
|
|
$this->read_post_meta( $payment ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Update payment. |
103
|
|
|
* |
104
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/abstract-wc-order-data-store-cpt.php#L113-L154 |
105
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L154-L257 |
106
|
|
|
* @param Payment $payment The payment to update in this data store. |
107
|
|
|
*/ |
108
|
|
|
public function update( Payment $payment ) { |
109
|
|
|
$data = array( |
110
|
|
|
'ID' => $payment->get_id(), |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$post_status = $this->get_post_status( $payment->status, null ); |
114
|
|
|
|
115
|
|
|
if ( null !== $post_status ) { |
|
|
|
|
116
|
|
|
$data['post_status'] = $post_status; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
wp_update_post( $data ); |
120
|
|
|
|
121
|
|
|
$this->update_post_meta( $payment ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get post status. |
126
|
|
|
* |
127
|
|
|
* @param string $meta_status The payment to get a WordPress post status for. |
128
|
|
|
* @param string $default The default WordPress post status to return. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function get_post_status( $meta_status, $default = 'payment_pending' ) { |
133
|
|
|
switch ( $meta_status ) { |
134
|
|
|
case Statuses::CANCELLED: |
135
|
|
|
return 'payment_cancelled'; |
136
|
|
|
case Statuses::EXPIRED: |
137
|
|
|
return 'payment_expired'; |
138
|
|
|
case Statuses::FAILURE: |
139
|
|
|
return 'payment_failed'; |
140
|
|
|
case Statuses::SUCCESS: |
141
|
|
|
return 'payment_completed'; |
142
|
|
|
case Statuses::OPEN: |
143
|
|
|
return 'payment_pending'; |
144
|
|
|
default: |
145
|
|
|
return $default; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get meta status label. |
151
|
|
|
* |
152
|
|
|
* @param string $meta_status The subscription meta status to get the status label for. |
153
|
|
|
* @return string|boolean |
154
|
|
|
*/ |
155
|
|
|
public function get_meta_status_label( $meta_status ) { |
156
|
|
|
$post_status = $this->get_post_status( $meta_status, null ); |
157
|
|
|
|
158
|
|
|
if ( empty( $post_status ) ) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$status_object = get_post_status_object( $post_status ); |
163
|
|
|
|
164
|
|
|
if ( isset( $status_object, $status_object->label ) ) { |
165
|
|
|
return $status_object->label; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Read post meta. |
173
|
|
|
* |
174
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/abstracts/abstract-wc-data.php#L462-L507 |
175
|
|
|
* @param Payment $payment The payment to read. |
176
|
|
|
*/ |
177
|
|
|
private function read_post_meta( $payment ) { |
178
|
|
|
$id = $payment->get_id(); |
179
|
|
|
|
180
|
|
|
$payment->config_id = $this->get_meta( $id, 'config_id' ); |
181
|
|
|
$payment->key = $this->get_meta( $id, 'key' ); |
182
|
|
|
$payment->amount = (float) $this->get_meta( $id, 'amount' ); |
183
|
|
|
$payment->currency = $this->get_meta( $id, 'currency' ); |
184
|
|
|
$payment->method = $this->get_meta( $id, 'method' ); |
185
|
|
|
$payment->issuer = $this->get_meta( $id, 'issuer' ); |
186
|
|
|
$payment->order_id = $this->get_meta( $id, 'order_id' ); |
187
|
|
|
$payment->transaction_id = $this->get_meta( $id, 'transaction_id' ); |
188
|
|
|
$payment->entrance_code = $this->get_meta( $id, 'entrance_code' ); |
189
|
|
|
$payment->action_url = $this->get_meta( $id, 'action_url' ); |
190
|
|
|
$payment->source = $this->get_meta( $id, 'source' ); |
191
|
|
|
$payment->source_id = $this->get_meta( $id, 'source_id' ); |
192
|
|
|
$payment->description = $this->get_meta( $id, 'description' ); |
193
|
|
|
$payment->language = $this->get_meta( $id, 'language' ); |
194
|
|
|
$payment->locale = $this->get_meta( $id, 'locale' ); |
195
|
|
|
$payment->email = $this->get_meta( $id, 'email' ); |
196
|
|
|
$payment->status = $this->get_meta( $id, 'status' ); |
197
|
|
|
$payment->customer_name = $this->get_meta( $id, 'customer_name' ); |
198
|
|
|
$payment->address = $this->get_meta( $id, 'address' ); |
199
|
|
|
$payment->zip = $this->get_meta( $id, 'zip' ); |
200
|
|
|
$payment->city = $this->get_meta( $id, 'city' ); |
201
|
|
|
$payment->country = $this->get_meta( $id, 'country' ); |
202
|
|
|
$payment->telephone_number = $this->get_meta( $id, 'telephone_number' ); |
203
|
|
|
$payment->analytics_client_id = $this->get_meta( $id, 'analytics_client_id' ); |
204
|
|
|
$payment->subscription_id = $this->get_meta( $id, 'subscription_id' ); |
205
|
|
|
$payment->recurring_type = $this->get_meta( $id, 'recurring_type' ); |
206
|
|
|
$payment->recurring = $this->get_meta( $id, 'recurring' ); |
207
|
|
|
$payment->start_date = $this->get_meta_date( $id, 'start_date' ); |
208
|
|
|
$payment->end_date = $this->get_meta_date( $id, 'end_date' ); |
209
|
|
|
$payment->user_agent = $this->get_meta( $id, 'user_agent' ); |
210
|
|
|
$payment->user_ip = $this->get_meta( $id, 'user_ip' ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Update payment post meta. |
215
|
|
|
* |
216
|
|
|
* @see https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L154-L257 |
217
|
|
|
* @param Payment $payment The payment to update. |
218
|
|
|
*/ |
219
|
|
|
private function update_post_meta( $payment ) { |
220
|
|
|
$id = $payment->get_id(); |
221
|
|
|
|
222
|
|
|
$this->update_meta( $id, 'config_id', $payment->config_id ); |
223
|
|
|
$this->update_meta( $id, 'key', $payment->key ); |
224
|
|
|
$this->update_meta( $id, 'order_id', $payment->order_id ); |
225
|
|
|
$this->update_meta( $id, 'currency', $payment->currency ); |
226
|
|
|
$this->update_meta( $id, 'amount', $payment->amount ); |
227
|
|
|
$this->update_meta( $id, 'method', $payment->method ); |
228
|
|
|
$this->update_meta( $id, 'issuer', $payment->issuer ); |
229
|
|
|
$this->update_meta( $id, 'expiration_period', null ); |
230
|
|
|
$this->update_meta( $id, 'language', $payment->language ); |
231
|
|
|
$this->update_meta( $id, 'locale', $payment->locale ); |
232
|
|
|
$this->update_meta( $id, 'entrance_code', $payment->entrance_code ); |
233
|
|
|
$this->update_meta( $id, 'description', $payment->description ); |
234
|
|
|
$this->update_meta( $id, 'first_name', $payment->first_name ); |
235
|
|
|
$this->update_meta( $id, 'last_name', $payment->last_name ); |
236
|
|
|
$this->update_meta( $id, 'consumer_name', $payment->consumer_name ); |
237
|
|
|
$this->update_meta( $id, 'consumer_account_number', $payment->consumer_account_number ); |
238
|
|
|
$this->update_meta( $id, 'consumer_iban', $payment->consumer_iban ); |
239
|
|
|
$this->update_meta( $id, 'consumer_bic', $payment->consumer_bic ); |
240
|
|
|
$this->update_meta( $id, 'consumer_city', $payment->consumer_city ); |
241
|
|
|
$this->update_meta( $id, 'source', $payment->source ); |
242
|
|
|
$this->update_meta( $id, 'source_id', $payment->source_id ); |
243
|
|
|
$this->update_meta( $id, 'email', $payment->email ); |
244
|
|
|
$this->update_meta( $id, 'customer_name', $payment->customer_name ); |
245
|
|
|
$this->update_meta( $id, 'address', $payment->address ); |
246
|
|
|
$this->update_meta( $id, 'zip', $payment->zip ); |
247
|
|
|
$this->update_meta( $id, 'city', $payment->city ); |
248
|
|
|
$this->update_meta( $id, 'country', $payment->country ); |
249
|
|
|
$this->update_meta( $id, 'telephone_number', $payment->telephone_number ); |
250
|
|
|
$this->update_meta( $id, 'analytics_client_id', $payment->analytics_client_id ); |
251
|
|
|
$this->update_meta( $id, 'subscription_id', $payment->subscription_id ); |
252
|
|
|
$this->update_meta( $id, 'recurring_type', $payment->recurring_type ); |
253
|
|
|
$this->update_meta( $id, 'recurring', $payment->recurring ); |
254
|
|
|
$this->update_meta( $id, 'transaction_id', $payment->get_transaction_id() ); |
255
|
|
|
$this->update_meta( $id, 'action_url', $payment->get_action_url() ); |
256
|
|
|
$this->update_meta( $id, 'start_date', $payment->start_date ); |
257
|
|
|
$this->update_meta( $id, 'end_date', $payment->end_date ); |
258
|
|
|
$this->update_meta( $id, 'user_agent', $payment->user_agent ); |
259
|
|
|
$this->update_meta( $id, 'user_ip', $payment->user_ip ); |
260
|
|
|
|
261
|
|
|
$this->update_meta_status( $payment ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Update meta status. |
266
|
|
|
* |
267
|
|
|
* @param Payment $payment The payment to update the status for. |
268
|
|
|
*/ |
269
|
|
|
public function update_meta_status( $payment ) { |
270
|
|
|
$id = $payment->get_id(); |
271
|
|
|
|
272
|
|
|
$previous_status = $this->get_meta( $id, 'status' ); |
273
|
|
|
|
274
|
|
|
$this->update_meta( $id, 'status', $payment->status ); |
275
|
|
|
|
276
|
|
|
if ( $previous_status !== $payment->status ) { |
277
|
|
|
$old = $previous_status; |
278
|
|
|
$old = strtolower( $old ); |
279
|
|
|
$old = empty( $old ) ? 'unknown' : $old; |
280
|
|
|
|
281
|
|
|
$new = $payment->status; |
282
|
|
|
$new = strtolower( $new ); |
283
|
|
|
$new = empty( $new ) ? 'unknown' : $new; |
284
|
|
|
|
285
|
|
|
$can_redirect = false; |
286
|
|
|
|
287
|
|
|
do_action( 'pronamic_payment_status_update_' . $payment->source . '_' . $old . '_to_' . $new, $payment, $can_redirect, $previous_status, $payment->status ); |
288
|
|
|
do_action( 'pronamic_payment_status_update_' . $payment->source, $payment, $can_redirect, $previous_status, $payment->status ); |
289
|
|
|
do_action( 'pronamic_payment_status_update', $payment, $can_redirect, $previous_status, $payment->status ); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|