|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Payment Actions |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Payments |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php 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 purchase aka donation |
|
19
|
|
|
* |
|
20
|
|
|
* Performs all necessary actions to complete a purchase. |
|
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
|
34 |
|
if ( $old_status == 'publish' || $old_status == 'complete' ) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Make sure the payment completion is only processed when new status is complete |
|
39
|
34 |
|
if ( $new_status != 'publish' && $new_status != 'complete' ) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
34 |
|
|
|
44
|
34 |
|
$payment = new Give_Payment( $payment_id ); |
|
45
|
34 |
|
|
|
46
|
34 |
|
$creation_date = get_post_field( 'post_date', $payment_id, 'raw' ); |
|
47
|
34 |
|
$payment_meta = $payment->payment_meta; |
|
|
|
|
|
|
48
|
34 |
|
$completed_date = $payment->completed_date; |
|
|
|
|
|
|
49
|
|
|
$user_info = $payment->user_info; |
|
|
|
|
|
|
50
|
34 |
|
$customer_id = $payment->customer_id; |
|
|
|
|
|
|
51
|
|
|
$amount = $payment->total; |
|
|
|
|
|
|
52
|
34 |
|
$price_id = $payment->price_id; |
|
|
|
|
|
|
53
|
|
|
$form_id = $payment->form_id; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
34 |
|
do_action( 'give_pre_complete_purchase', $payment_id ); |
|
56
|
|
|
|
|
57
|
34 |
|
// Ensure these actions only run once, ever |
|
58
|
|
|
if ( empty( $completed_date ) ) { |
|
59
|
|
|
|
|
60
|
|
|
give_record_sale_in_log( $form_id, $payment_id, $price_id, $creation_date ); |
|
61
|
|
|
do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta ); |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
34 |
|
// Increase the earnings for this form ID |
|
66
|
34 |
|
give_increase_earnings( $form_id, $amount ); |
|
67
|
|
|
give_increase_purchase_count( $form_id ); |
|
68
|
|
|
|
|
69
|
|
|
// Clear the total earnings cache |
|
70
|
34 |
|
delete_transient( 'give_earnings_total' ); |
|
71
|
|
|
// Clear the This Month earnings (this_monththis_month is NOT a typo) |
|
72
|
34 |
|
delete_transient( md5( 'give_earnings_this_monththis_month' ) ); |
|
73
|
34 |
|
delete_transient( md5( 'give_earnings_todaytoday' ) ); |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
// Increase the donor's purchase stats |
|
77
|
34 |
|
$customer = new Give_Customer( $customer_id ); |
|
78
|
|
|
$customer->increase_purchase_count(); |
|
79
|
34 |
|
$customer->increase_value( $amount ); |
|
80
|
|
|
|
|
81
|
|
|
give_increase_total_earnings( $amount ); |
|
82
|
34 |
|
|
|
83
|
|
|
// Ensure this action only runs once ever |
|
84
|
|
|
if ( empty( $completed_date ) ) { |
|
85
|
34 |
|
|
|
86
|
|
|
// Save the completed date |
|
87
|
34 |
|
$payment->completed_date = current_time( 'mysql' ); |
|
|
|
|
|
|
88
|
34 |
|
$payment->save(); |
|
89
|
|
|
do_action( 'give_complete_purchase', $payment_id ); |
|
90
|
34 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 ); |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Record payment status change |
|
99
|
|
|
* |
|
100
|
|
|
* @since 1.0 |
|
101
|
|
|
* |
|
102
|
|
|
* @param int $payment_id the ID number of the payment |
|
103
|
|
|
* @param string $new_status the status of the payment, probably "publish" |
|
104
|
|
|
* @param string $old_status the status of the payment prior to being marked as "complete", probably "pending" |
|
105
|
|
|
* |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
function give_record_status_change( $payment_id, $new_status, $old_status ) { |
|
109
|
34 |
|
|
|
110
|
34 |
|
// Get the list of statuses so that status in the payment note can be translated |
|
111
|
34 |
|
$stati = give_get_payment_statuses(); |
|
112
|
|
|
$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status; |
|
113
|
34 |
|
$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status; |
|
114
|
|
|
|
|
115
|
34 |
|
$status_change = sprintf( __( 'Status changed from %s to %s', 'give' ), $old_status, $new_status ); |
|
116
|
34 |
|
|
|
117
|
|
|
give_insert_payment_note( $payment_id, $status_change ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 ); |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Flushes the current user's purchase history transient when a payment status |
|
125
|
|
|
* is updated |
|
126
|
|
|
* |
|
127
|
|
|
* @since 1.0 |
|
128
|
|
|
* |
|
129
|
|
|
* @param $payment_id |
|
130
|
|
|
* @param $new_status the status of the payment, probably "publish" |
|
131
|
|
|
* @param $old_status the status of the payment prior to being marked as "complete", probably "pending" |
|
132
|
|
|
*/ |
|
133
|
34 |
|
function give_clear_user_history_cache( $payment_id, $new_status, $old_status ) { |
|
|
|
|
|
|
134
|
34 |
|
|
|
135
|
|
|
$payment = new Give_Payment( $payment_id ); |
|
136
|
|
|
|
|
137
|
|
|
if ( ! empty( $payment->user_id ) ) { |
|
|
|
|
|
|
138
|
|
|
delete_transient( 'give_user_' . $payment->user_id . '_purchases' ); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
add_action( 'give_update_payment_status', 'give_clear_user_history_cache', 10, 3 ); |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Updates all old payments, prior to 1.2, with new |
|
147
|
|
|
* meta for the total purchase amount |
|
148
|
|
|
* |
|
149
|
|
|
* This is so that payments can be queried 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
|
34 |
|
$payment->total = $meta['amount']; |
|
|
|
|
|
|
179
|
|
|
$payment->save(); |
|
180
|
34 |
|
|
|
181
|
34 |
|
} |
|
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
|
|
|
* Updates week-old+ 'pending' orders to 'abandoned' |
|
191
|
|
|
* |
|
192
|
|
|
* @since 1.0 |
|
193
|
|
|
* @return void |
|
194
|
|
|
*/ |
|
195
|
|
|
function give_mark_abandoned_donations() { |
|
196
|
|
|
$args = array( |
|
197
|
|
|
'status' => 'pending', |
|
198
|
|
|
'number' => - 1, |
|
199
|
|
|
'output' => 'give_payments', |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
add_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
|
203
|
|
|
|
|
204
|
|
|
$payments = give_get_payments( $args ); |
|
205
|
|
|
|
|
206
|
|
|
remove_filter( 'posts_where', 'give_filter_where_older_than_week' ); |
|
207
|
|
|
|
|
208
|
|
|
if ( $payments ) { |
|
209
|
|
|
foreach ( $payments as $payment ) { |
|
210
|
|
|
$gateway = give_get_payment_gateway( $payment ); |
|
211
|
|
|
//Skip offline gateway payments |
|
212
|
|
|
if ( $gateway == 'offline' ) { |
|
213
|
|
|
continue; |
|
214
|
|
|
} |
|
215
|
|
|
$payment->status = 'abandoned'; |
|
216
|
|
|
$payment->save(); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
add_action( 'give_weekly_scheduled_events', 'give_mark_abandoned_donations' ); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.