1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Payment Actions |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Payments |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Complete a donation |
19
|
|
|
* |
20
|
|
|
* Performs all necessary actions to complete a donation. |
21
|
|
|
* Triggered by the give_update_payment_status() function. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @param int $payment_id The ID number of the payment. |
26
|
|
|
* @param string $new_status The status of the payment, probably "publish". |
27
|
|
|
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
function give_complete_purchase( $payment_id, $new_status, $old_status ) { |
32
|
|
|
|
33
|
|
|
// Make sure that payments are only completed once. |
34
|
42 |
|
if ( $old_status == 'publish' || $old_status == 'complete' ) { |
|
|
|
|
35
|
6 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Make sure the payment completion is only processed when new status is complete. |
39
|
42 |
|
if ( $new_status != 'publish' && $new_status != 'complete' ) { |
|
|
|
|
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
42 |
|
$payment = new Give_Payment( $payment_id ); |
44
|
|
|
|
45
|
42 |
|
$creation_date = get_post_field( 'post_date', $payment_id, 'raw' ); |
46
|
42 |
|
$payment_meta = $payment->payment_meta; |
47
|
42 |
|
$completed_date = $payment->completed_date; |
48
|
42 |
|
$user_info = $payment->user_info; |
|
|
|
|
49
|
42 |
|
$donor_id = $payment->customer_id; |
50
|
42 |
|
$amount = $payment->total; |
51
|
42 |
|
$price_id = $payment->price_id; |
52
|
42 |
|
$form_id = $payment->form_id; |
53
|
|
|
|
54
|
42 |
|
/** |
55
|
|
|
* Fires before completing donation. |
56
|
|
|
* |
57
|
42 |
|
* @since 1.0 |
58
|
|
|
* |
59
|
42 |
|
* @param int $payment_id The ID of the payment. |
60
|
42 |
|
*/ |
61
|
|
|
do_action( 'give_pre_complete_donation', $payment_id ); |
62
|
42 |
|
|
63
|
|
|
// Ensure these actions only run once, ever. |
64
|
|
|
if ( empty( $completed_date ) ) { |
65
|
42 |
|
|
66
|
42 |
|
give_record_donation_in_log( $form_id, $payment_id, $price_id, $creation_date ); |
67
|
|
|
|
68
|
|
|
/** |
69
|
42 |
|
* Fires after logging donation record. |
70
|
|
|
* |
71
|
42 |
|
* @since 1.0 |
72
|
42 |
|
* |
73
|
|
|
* @param int $form_id The ID number of the form. |
74
|
|
|
* @param int $payment_id The ID number of the payment. |
75
|
42 |
|
* @param array $payment_meta The payment meta. |
76
|
42 |
|
*/ |
77
|
42 |
|
do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta ); |
78
|
|
|
|
79
|
42 |
|
} |
80
|
|
|
|
81
|
|
|
// Increase the earnings for this form ID. |
82
|
42 |
|
give_increase_earnings( $form_id, $amount, $payment_id ); |
83
|
|
|
give_increase_donation_count( $form_id ); |
84
|
|
|
|
85
|
42 |
|
// @todo: Refresh only range related stat cache |
86
|
42 |
|
give_delete_donation_stats(); |
87
|
42 |
|
|
88
|
42 |
|
// Increase the donor's donation stats. |
89
|
|
|
$donor = new Give_Donor( $donor_id ); |
90
|
42 |
|
$donor->increase_purchase_count(); |
91
|
|
|
$donor->increase_value( $amount ); |
92
|
|
|
|
93
|
|
|
give_increase_total_earnings( $amount ); |
94
|
|
|
|
95
|
|
|
// Ensure this action only runs once ever. |
96
|
|
|
if ( empty( $completed_date ) ) { |
97
|
|
|
|
98
|
|
|
// Save the completed date. |
99
|
|
|
$payment->completed_date = current_time( 'mysql' ); |
100
|
|
|
$payment->save(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Fires after a donation successfully complete. |
104
|
|
|
* |
105
|
|
|
* @since 1.0 |
106
|
|
|
* |
107
|
|
|
* @param int $payment_id The ID of the payment. |
108
|
|
|
*/ |
109
|
42 |
|
do_action( 'give_complete_donation', $payment_id ); |
110
|
42 |
|
} |
111
|
42 |
|
|
112
|
|
|
} |
113
|
42 |
|
|
114
|
|
|
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 ); |
115
|
42 |
|
|
116
|
42 |
|
|
117
|
|
|
/** |
118
|
42 |
|
* Record payment status change |
119
|
|
|
* |
120
|
42 |
|
* @since 1.0 |
121
|
42 |
|
* |
122
|
|
|
* @param int $payment_id The ID number of the payment. |
123
|
|
|
* @param string $new_status The status of the payment, probably "publish". |
124
|
|
|
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending". |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
function give_record_status_change( $payment_id, $new_status, $old_status ) { |
129
|
|
|
|
130
|
|
|
// Get the list of statuses so that status in the payment note can be translated. |
131
|
|
|
$stati = give_get_payment_statuses(); |
132
|
|
|
$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status; |
133
|
|
|
$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status; |
134
|
|
|
|
135
|
|
|
// translators: 1: old status 2: new status. |
136
|
|
|
$status_change = sprintf( esc_html__( 'Status changed from %1$s to %2$s.', 'give' ), $old_status, $new_status ); |
137
|
|
|
|
138
|
42 |
|
give_insert_payment_note( $payment_id, $status_change ); |
139
|
|
|
} |
140
|
42 |
|
|
141
|
41 |
|
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 ); |
142
|
41 |
|
|
143
|
|
|
|
144
|
42 |
|
/** |
145
|
|
|
* Update Old Payments Totals |
146
|
|
|
* |
147
|
|
|
* Updates all old payments, prior to 1.2, with new meta for the total donation amount. |
148
|
|
|
* |
149
|
|
|
* It's done to query payments by their totals. |
150
|
|
|
* |
151
|
|
|
* @since 1.0 |
152
|
|
|
* |
153
|
|
|
* @param array $data Arguments passed. |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
function give_update_old_payments_with_totals( $data ) { |
158
|
|
|
if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) { |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ( get_option( 'give_payment_totals_upgraded' ) ) { |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$payments = give_get_payments( array( |
167
|
|
|
'offset' => 0, |
168
|
|
|
'number' => - 1, |
169
|
|
|
'mode' => 'all', |
170
|
|
|
) ); |
171
|
|
|
|
172
|
|
|
if ( $payments ) { |
|
|
|
|
173
|
|
|
foreach ( $payments as $payment ) { |
174
|
|
|
|
175
|
|
|
$payment = new Give_Payment( $payment->ID ); |
176
|
|
|
$meta = $payment->get_meta(); |
177
|
|
|
|
178
|
|
|
$payment->total = $meta['amount']; |
179
|
|
|
$payment->save(); |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
add_option( 'give_payment_totals_upgraded', 1 ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' ); |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Mark Abandoned Donations |
191
|
|
|
* |
192
|
|
|
* Updates over a week-old 'pending' donations to 'abandoned' status. |
193
|
|
|
* |
194
|
|
|
* @since 1.0 |
195
|
|
|
* |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
function give_mark_abandoned_donations() { |
199
|
|
|
$args = array( |
200
|
|
|
'status' => 'pending', |
201
|
|
|
'number' => - 1, |
202
|
|
|
'output' => 'give_payments', |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
add_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
206
|
|
|
|
207
|
|
|
$payments = give_get_payments( $args ); |
208
|
|
|
|
209
|
|
|
remove_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
210
|
|
|
|
211
|
|
|
if ( $payments ) { |
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Filter payment gateways: Used to set payment gateways which can be skip while transferring pending payment to abandon. |
214
|
|
|
* |
215
|
|
|
* @since 1.6 |
216
|
|
|
* |
217
|
|
|
* @param array $skip_payment_gateways Array of payment gateways |
218
|
|
|
*/ |
219
|
|
|
$skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) ); |
220
|
|
|
|
221
|
|
|
/* @var Give_Payment $payment */ |
222
|
|
|
foreach ( $payments as $payment ) { |
223
|
|
|
$gateway = give_get_payment_gateway( $payment->ID ); |
224
|
|
|
|
225
|
|
|
// Skip payment gateways. |
226
|
|
|
if ( in_array( $gateway, $skip_payment_gateways ) ) { |
227
|
|
|
continue; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$payment->status = 'abandoned'; |
231
|
|
|
$payment->save(); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
Give_Cron::add_weekly_event( 'give_mark_abandoned_donations' ); |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Trigger the refresh of this month reports transients |
241
|
|
|
* |
242
|
|
|
* @since 1.7 |
243
|
|
|
* |
244
|
|
|
* @param int $payment_ID Payment ID. |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
function give_refresh_thismonth_stat_transients( $payment_ID ) { |
|
|
|
|
249
|
|
|
// Monthly stats. |
250
|
|
|
Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
251
|
|
|
|
252
|
|
|
// @todo: Refresh only range related stat cache |
253
|
|
|
give_delete_donation_stats(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
add_action( 'save_post_give_payment', 'give_refresh_thismonth_stat_transients' ); |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Add support to get all payment meta. |
261
|
|
|
* Note: only use for internal purpose |
262
|
|
|
* |
263
|
|
|
* @since 2.0 |
264
|
|
|
* |
265
|
|
|
* @param $check |
266
|
|
|
* @param $object_id |
267
|
|
|
* @param $meta_key |
268
|
|
|
* @param $single |
269
|
|
|
* |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
function give_bc_v20_get_payment_meta( $check, $object_id, $meta_key, $single ) { |
273
|
|
|
// Bailout. |
274
|
|
|
if ( |
275
|
|
|
'give_payment' !== get_post_type( $object_id ) || |
276
|
|
|
'_give_payment_meta' !== $meta_key || |
277
|
|
|
! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) |
278
|
|
|
) { |
279
|
|
|
return $check; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$cache_key = "_give_payment_meta_{$object_id}"; |
283
|
|
|
|
284
|
|
|
// Get already calculate payment meta from cache. |
285
|
|
|
$payment_meta = Give_Cache::get_db_query( $cache_key ); |
286
|
|
|
|
287
|
|
|
if ( is_null( $payment_meta ) ) { |
288
|
|
|
// Remove filter. |
289
|
|
|
remove_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999 ); |
290
|
|
|
|
291
|
|
|
$donation = new Give_Payment( $object_id ); |
292
|
|
|
|
293
|
|
|
// Get all payment meta. |
294
|
|
|
$payment_meta = give_get_meta( $object_id ); |
295
|
|
|
|
296
|
|
|
// Set default value to array. |
297
|
|
|
if ( empty( $payment_meta ) ) { |
298
|
|
|
return $check; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
// Convert all meta key value to string instead of array |
302
|
|
|
array_walk( $payment_meta, function ( &$meta, $key ) { |
|
|
|
|
303
|
|
|
$meta = current( $meta ); |
304
|
|
|
} ); |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Add backward compatibility to old meta keys. |
308
|
|
|
*/ |
309
|
|
|
// Donation key. |
310
|
|
|
$payment_meta['key'] = ! empty( $payment_meta['_give_payment_purchase_key'] ) ? $payment_meta['_give_payment_purchase_key'] : ''; |
311
|
|
|
|
312
|
|
|
// Donation form. |
313
|
|
|
$payment_meta['form_title'] = ! empty( $payment_meta['_give_payment_form_title'] ) ? $payment_meta['_give_payment_form_title'] : ''; |
314
|
|
|
|
315
|
|
|
// Donor email. |
316
|
|
|
$payment_meta['email'] = ! empty( $payment_meta['_give_payment_donor_email'] ) ? $payment_meta['_give_payment_donor_email'] : ''; |
317
|
|
|
$payment_meta['email'] = ! empty( $payment_meta['email'] ) ? |
318
|
|
|
$payment_meta['email'] : |
319
|
|
|
Give()->donors->get_column( 'email', $donation->donor_id ); |
320
|
|
|
|
321
|
|
|
// Form id. |
322
|
|
|
$payment_meta['form_id'] = ! empty( $payment_meta['_give_payment_form_id'] ) ? $payment_meta['_give_payment_form_id'] : ''; |
323
|
|
|
|
324
|
|
|
// Price id. |
325
|
|
|
$payment_meta['price_id'] = ! empty( $payment_meta['_give_payment_price_id'] ) ? $payment_meta['_give_payment_price_id'] : ''; |
326
|
|
|
|
327
|
|
|
// Date. |
328
|
|
|
$payment_meta['date'] = ! empty( $payment_meta['_give_payment_date'] ) ? $payment_meta['_give_payment_date'] : ''; |
329
|
|
|
$payment_meta['date'] = ! empty( $payment_meta['date'] ) ? |
330
|
|
|
$payment_meta['date'] : |
331
|
|
|
get_post_field( 'post_date', $object_id ); |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
334
|
|
|
// Currency. |
335
|
|
|
$payment_meta['currency'] = ! empty( $payment_meta['_give_payment_currency'] ) ? $payment_meta['_give_payment_currency'] : ''; |
336
|
|
|
|
337
|
|
|
// Decode donor data. |
338
|
|
|
$donor_id = ! empty( $payment_meta['_give_payment_donor_id'] ) ? $payment_meta['_give_payment_donor_id'] : 0; |
339
|
|
|
$donor = new Give_Donor( $donor_id ); |
340
|
|
|
|
341
|
|
|
// Donor first name. |
342
|
|
|
$donor_data['first_name'] = ! empty( $payment_meta['_give_donor_billing_first_name'] ) ? $payment_meta['_give_donor_billing_first_name'] : ''; |
343
|
|
|
$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ? |
344
|
|
|
$donor_data['first_name'] : |
345
|
|
|
$donor->get_first_name(); |
346
|
|
|
|
347
|
|
|
// Donor last name. |
348
|
|
|
$donor_data['last_name'] = ! empty( $payment_meta['_give_donor_billing_last_name'] ) ? $payment_meta['_give_donor_billing_last_name'] : ''; |
349
|
|
|
$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ? |
350
|
|
|
$donor_data['last_name'] : |
351
|
|
|
$donor->get_last_name(); |
352
|
|
|
|
353
|
|
|
// Donor email. |
354
|
|
|
$donor_data['email'] = $payment_meta['email']; |
355
|
|
|
|
356
|
|
|
// User ID. |
357
|
|
|
$donor_data['id'] = $donation->user_id; |
358
|
|
|
|
359
|
|
|
$donor_data['address'] = false; |
360
|
|
|
|
361
|
|
|
// Address1. |
362
|
|
|
$address1 = ! empty( $payment_meta['_give_donor_billing_address1'] ) ? $payment_meta['_give_donor_billing_address1'] : ''; |
363
|
|
|
if ( $address1 ) { |
364
|
|
|
$donor_data['address']['line1'] = $address1; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
// Address2. |
368
|
|
|
$address2 = ! empty( $payment_meta['_give_donor_billing_address2'] ) ? $payment_meta['_give_donor_billing_address2'] : ''; |
369
|
|
|
if ( $address2 ) { |
370
|
|
|
$donor_data['address']['line2'] = $address2; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// City. |
374
|
|
|
$city = ! empty( $payment_meta['_give_donor_billing_city'] ) ? $payment_meta['_give_donor_billing_city'] : ''; |
375
|
|
|
if ( $city ) { |
376
|
|
|
$donor_data['address']['city'] = $city; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
// Zip. |
380
|
|
|
$zip = ! empty( $payment_meta['_give_donor_billing_zip'] ) ? $payment_meta['_give_donor_billing_zip'] : ''; |
381
|
|
|
if ( $zip ) { |
382
|
|
|
$donor_data['address']['zip'] = $zip; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
// State. |
386
|
|
|
$state = ! empty( $payment_meta['_give_donor_billing_state'] ) ? $payment_meta['_give_donor_billing_state'] : ''; |
387
|
|
|
if ( $state ) { |
388
|
|
|
$donor_data['address']['state'] = $state; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
// Country. |
392
|
|
|
$country = ! empty( $payment_meta['_give_donor_billing_country'] ) ? $payment_meta['_give_donor_billing_country'] : ''; |
393
|
|
|
if ( $country ) { |
394
|
|
|
$donor_data['address']['country'] = $country; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$payment_meta['user_info'] = $donor_data; |
398
|
|
|
|
399
|
|
|
// Add filter |
400
|
|
|
add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 ); |
401
|
|
|
|
402
|
|
|
// Set custom meta key into payment meta. |
403
|
|
|
if ( ! empty( $payment_meta['_give_payment_meta'] ) ) { |
404
|
|
|
$payment_meta = array_merge( maybe_unserialize( $payment_meta['_give_payment_meta'] ), $payment_meta ); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
// Set cache. |
408
|
|
|
Give_Cache::set_db_query( $cache_key, $payment_meta ); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
if ( $single ) { |
412
|
|
|
/** |
413
|
|
|
* Filter the payment meta |
414
|
|
|
* Add custom meta key to payment meta |
415
|
|
|
* |
416
|
|
|
* @since 2.0 |
417
|
|
|
*/ |
418
|
|
|
$new_payment_meta[0] = apply_filters( 'give_get_payment_meta', $payment_meta, $object_id, $meta_key ); |
419
|
|
|
|
420
|
|
|
$payment_meta = $new_payment_meta; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
return $payment_meta; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 ); |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Add meta in payment that store page id and page url. |
430
|
|
|
* |
431
|
|
|
* Will add/update when user add click on the checkout page. |
432
|
|
|
* The status of the donation doest not matter as it get change when user had made the payment successfully. |
433
|
|
|
* |
434
|
|
|
* @since 1.8.13 |
435
|
|
|
* |
436
|
|
|
* @param int $payment_id Payment id for which the meta value should be updated. |
437
|
|
|
*/ |
438
|
|
|
function give_payment_save_page_data( $payment_id ) { |
439
|
|
|
$page_url = ( ! empty( $_REQUEST['give-current-url'] ) ? esc_url( $_REQUEST['give-current-url'] ) : false ); |
|
|
|
|
440
|
|
|
|
441
|
|
|
// Check $page_url is not empty. |
442
|
|
|
if ( $page_url ) { |
443
|
|
|
update_post_meta( $payment_id, '_give_current_url', $page_url ); |
444
|
|
|
$page_id = url_to_postid( $page_url ); |
445
|
|
|
// Check $page_id is not empty. |
446
|
|
|
if ( $page_id ) { |
447
|
|
|
update_post_meta( $payment_id, '_give_current_page_id', $page_id ); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
// Fire when payment is save. |
453
|
|
|
add_action( 'give_insert_payment', 'give_payment_save_page_data' ); |