Complex classes like Give_Payment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Payment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class Give_Payment { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The Payment we are working with |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | * @access private |
||
| 33 | * @since 1.0 |
||
| 34 | */ |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The Payment ID |
||
| 38 | * |
||
| 39 | * @since 1.5 |
||
| 40 | * @var integer |
||
| 41 | */ |
||
| 42 | public $ID = 0; |
||
| 43 | protected $_ID = 0; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Identify if the payment is a new one or existing |
||
| 47 | * |
||
| 48 | * @since 1.5 |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $new = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The Payment number (for use with sequential payments) |
||
| 55 | * |
||
| 56 | * @since 1.5 |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $number = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The Gateway mode the payment was made in |
||
| 63 | * |
||
| 64 | * @since 1.5 |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $mode = 'live'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The Unique Payment Key |
||
| 71 | * |
||
| 72 | * @since 1.5 |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $key = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The Donation Form Title |
||
| 79 | * |
||
| 80 | * @since 1.5 |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $form_title = 0; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The Donation Form ID |
||
| 87 | * |
||
| 88 | * @since 1.5 |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $form_id = 0; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The Donation Form Price ID |
||
| 95 | * |
||
| 96 | * @since 1.5 |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $price_id = 0; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The total amount the payment is for |
||
| 103 | * Includes donation amount and fees |
||
| 104 | * |
||
| 105 | * @since 1.5 |
||
| 106 | * @var float |
||
| 107 | */ |
||
| 108 | protected $total = 0.00; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The Subtotal fo the payment before fees |
||
| 112 | * |
||
| 113 | * @since 1.5 |
||
| 114 | * @var float |
||
| 115 | */ |
||
| 116 | protected $subtotal = 0; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Array of global fees for this payment |
||
| 120 | * |
||
| 121 | * @since 1.5 |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $fees = array(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The sum of the fee amounts |
||
| 128 | * |
||
| 129 | * @since 1.5 |
||
| 130 | * @var float |
||
| 131 | */ |
||
| 132 | protected $fees_total = 0; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The date the payment was created |
||
| 136 | * |
||
| 137 | * @since 1.5 |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $date = ''; |
||
| 141 | protected $post_date = ''; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * The date the payment was marked as 'complete' |
||
| 145 | * |
||
| 146 | * @since 1.5 |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $completed_date = ''; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The status of the payment |
||
| 153 | * |
||
| 154 | * @since 1.5 |
||
| 155 | * @var string |
||
| 156 | */ |
||
| 157 | protected $status = 'pending'; |
||
| 158 | protected $post_status = 'pending'; // Same as $status but here for backwards compat |
||
| 159 | |||
| 160 | /** |
||
| 161 | * When updating, the old status prior to the change |
||
| 162 | * |
||
| 163 | * @since 1.5 |
||
| 164 | * @var string |
||
| 165 | */ |
||
| 166 | protected $old_status = ''; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * The display name of the current payment status |
||
| 170 | * |
||
| 171 | * @since 1.5 |
||
| 172 | * @var string |
||
| 173 | */ |
||
| 174 | protected $status_nicename = ''; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * The customer ID that made the payment |
||
| 178 | * |
||
| 179 | * @since 1.5 |
||
| 180 | * @var integer |
||
| 181 | */ |
||
| 182 | protected $customer_id = null; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * The User ID (if logged in) that made the payment |
||
| 186 | * |
||
| 187 | * @since 1.5 |
||
| 188 | * @var integer |
||
| 189 | */ |
||
| 190 | protected $user_id = 0; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * The first name of the payee |
||
| 194 | * |
||
| 195 | * @since 1.5 |
||
| 196 | * @var string |
||
| 197 | */ |
||
| 198 | protected $first_name = ''; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The last name of the payee |
||
| 202 | * |
||
| 203 | * @since 1.5 |
||
| 204 | * @var string |
||
| 205 | */ |
||
| 206 | protected $last_name = ''; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * The email used for the payment |
||
| 210 | * |
||
| 211 | * @since 1.5 |
||
| 212 | * @var string |
||
| 213 | */ |
||
| 214 | protected $email = ''; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Legacy (not to be accessed) array of user information |
||
| 218 | * |
||
| 219 | * @since 1.5 |
||
| 220 | * @var array |
||
| 221 | */ |
||
| 222 | private $user_info = array(); |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Legacy (not to be accessed) payment meta array |
||
| 226 | * |
||
| 227 | * @since 1.5 |
||
| 228 | * @var array |
||
| 229 | */ |
||
| 230 | private $payment_meta = array(); |
||
| 231 | |||
| 232 | /** |
||
| 233 | * The physical address used for the payment if provided |
||
| 234 | * |
||
| 235 | * @since 1.5 |
||
| 236 | * @var array |
||
| 237 | */ |
||
| 238 | protected $address = array(); |
||
| 239 | |||
| 240 | /** |
||
| 241 | * The transaction ID returned by the gateway |
||
| 242 | * |
||
| 243 | * @since 1.5 |
||
| 244 | * @var string |
||
| 245 | */ |
||
| 246 | protected $transaction_id = ''; |
||
| 247 | |||
| 248 | /** |
||
| 249 | * IP Address payment was made from |
||
| 250 | * |
||
| 251 | * @since 1.5 |
||
| 252 | * @var string |
||
| 253 | */ |
||
| 254 | protected $ip = ''; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * The gateway used to process the payment |
||
| 258 | * |
||
| 259 | * @since 1.5 |
||
| 260 | * @var string |
||
| 261 | */ |
||
| 262 | protected $gateway = ''; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * The the payment was made with |
||
| 266 | * |
||
| 267 | * @since 1.5 |
||
| 268 | * @var string |
||
| 269 | */ |
||
| 270 | protected $currency = ''; |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Array of items that have changed since the last save() was run |
||
| 274 | * This is for internal use, to allow fewer update_payment_meta calls to be run |
||
| 275 | * |
||
| 276 | * @since 1.5 |
||
| 277 | * @var array |
||
| 278 | */ |
||
| 279 | private $pending; |
||
| 280 | |||
| 281 | /** |
||
| 282 | * The parent payment (if applicable) |
||
| 283 | * |
||
| 284 | * @since 1.5 |
||
| 285 | * @var integer |
||
| 286 | */ |
||
| 287 | protected $parent_payment = 0; |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Setup the Give Payments class |
||
| 291 | * |
||
| 292 | * @since 1.0 |
||
| 293 | * |
||
| 294 | * @param int $payment_id A given payment |
||
| 295 | * |
||
| 296 | * @return mixed void|false |
||
| 297 | */ |
||
| 298 | 52 | public function __construct( $payment_id = false ) { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Magic GET function |
||
| 309 | * |
||
| 310 | * @since 1.5 |
||
| 311 | * |
||
| 312 | * @param string $key The property |
||
| 313 | * |
||
| 314 | * @return mixed The value |
||
| 315 | */ |
||
| 316 | 52 | public function __get( $key ) { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Magic SET function |
||
| 333 | * |
||
| 334 | * Sets up the pending array for the save method |
||
| 335 | * |
||
| 336 | * @since 1.5 |
||
| 337 | * |
||
| 338 | * @param string $key The property name |
||
| 339 | * @param mixed $value The value of the property |
||
| 340 | */ |
||
| 341 | 52 | public function __set( $key, $value ) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Magic ISSET function, which allows empty checks on protected elements |
||
| 359 | * |
||
| 360 | * @since 1.5 |
||
| 361 | * |
||
| 362 | * @param string $name The attribute to get |
||
| 363 | * |
||
| 364 | * @return boolean If the item is set or not |
||
| 365 | */ |
||
| 366 | 42 | public function __isset( $name ) { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Setup payment properties |
||
| 376 | * |
||
| 377 | * @since 1.5 |
||
| 378 | * |
||
| 379 | * @param int $payment_id The payment ID |
||
| 380 | * |
||
| 381 | * @return bool If the setup was successful or not |
||
| 382 | */ |
||
| 383 | 52 | private function setup_payment( $payment_id ) { |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Payment class object is storing various meta value in object parameter. |
||
| 462 | * So if user is updating payment meta but not updating payment object, then payment meta values will not reflect/changes on payment meta automatically |
||
| 463 | * and you can still access payment meta old value in any old payment object ( previously created ) which can cause to show or save wrong payment data. |
||
| 464 | * To prevent that user can use this function after updating any payment meta value ( in bulk or single update ). |
||
| 465 | * |
||
| 466 | 52 | * @since 1.6 |
|
| 467 | * @access public |
||
| 468 | * |
||
| 469 | 52 | * @param int $payment_id Payment ID. |
|
| 470 | 52 | * |
|
| 471 | 31 | * @return void |
|
| 472 | 52 | */ |
|
| 473 | public function update_payment_setup( $payment_id ){ |
||
| 474 | 22 | $this->setup_payment( $payment_id ); |
|
| 475 | 21 | } |
|
| 476 | 21 | ||
| 477 | /** |
||
| 478 | * Create the base of a payment. |
||
| 479 | 52 | * |
|
| 480 | * @since 1.5 |
||
| 481 | 1 | * @return int|bool False on failure, the payment ID on success. |
|
| 482 | 1 | */ |
|
| 483 | 1 | private function insert_payment() { |
|
| 589 | 52 | ||
| 590 | /** |
||
| 591 | * Save |
||
| 592 | 52 | * |
|
| 593 | * Once items have been set, an update is needed to save them to the database. |
||
| 594 | * |
||
| 595 | 52 | * @return bool True of the save occurred, false if it failed or wasn't needed |
|
| 596 | */ |
||
| 597 | public function save() { |
||
| 875 | 52 | ||
| 876 | /** |
||
| 877 | * Add a donation to a given payment |
||
| 878 | * |
||
| 879 | * @since 1.5 |
||
| 880 | * |
||
| 881 | 52 | * @param int $form_id The donation form to add |
|
| 882 | 52 | * @param array $args Other arguments to pass to the function |
|
| 883 | 52 | * @param array $options List of donation options |
|
| 884 | 52 | * |
|
| 885 | * @return bool True when successful, false otherwise |
||
| 886 | 52 | */ |
|
| 887 | public function add_donation( $form_id = 0, $args = array(), $options = array() ) { |
||
| 970 | 2 | ||
| 971 | 2 | /** |
|
| 972 | 2 | * Remove a donation from the payment |
|
| 973 | * |
||
| 974 | 2 | * @since 1.5 |
|
| 975 | * |
||
| 976 | * @param int $form_id The form ID to remove |
||
| 977 | 2 | * @param array $args Arguments to pass to identify (quantity, amount, price_id) |
|
| 978 | * |
||
| 979 | * @return bool If the item was removed or not |
||
| 980 | */ |
||
| 981 | 2 | public function remove_donation( $form_id, $args = array() ) { |
|
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Add a fee to a given payment |
||
| 1014 | * |
||
| 1015 | * @since 1.5 |
||
| 1016 | * |
||
| 1017 | * @param array $args Array of arguments for the fee to add |
||
| 1018 | * @param bool $global |
||
| 1019 | * |
||
| 1020 | * @return bool If the fee was added |
||
| 1021 | */ |
||
| 1022 | public function add_fee( $args, $global = true ) { |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Remove a fee from the payment |
||
| 1048 | * |
||
| 1049 | * @since 1.5 |
||
| 1050 | * |
||
| 1051 | * @param int $key The array key index to remove |
||
| 1052 | * |
||
| 1053 | * @return bool If the fee was removed successfully |
||
| 1054 | */ |
||
| 1055 | public function remove_fee( $key ) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Remove a fee by the defined attributed |
||
| 1067 | * |
||
| 1068 | * @since 1.5 |
||
| 1069 | * |
||
| 1070 | * @param string $key The key to remove by |
||
| 1071 | * @param int|string $value The value to search for |
||
| 1072 | * @param boolean $global False - removes the first value it fines, True - removes all matches |
||
| 1073 | * |
||
| 1074 | * @return boolean If the item is removed |
||
| 1075 | */ |
||
| 1076 | public function remove_fee_by( $key, $value, $global = false ) { |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Get the fees, filterable by type |
||
| 1135 | * |
||
| 1136 | * @since 1.5 |
||
| 1137 | * |
||
| 1138 | * @param string $type All, item, fee |
||
| 1139 | * |
||
| 1140 | * @return array The Fees for the type specified |
||
| 1141 | */ |
||
| 1142 | public function get_fees( $type = 'all' ) { |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Add a note to a payment |
||
| 1164 | * |
||
| 1165 | * @since 1.0 |
||
| 1166 | * |
||
| 1167 | * @param string $note The note to add |
||
| 1168 | * |
||
| 1169 | * @return void |
||
| 1170 | */ |
||
| 1171 | public function add_note( $note = false ) { |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Increase the payment's subtotal |
||
| 1182 | * |
||
| 1183 | * @since 1.5 |
||
| 1184 | * |
||
| 1185 | * @param float $amount The amount to increase the payment subtotal by |
||
| 1186 | * |
||
| 1187 | * @return void |
||
| 1188 | 2 | */ |
|
| 1189 | 2 | private function increase_subtotal( $amount = 0.00 ) { |
|
| 1195 | |||
| 1196 | 2 | /** |
|
| 1197 | 2 | * Decrease the payment's subtotal |
|
| 1198 | * |
||
| 1199 | * @since 1.5 |
||
| 1200 | * |
||
| 1201 | * @param float $amount The amount to decrease the payment subtotal by |
||
| 1202 | * |
||
| 1203 | * @return void |
||
| 1204 | */ |
||
| 1205 | private function decrease_subtotal( $amount = 0.00 ) { |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Increase the payment's subtotal |
||
| 1218 | * |
||
| 1219 | * @since 1.5 |
||
| 1220 | * |
||
| 1221 | * @param float $amount The amount to increase the payment subtotal by |
||
| 1222 | * |
||
| 1223 | * @return void |
||
| 1224 | */ |
||
| 1225 | private function increase_fees( $amount = 0.00 ) { |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Decrease the payment's subtotal |
||
| 1234 | * |
||
| 1235 | * @since 1.5 |
||
| 1236 | * |
||
| 1237 | * @param float $amount The amount to decrease the payment subtotal by |
||
| 1238 | * |
||
| 1239 | * @return void |
||
| 1240 | */ |
||
| 1241 | 52 | private function decrease_fees( $amount = 0.00 ) { |
|
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Set or update the total for a payment |
||
| 1254 | 52 | * |
|
| 1255 | * @since 1.0 |
||
| 1256 | * @return void |
||
| 1257 | 52 | */ |
|
| 1258 | 39 | private function recalculate_total() { |
|
| 1261 | 52 | ||
| 1262 | /** |
||
| 1263 | 52 | * Set the payment status and run any status specific changes necessary |
|
| 1264 | 52 | * |
|
| 1265 | * @since 1.0 |
||
| 1266 | * |
||
| 1267 | 42 | * @param string $status The status to set the payment to |
|
| 1268 | * |
||
| 1269 | 42 | * @return bool Returns if the status was successfully updated |
|
| 1270 | */ |
||
| 1271 | public function update_status( $status = false ) { |
||
| 1272 | 42 | ||
| 1273 | //standardize the 'complete(d)' status |
||
| 1274 | 42 | if ( $status == 'completed' || $status == 'complete' ) { |
|
| 1275 | $status = 'publish'; |
||
| 1276 | } |
||
| 1277 | 42 | ||
| 1278 | 42 | $old_status = ! empty( $this->old_status ) ? $this->old_status : false; |
|
| 1279 | 42 | ||
| 1280 | 42 | if ( $old_status === $status ) { |
|
| 1281 | return false; // Don't permit status changes that aren't changes |
||
| 1282 | 42 | } |
|
| 1283 | |||
| 1284 | 42 | $do_change = apply_filters( 'give_should_update_payment_status', true, $this->ID, $status, $old_status ); |
|
| 1285 | 42 | ||
| 1286 | $updated = false; |
||
| 1287 | |||
| 1288 | |||
| 1289 | 42 | if ( $do_change ) { |
|
| 1290 | 4 | ||
| 1291 | 4 | do_action( 'give_before_payment_status_change', $this->ID, $status, $old_status ); |
|
| 1292 | 42 | ||
| 1293 | $update_fields = array( |
||
| 1294 | 'ID' => $this->ID, |
||
| 1295 | 42 | 'post_status' => $status, |
|
| 1296 | 3 | 'edit_date' => current_time( 'mysql' ) |
|
| 1297 | 3 | ); |
|
| 1298 | |||
| 1299 | $updated = wp_update_post( apply_filters( 'give_update_payment_status_fields', $update_fields ) ); |
||
| 1300 | 42 | ||
| 1301 | $all_payment_statuses = give_get_payment_statuses(); |
||
| 1302 | 42 | $this->status_nicename = array_key_exists( $status, $all_payment_statuses ) ? $all_payment_statuses[ $status ] : ucfirst( $status ); |
|
| 1303 | |||
| 1304 | 42 | // Process any specific status functions |
|
| 1305 | switch ( $status ) { |
||
| 1306 | case 'refunded': |
||
| 1307 | $this->process_refund(); |
||
| 1308 | break; |
||
| 1309 | case 'failed': |
||
| 1310 | $this->process_failure(); |
||
| 1311 | break; |
||
| 1312 | case 'pending': |
||
| 1313 | $this->process_pending(); |
||
| 1314 | 4 | break; |
|
| 1315 | 4 | case 'cancelled': |
|
| 1316 | 4 | $this->process_cancelled(); |
|
| 1317 | 4 | break; |
|
| 1318 | } |
||
| 1319 | 4 | ||
| 1320 | 4 | do_action( 'give_update_payment_status', $this->ID, $status, $old_status ); |
|
| 1321 | |||
| 1322 | } |
||
| 1323 | |||
| 1324 | return $updated; |
||
| 1325 | |||
| 1326 | } |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Change the status of the payment to refunded, and run the necessary changes |
||
| 1330 | * |
||
| 1331 | * @since 1.5 |
||
| 1332 | 52 | * @return void |
|
| 1333 | */ |
||
| 1334 | 52 | public function refund() { |
|
| 1341 | |||
| 1342 | 52 | /** |
|
| 1343 | 52 | * Get a post meta item for the payment |
|
| 1344 | 52 | * |
|
| 1345 | * @since 1.5 |
||
| 1346 | 52 | * |
|
| 1347 | 52 | * @param string $meta_key The Meta Key |
|
| 1348 | 52 | * @param boolean $single Return single item or array |
|
| 1349 | * |
||
| 1350 | 52 | * @return mixed The value from the post meta |
|
| 1351 | 52 | */ |
|
| 1352 | 52 | public function get_meta( $meta_key = '_give_payment_meta', $single = true ) { |
|
| 1379 | |||
| 1380 | /** |
||
| 1381 | * Update the post meta |
||
| 1382 | * |
||
| 1383 | * @since 1.5 |
||
| 1384 | 52 | * |
|
| 1385 | * @param string $meta_key The meta key to update |
||
| 1386 | 52 | * @param string $meta_value The meta value |
|
| 1387 | 52 | * @param string $prev_value Previous meta value |
|
| 1388 | * |
||
| 1389 | 52 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure |
|
| 1390 | 52 | */ |
|
| 1391 | public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * When a payment is set to a status of 'refunded' process the necessary actions to reduce stats |
||
| 1424 | 4 | * |
|
| 1425 | * @since 1.5 |
||
| 1426 | 4 | * @access private |
|
| 1427 | 4 | * @return void |
|
| 1428 | 4 | */ |
|
| 1429 | private function process_refund() { |
||
| 1458 | |||
| 1459 | /** |
||
| 1460 | 3 | * Process when a payment is set to failed |
|
| 1461 | 1 | * |
|
| 1462 | 1 | * @since 1.5 |
|
| 1463 | * @return void |
||
| 1464 | */ |
||
| 1465 | 3 | private function process_failure() { |
|
| 1469 | |||
| 1470 | /** |
||
| 1471 | 2 | * Process when a payment moves to pending |
|
| 1472 | 2 | * |
|
| 1473 | 2 | * @since 1.5 |
|
| 1474 | * @return void |
||
| 1475 | 2 | */ |
|
| 1476 | 2 | private function process_pending() { |
|
| 1504 | |||
| 1505 | /** |
||
| 1506 | 6 | * Process when a payment moves to cancelled |
|
| 1507 | * |
||
| 1508 | 6 | * @since 1.5 |
|
| 1509 | * @return void |
||
| 1510 | 6 | */ |
|
| 1511 | 4 | private function process_cancelled() { |
|
| 1512 | 4 | $process_cancelled = true; |
|
| 1513 | |||
| 1514 | 6 | // If the payment was not in publish or revoked status, don't decrement stats as they were never incremented |
|
| 1515 | 4 | if ( ( 'publish' != $this->old_status && 'revoked' != $this->old_status ) || 'cancelled' != $this->status ) { |
|
| 1516 | 4 | $process_cancelled = false; |
|
| 1517 | } |
||
| 1518 | 6 | ||
| 1519 | // Allow extensions to filter for their own payment types, Example: Recurring Payments |
||
| 1520 | 6 | $process_cancelled = apply_filters( 'give_should_process_cancelled', $process_cancelled, $this ); |
|
| 1521 | |||
| 1522 | if ( false === $process_cancelled ) { |
||
| 1523 | return; |
||
| 1524 | } |
||
| 1525 | |||
| 1526 | $decrease_store_earnings = apply_filters( 'give_decrease_store_earnings_on_cancelled', true, $this ); |
||
| 1527 | $decrease_customer_value = apply_filters( 'give_decrease_customer_value_on_cancelled', true, $this ); |
||
| 1528 | 6 | $decrease_purchase_count = apply_filters( 'give_decrease_customer_purchase_count_on_cancelled', true, $this ); |
|
| 1529 | 6 | ||
| 1530 | $this->maybe_alter_stats( $decrease_store_earnings, $decrease_customer_value, $decrease_purchase_count ); |
||
| 1531 | $this->delete_sales_logs(); |
||
| 1532 | 6 | ||
| 1533 | 6 | $this->completed_date = false; |
|
| 1534 | 6 | $this->update_meta( '_give_completed_date', '' ); |
|
| 1535 | |||
| 1536 | // Clear the This Month earnings (this_monththis_month is NOT a typo) |
||
| 1537 | 6 | delete_transient( md5( 'give_earnings_this_monththis_month' ) ); |
|
| 1538 | 6 | } |
|
| 1539 | 6 | ||
| 1540 | /** |
||
| 1541 | 6 | * Used during the process of moving to refunded or pending, to decrement stats |
|
| 1542 | 6 | * |
|
| 1543 | * @since 1.5 |
||
| 1544 | * |
||
| 1545 | * @param bool $alter_store_earnings If the method should alter the store earnings |
||
| 1546 | * @param bool $alter_customer_value If the method should reduce the customer value |
||
| 1547 | * @param bool $alter_customer_purchase_count If the method should reduce the customer's purchase count |
||
| 1548 | * |
||
| 1549 | * @return void |
||
| 1550 | */ |
||
| 1551 | private function maybe_alter_stats( $alter_store_earnings, $alter_customer_value, $alter_customer_purchase_count ) { |
||
| 1576 | |||
| 1577 | 52 | /** |
|
| 1578 | 52 | * Delete sales logs for this purchase |
|
| 1579 | * |
||
| 1580 | * @since 1.5 |
||
| 1581 | * @return void |
||
| 1582 | */ |
||
| 1583 | private function delete_sales_logs() { |
||
| 1598 | |||
| 1599 | 52 | /** |
|
| 1600 | * Setup functions only, these are not to be used by developers. |
||
| 1601 | * These functions exist only to allow the setup routine to be backwards compatible with our old |
||
| 1602 | * helper functions. |
||
| 1603 | * |
||
| 1604 | * These will run whenever setup_payment is called, which should only be called once. |
||
| 1605 | * To update an attribute, update it directly instead of re-running the setup routine |
||
| 1606 | */ |
||
| 1607 | |||
| 1608 | 52 | /** |
|
| 1609 | 52 | * Setup the payment completed date |
|
| 1610 | * |
||
| 1611 | 52 | * @since 1.5 |
|
| 1612 | * @return string The date the payment was completed |
||
| 1613 | */ |
||
| 1614 | private function setup_completed_date() { |
||
| 1625 | |||
| 1626 | /** |
||
| 1627 | * Setup the payment mode |
||
| 1628 | * |
||
| 1629 | * @since 1.5 |
||
| 1630 | 52 | * @return string The payment mode |
|
| 1631 | */ |
||
| 1632 | private function setup_mode() { |
||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * Setup the payment total |
||
| 1638 | * |
||
| 1639 | * @since 1.5 |
||
| 1640 | 52 | * @return float The payment total |
|
| 1641 | 52 | */ |
|
| 1642 | private function setup_total() { |
||
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Setup the payment subtotal |
||
| 1659 | * |
||
| 1660 | * @since 1.5 |
||
| 1661 | * @return float The subtotal of the payment |
||
| 1662 | */ |
||
| 1663 | private function setup_subtotal() { |
||
| 1668 | |||
| 1669 | /** |
||
| 1670 | * Setup the payment fees |
||
| 1671 | * |
||
| 1672 | * @since 1.5 |
||
| 1673 | * @return float The fees total for the payment |
||
| 1674 | */ |
||
| 1675 | private function setup_fees_total() { |
||
| 1688 | |||
| 1689 | /** |
||
| 1690 | * Setup the currency code |
||
| 1691 | * |
||
| 1692 | * @since 1.5 |
||
| 1693 | * @return string The currency for the payment |
||
| 1694 | */ |
||
| 1695 | 52 | private function setup_currency() { |
|
| 1700 | |||
| 1701 | /** |
||
| 1702 | * Setup any fees associated with the payment |
||
| 1703 | * |
||
| 1704 | * @since 1.5 |
||
| 1705 | * @return array The Fees |
||
| 1706 | */ |
||
| 1707 | 52 | private function setup_fees() { |
|
| 1712 | |||
| 1713 | /** |
||
| 1714 | * Setup the gateway used for the payment |
||
| 1715 | * |
||
| 1716 | * @since 1.5 |
||
| 1717 | * @return string The gateway |
||
| 1718 | */ |
||
| 1719 | 52 | private function setup_gateway() { |
|
| 1724 | |||
| 1725 | /** |
||
| 1726 | * Setup the transaction ID |
||
| 1727 | * |
||
| 1728 | * @since 1.5 |
||
| 1729 | * @return string The transaction ID for the payment |
||
| 1730 | */ |
||
| 1731 | 52 | private function setup_transaction_id() { |
|
| 1743 | |||
| 1744 | /** |
||
| 1745 | * Setup the IP Address for the payment |
||
| 1746 | * |
||
| 1747 | 52 | * @since 1.5 |
|
| 1748 | * @return string The IP address for the payment |
||
| 1749 | 52 | */ |
|
| 1750 | 52 | private function setup_ip() { |
|
| 1755 | |||
| 1756 | 52 | /** |
|
| 1757 | * Setup the customer ID |
||
| 1758 | * |
||
| 1759 | * @since 1.5 |
||
| 1760 | * @return int The Customer ID |
||
| 1761 | */ |
||
| 1762 | private function setup_customer_id() { |
||
| 1767 | |||
| 1768 | /** |
||
| 1769 | * Setup the User ID associated with the purchase |
||
| 1770 | * |
||
| 1771 | 52 | * @since 1.5 |
|
| 1772 | 52 | * @return int The User ID |
|
| 1773 | 52 | */ |
|
| 1774 | 52 | private function setup_user_id() { |
|
| 1779 | 21 | ||
| 1780 | 21 | /** |
|
| 1781 | * Setup the email address for the purchase |
||
| 1782 | 21 | * |
|
| 1783 | 21 | * @since 1.5 |
|
| 1784 | * @return string The email address for the payment |
||
| 1785 | 21 | */ |
|
| 1786 | 21 | private function setup_email() { |
|
| 1795 | |||
| 1796 | 52 | /** |
|
| 1797 | * Setup the user info |
||
| 1798 | 52 | * |
|
| 1799 | * @since 1.5 |
||
| 1800 | * @return array The user info associated with the payment |
||
| 1801 | 52 | */ |
|
| 1802 | private function setup_user_info() { |
||
| 1859 | |||
| 1860 | 52 | /** |
|
| 1861 | * Setup the Address for the payment |
||
| 1862 | * |
||
| 1863 | * @since 1.5 |
||
| 1864 | * @return array The Address information for the payment |
||
| 1865 | */ |
||
| 1866 | private function setup_address() { |
||
| 1879 | |||
| 1880 | /** |
||
| 1881 | 52 | * Setup the form title |
|
| 1882 | 52 | * |
|
| 1883 | * @since 1.5 |
||
| 1884 | 52 | * @return string The Form Title |
|
| 1885 | */ |
||
| 1886 | 20 | private function setup_form_title() { |
|
| 1892 | 2 | ||
| 1893 | /** |
||
| 1894 | 20 | * Setup the form ID |
|
| 1895 | * |
||
| 1896 | 52 | * @since 1.5 |
|
| 1897 | * @return int The Form ID |
||
| 1898 | */ |
||
| 1899 | private function setup_form_id() { |
||
| 1905 | |||
| 1906 | /** |
||
| 1907 | * Setup the price ID |
||
| 1908 | * |
||
| 1909 | * @since 1.5 |
||
| 1910 | * @return int The Form Price ID |
||
| 1911 | */ |
||
| 1912 | private function setup_price_id() { |
||
| 1917 | |||
| 1918 | /** |
||
| 1919 | * Setup the payment key |
||
| 1920 | * |
||
| 1921 | * @since 1.5 |
||
| 1922 | * @return string The Payment Key |
||
| 1923 | */ |
||
| 1924 | private function setup_payment_key() { |
||
| 1929 | |||
| 1930 | /** |
||
| 1931 | * Setup the payment number |
||
| 1932 | * |
||
| 1933 | * @since 1.5 |
||
| 1934 | 42 | * @return int|string Integer by default, or string if sequential order numbers is enabled |
|
| 1935 | 42 | */ |
|
| 1936 | private function setup_payment_number() { |
||
| 1953 | |||
| 1954 | 11 | /** |
|
| 1955 | 11 | * Converts this object into an array for special cases |
|
| 1956 | * |
||
| 1957 | * @return array The payment object as an array |
||
| 1958 | */ |
||
| 1959 | public function array_convert() { |
||
| 1962 | |||
| 1963 | /** |
||
| 1964 | * Retrieve payment completion date |
||
| 1965 | * |
||
| 1966 | * @since 1.5 |
||
| 1967 | * @return string Date payment was completed |
||
| 1968 | */ |
||
| 1969 | private function get_completed_date() { |
||
| 1972 | |||
| 1973 | /** |
||
| 1974 | 52 | * Retrieve payment subtotal |
|
| 1975 | 52 | * |
|
| 1976 | * @since 1.5 |
||
| 1977 | * @return float Payment subtotal |
||
| 1978 | */ |
||
| 1979 | private function get_subtotal() { |
||
| 1982 | |||
| 1983 | /** |
||
| 1984 | 41 | * Retrieve payment currency |
|
| 1985 | 41 | * |
|
| 1986 | * @since 1.5 |
||
| 1987 | * @return string Payment currency code |
||
| 1988 | */ |
||
| 1989 | private function get_currency() { |
||
| 1992 | |||
| 1993 | /** |
||
| 1994 | 42 | * Retrieve payment gateway |
|
| 1995 | 42 | * |
|
| 1996 | * @since 1.5 |
||
| 1997 | * @return string Gateway used |
||
| 1998 | */ |
||
| 1999 | private function get_gateway() { |
||
| 2002 | |||
| 2003 | /** |
||
| 2004 | 43 | * Retrieve payment transaction ID |
|
| 2005 | 43 | * |
|
| 2006 | * @since 1.5 |
||
| 2007 | * @return string Transaction ID from merchant processor |
||
| 2008 | */ |
||
| 2009 | private function get_transaction_id() { |
||
| 2012 | |||
| 2013 | /** |
||
| 2014 | * Retrieve payment IP |
||
| 2015 | * |
||
| 2016 | * @since 1.5 |
||
| 2017 | * @return string Payment IP address |
||
| 2018 | */ |
||
| 2019 | private function get_ip() { |
||
| 2022 | |||
| 2023 | /** |
||
| 2024 | 52 | * Retrieve payment customer ID |
|
| 2025 | 52 | * |
|
| 2026 | * @since 1.5 |
||
| 2027 | * @return int Payment customer ID |
||
| 2028 | */ |
||
| 2029 | private function get_customer_id() { |
||
| 2032 | |||
| 2033 | /** |
||
| 2034 | 52 | * Retrieve payment user ID |
|
| 2035 | 52 | * |
|
| 2036 | * @since 1.5 |
||
| 2037 | * @return int Payment user ID |
||
| 2038 | */ |
||
| 2039 | private function get_user_id() { |
||
| 2042 | |||
| 2043 | /** |
||
| 2044 | 42 | * Retrieve payment email |
|
| 2045 | 42 | * |
|
| 2046 | * @since 1.5 |
||
| 2047 | * @return string Payment customer email |
||
| 2048 | */ |
||
| 2049 | private function get_email() { |
||
| 2052 | |||
| 2053 | /** |
||
| 2054 | * Retrieve payment user info |
||
| 2055 | * |
||
| 2056 | * @since 1.5 |
||
| 2057 | * @return array Payment user info |
||
| 2058 | */ |
||
| 2059 | private function get_user_info() { |
||
| 2062 | |||
| 2063 | /** |
||
| 2064 | * Retrieve payment billing address |
||
| 2065 | * |
||
| 2066 | * @since 1.5 |
||
| 2067 | * @return array Payment billing address |
||
| 2068 | */ |
||
| 2069 | private function get_address() { |
||
| 2072 | |||
| 2073 | /** |
||
| 2074 | * Retrieve payment key |
||
| 2075 | * |
||
| 2076 | * @since 1.5 |
||
| 2077 | * @return string Payment key |
||
| 2078 | */ |
||
| 2079 | private function get_key() { |
||
| 2082 | |||
| 2083 | /** |
||
| 2084 | * Retrieve payment key |
||
| 2085 | * |
||
| 2086 | * @since 1.5 |
||
| 2087 | * @return string Payment key |
||
| 2088 | */ |
||
| 2089 | private function get_form_id() { |
||
| 2092 | |||
| 2093 | /** |
||
| 2094 | * Retrieve payment number |
||
| 2095 | * |
||
| 2096 | * @since 1.5 |
||
| 2097 | * @return int|string Payment number |
||
| 2098 | */ |
||
| 2099 | private function get_number() { |
||
| 2102 | |||
| 2103 | } |
||
| 2104 |
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.