Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • edd_check_for_existing_payment
  • edd_clear_earnings_cache
  • edd_clear_user_history_cache
  • edd_complete_purchase
  • edd_delete_purchase
  • edd_get_earnings_by_date
  • edd_get_payment_amount
  • edd_get_payment_fees
  • edd_get_payment_gateway
  • edd_get_payment_key
  • edd_get_payment_meta
  • edd_get_payment_meta_cart_details
  • edd_get_payment_meta_downloads
  • edd_get_payment_meta_user_info
  • edd_get_payment_notes
  • edd_get_payment_status
  • edd_get_payment_statuses
  • edd_get_payment_subtotal
  • edd_get_payment_tax
  • edd_get_payment_user_email
  • edd_get_payment_user_id
  • edd_get_payments
  • edd_get_purchase_id_by_key
  • edd_get_sales_by_date
  • edd_get_total_earnings
  • edd_get_total_sales
  • edd_hide_payment_notes
  • edd_hide_payment_notes_from_feeds
  • edd_insert_payment
  • edd_insert_payment_note
  • edd_is_payment_complete
  • edd_payment_amount
  • edd_payment_subtotal
  • edd_payment_tax
  • edd_record_status_change
  • edd_trigger_purchase_delete
  • edd_undo_purchase
  • edd_update_edited_purchase
  • edd_update_old_payments_with_totals
  • edd_update_payment_status
  1 <?php
  2 /**
  3  * Payment Functions
  4  *
  5  * @package     EDD
  6  * @subpackage  Payments
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  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' ) ) exit;
 14 
 15 /**
 16  * Get Payments
 17  *
 18  * Retrieve payments from the database.
 19  *
 20  * Since 1.2, this function takes an array of arguments, instead of individual
 21  * parameters. All of the original paremeters remain, but can be passed in any
 22  * order via the array.
 23  *
 24  * $offset = 0, $number = 20, $mode = 'live', $orderby = 'ID', $order = 'DESC',
 25  * $user = null, $status = 'any', $meta_key = null
 26  *
 27  * @since 1.0
 28  * @param array $args Arguments passed to get payments
 29  * @return object $payments Payments retrieved from the database
 30  */
 31 function edd_get_payments( $args = array() ) {
 32     $defaults = array(
 33         'number'   => 20,
 34         'page'     => null,
 35         'mode'     => 'live',
 36         'orderby'  => 'ID',
 37         'order'    => 'DESC',
 38         'user'     => null,
 39         'status'   => 'any',
 40         'meta_key' => null,
 41         'year'     => null,
 42         'month'    => null,
 43         'day'      => null,
 44         's'        => null,
 45         'children' => false,
 46         'fields'   => null
 47     );
 48 
 49     $args = wp_parse_args( $args, $defaults );
 50 
 51     $payment_args = array(
 52         'post_type'      => 'edd_payment',
 53         'paged'          => $args['page'],
 54         'order'          => $args['order'],
 55         'orderby'        => $args['orderby'],
 56         'post_status'    => $args['status'],
 57         'year'           => $args['year'],
 58         'monthnum'       => $args['month'],
 59         'day'            => $args['day'],
 60         'fields'         => $args['fields']
 61     );
 62 
 63     if( $args['number'] == -1 )
 64         $payment_args['nopaging'] = true;
 65     else
 66         $payment_args['posts_per_page'] = $args['number'];
 67 
 68     switch ( $args['orderby'] ) :
 69         case 'amount' :
 70             $payment_args['orderby']  = 'meta_value_num';
 71             $payment_args['meta_key'] = '_edd_payment_total';
 72             break;
 73         default :
 74             $payment_args['orderby'] = $args['status'];
 75             break;
 76     endswitch;
 77 
 78     if ( ! $args['children'] )
 79         $payment_args['post_parent'] = 0; // Only get top level payments
 80 
 81     if ( ! is_null( $args['meta_key'] ) )
 82         $payment_args['meta_key'] = $args['meta_key'];
 83 
 84     if ( ! is_null( $args['user'] ) ) {
 85         if ( is_numeric( $args['user'] ) ) {
 86             $user_key = '_edd_payment_user_id';
 87         } else {
 88             $user_key = '_edd_payment_user_email';
 89         }
 90         $payment_args['meta_query'] = array(
 91             array(
 92                 'key'   => $user_key,
 93                 'value' => $args['user']
 94             )
 95         );
 96     }
 97 
 98     $search = trim( $args['s'] );
 99 
100     if ( is_email( $search ) || strlen( $search ) == 32 ) {
101         // This is a purchase key search
102         $key = is_email( $search ) ? '_edd_payment_user_email' : '_edd_payment_purchase_key';
103 
104         $search_meta = array(
105             'key'   => $key,
106             'value' => $search
107         );
108 
109         if ( isset( $payment_args['meta_query'] ) ) {
110             $payment_args['meta_query'][1] = $search_meta;
111         } else {
112             // Create a new meta query
113             $payment_args['meta_query'] = array( $search_meta );
114         }
115     } elseif ( is_numeric( $search ) ) {
116         // Searching for payments by user ID
117         $search_meta = array(
118             'key'   => '_edd_payment_user_id',
119             'value' => $search
120         );
121 
122         if ( isset( $payment_args['meta_query'] ) ) {
123             $payment_args['meta_query'][1] = $search_meta;
124         } else {
125             // Create a new meta query
126             $payment_args['meta_query'] = array( $search_meta );
127         }
128     } else {
129         $payment_args['s'] = $search;
130     }
131 
132     if ( $args['mode'] != 'all' ) {
133         if ( isset( $payment_args['meta_query'] ) ) {
134 
135             // Append to the user meta query
136             $payment_args['meta_query'][2] = array(
137                 'key'   => '_edd_payment_mode',
138                 'value' => $args['mode']
139             );
140         } else {
141             // Create a new meta query
142             $payment_args['meta_query'] = array(
143                 array(
144                     'key'   => '_edd_payment_mode',
145                     'value' => $args['mode']
146                 )
147             );
148         }
149     }
150 
151     $payments = get_posts( apply_filters( 'edd_get_payments_args', $payment_args ) );
152     if ( $payments ) {
153         return $payments;
154     }
155 
156     return false;
157 }
158 
159 /**
160  * Insert Payment
161  *
162  * @since 1.0
163  * @param array $payment_data
164  * @return bool true if payment is inserted, false otherwise
165  */
166 function edd_insert_payment( $payment_data = array() ) {
167     if ( empty( $payment_data ) )
168         return false;
169 
170     // Construct the payment title
171     if ( isset( $payment_data['user_info']['first_name'] ) || isset( $payment_data['user_info']['last_name'] ) ) {
172         $payment_title = $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'];
173     } else {
174         $payment_title = $payment_data['user_email'];
175     }
176 
177     // Retrieve the ID of the discount used, if any
178     if ( $payment_data['user_info']['discount'] != 'none' ) {
179         $discount = edd_get_discount_by_code( $payment_data['user_info']['discount'] );
180     }
181 
182     $args = apply_filters( 'edd_insert_payment_args', array(
183         'post_title'    => $payment_title,
184         'post_status'   => isset( $payment_data['status'] ) ? $payment_data['status'] : 'pending',
185         'post_type'     => 'edd_payment',
186         'post_parent'   => isset( $payment_data['parent'] ) ? $payment_data['parent'] : null,
187         'post_date'     => isset( $payment_data['post_date'] ) ? $payment_data['post_date'] : null,
188         'post_date_gmt' => isset( $payment_data['post_date'] ) ? $payment_data['post_date'] : null
189     ), $payment_data );
190 
191     // Create a blank payment
192     $payment = wp_insert_post( $args );
193 
194     if ( $payment ) {
195         $payment_meta = array(
196             'currency'     => $payment_data['currency'],
197             'downloads'    => serialize( $payment_data['downloads'] ),
198             'user_info'    => serialize( $payment_data['user_info'] ),
199             'cart_details' => serialize( $payment_data['cart_details'] ),
200             'tax'          => edd_is_cart_taxed() ? edd_get_cart_tax() : 0,
201         );
202 
203         $mode    = edd_is_test_mode() ? 'test' : 'live';
204         $gateway = isset( $_POST['edd-gateway'] ) ? $_POST['edd-gateway'] : '';
205 
206         // Record the payment details
207         update_post_meta( $payment, '_edd_payment_meta',         apply_filters( 'edd_payment_meta', $payment_meta, $payment_data ) );
208         update_post_meta( $payment, '_edd_payment_user_id',      $payment_data['user_info']['id'] );
209         update_post_meta( $payment, '_edd_payment_user_email',   $payment_data['user_email'] );
210         update_post_meta( $payment, '_edd_payment_user_ip',      edd_get_ip() );
211         update_post_meta( $payment, '_edd_payment_purchase_key', $payment_data['purchase_key'] );
212         update_post_meta( $payment, '_edd_payment_total',        $payment_data['price'] );
213         update_post_meta( $payment, '_edd_payment_mode',         $mode );
214         update_post_meta( $payment, '_edd_payment_gateway',      $gateway );
215         if ( ! empty( $discount ) )
216             update_post_meta( $payment, '_edd_payment_discount_id',  $discount->ID );
217 
218         // Clear the user's purchased cache
219         delete_transient( 'edd_user_' . $payment_data['user_info']['id'] . '_purchases' );
220 
221         do_action( 'edd_insert_payment', $payment, $payment_data );
222 
223         return $payment; // Return the ID
224     }
225     // Return false if no payment was inserted
226     return false;
227 }
228 
229 /**
230  * Updates a payment status, and performs all necessary functions to mark it as complete, and to finish a purchase.
231  *
232  * @since 1.0
233  * @param int $payment_id Payment ID
234  * @param string $new_status New Payment Status (default: publish)
235  * @return void
236  */
237 function edd_update_payment_status( $payment_id, $new_status = 'publish' ) {
238     if ( $new_status == 'completed' || $new_status == 'complete' ) {
239         $new_status = 'publish';
240     }
241 
242     $payment = get_post( $payment_id );
243 
244     if ( is_wp_error( $payment ) || !is_object( $payment ) )
245         return;
246 
247     $old_status = $payment->post_status;
248 
249     if ( $old_status === $new_status )
250         return; // Don't permit status changes that aren't changes
251 
252     do_action( 'edd_before_payment_status_change', $payment_id, $new_status, $old_status );
253 
254     $update_fields = array( 'ID' => $payment_id, 'post_status' => $new_status );
255 
256     wp_update_post( apply_filters( 'edd_update_payment_status_fields', $update_fields ) );
257 
258     do_action( 'edd_update_payment_status', $payment_id, $new_status, $old_status );
259 }
260 
261 /**
262  * Deletes a Purchase
263  *
264  * @since 1.0
265  * @global $edd_logs
266  * @uses EDD_Logging::delete_logs()
267  * @param int $payment_id Payment ID (default: 0)
268  * @return void
269  */
270 function edd_delete_purchase( $payment_id = 0 ) {
271     global $edd_logs;
272 
273     $downloads = edd_get_payment_meta_downloads( $payment_id );
274 
275     if ( is_array( $downloads ) ) {
276         // Update sale counts and earnings for all purchased products
277         foreach ( $downloads as $download ) {
278             edd_undo_purchase( $download['id'], $payment_id );
279         }
280     }
281 
282     do_action( 'edd_payment_delete', $payment_id );
283 
284     // Remove the payment
285     wp_delete_post( $payment_id, true );
286 
287     // Remove related sale log entries
288     $edd_logs->delete_logs(
289         null,
290         'sale',
291         array(
292             array(
293                 'key'   => '_edd_log_payment_id',
294                 'value' => $payment_id
295             )
296         )
297     );
298 
299     do_action( 'edd_payment_deleted', $payment_id );
300 }
301 
302 /**
303  * Undos a purchase, including the decrease of sale and earning stats. Used for
304  * when refunding or deleting a purchase
305  *
306  * @since 1.0.8.1
307  * @param int $download_id Download (Post) ID
308  * @param int $payment_id Payment ID
309  * @return void
310  */
311 function edd_undo_purchase( $download_id, $payment_id ) {
312     $payment = get_post( $payment_id );
313 
314     $status  = $payment->post_status;
315 
316     if ( $status != 'publish' )
317         return; // Payment has already been reversed, or was never completed
318 
319     edd_decrease_purchase_count( $download_id );
320     $purchase_meta      = edd_get_payment_meta( $payment_id );
321     $user_purchase_info = maybe_unserialize( $purchase_meta['user_info'] );
322     $cart_details       = maybe_unserialize( $purchase_meta['cart_details'] );
323     $amount             = null;
324 
325     if ( is_array( $cart_details ) ) {
326         $cart_item_id   = array_search( $download_id, $cart_details );
327         $amount         = isset( $cart_details[$cart_item_id]['price'] ) ? $cart_details[$cart_item_id]['price'] : null;
328     }
329 
330     $amount = edd_get_download_final_price( $download_id, $user_purchase_info, $amount );
331 
332     edd_decrease_earnings( $download_id, $amount );
333 }
334 
335 /**
336  * Check For Existing Payment
337  *
338  * @since 1.0
339  * @param int $payment_id Payment ID
340  * @return bool true if payment exists, false otherwise
341  */
342 function edd_check_for_existing_payment( $payment_id ) {
343     $payment = get_post( $payment_id );
344 
345     if ( $payment && $payment->post_status == 'publish' ) {
346         return true; // Payment exists
347     }
348 
349     return false; // This payment doesn't exist
350 }
351 
352 /**
353  * Get Payment Status
354  *
355  * @since 1.0
356  * @param obj $payment Payment Object
357  * @param bool $return_label Whether to return the payment status or not
358  * @return mixed string if payment status exists, false otherwise
359  */
360 function edd_get_payment_status( $payment = OBJECT, $return_label = false ) {
361     if ( ! is_object( $payment ) || !isset( $payment->post_status ) )
362         return false;
363 
364     $statuses = edd_get_payment_statuses();
365     if ( ! is_array( $statuses ) || empty( $statuses ) )
366         return false;
367 
368     if ( array_key_exists( $payment->post_status, $statuses ) ) {
369         if ( true === $return_label ) {
370             return $statuses[ $payment->post_status ];
371         } else {
372             return array_search( $payment->post_status, $statuses );
373         }
374     }
375 
376     return false;
377 }
378 
379 /**
380  * Retrieves all available statuses for payments.
381  *
382  * @since 1.0.8.1
383  * @return array $payment_status All the available payment statuses
384  */
385 function edd_get_payment_statuses() {
386     $payment_statuses = array(
387         'pending'  => __( 'Pending', 'edd' ),
388         'publish'  => __( 'Complete', 'edd' ),
389         'refunded' => __( 'Refunded', 'edd' ),
390         'failed'   => __( 'Failed', 'edd' ),
391         'revoked'  => __( 'Revoked', 'edd' )
392     );
393 
394     return apply_filters( 'edd_payment_statuses', $payment_statuses );
395 }
396 
397 /**
398  * Get Earnings By Date
399  *
400  * @since 1.0
401  * @param int $day Day number
402  * @param int $month_num Month number
403  * @param int $year Year
404  * @param int $hour Hour
405  * @return int $earnings Earnings
406  */
407 function edd_get_earnings_by_date( $day = null, $month_num, $year = null, $hour = null ) {
408     $args = array(
409         'post_type'      => 'edd_payment',
410         'nopaging'       => true,
411         'year'           => $year,
412         'monthnum'       => $month_num,
413         'meta_key'       => '_edd_payment_mode',
414         'meta_value'     => 'live',
415         'post_status'    => 'publish',
416         'fields'         => 'ids',
417         'update_post_term_cache' => false
418     );
419     if ( ! empty( $day ) )
420         $args['day'] = $day;
421 
422     if ( ! empty( $hour ) )
423         $args['hour'] = $hour;
424 
425     $args     = apply_filters( 'edd_get_earnings_by_date_args', $args );
426     $key      = md5( serialize( $args ) );
427     $earnings = get_transient( $key );
428 
429     if( false === $earnings ) {
430         $sales = get_posts( $args );
431         $earnings = 0;
432         if ( $sales ) {
433             foreach ( $sales as $sale ) {
434                 $amount    = edd_get_payment_amount( $sale );
435                 $earnings  = $earnings + $amount;
436             }
437         }
438         // Cache the results for one hour
439         set_transient( $key, $earnings, 60*60 );
440     }
441 
442     return round( $earnings, 2 );
443 }
444 
445 /**
446  * Get Sales By Date
447  *
448  * @since 1.1.4.0
449  * @author Sunny Ratilal
450  * @param int $day Day number
451  * @param int $month_num Month number
452  * @param int $year Year
453  * @param int $hour Hour
454  * @return int $count Sales
455  */
456 function edd_get_sales_by_date( $day = null, $month_num = null, $year = null, $hour = null ) {
457     $args = array(
458         'post_type'      => 'edd_payment',
459         'nopaging'       => true,
460         'year'           => $year,
461         'meta_key'       => '_edd_payment_mode',
462         'meta_value'     => 'live',
463         'fields'         => 'ids',
464         'post_status'    => 'publish',
465         'update_post_meta_cache' => false,
466         'update_post_term_cache' => false
467     );
468 
469     if ( ! empty( $month_num ) )
470         $args['monthnum'] = $month_num;
471 
472     if ( ! empty( $day ) )
473         $args['day'] = $day;
474 
475     if ( ! empty( $hour ) )
476         $args['hour'] = $hour;
477 
478     $key   = md5( serialize( $args ) );
479     $count = get_transient( $key, 'edd' );
480 
481     if( false === $count ) {
482         $sales = new WP_Query( $args );
483         $count = (int) $sales->post_count;
484         // Cache the results for one hour
485         set_transient( $key, $count, 60*60 );
486     }
487 
488     return $count;
489 }
490 
491 /**
492  * Checks whether a payment has been marked as complete.
493  *
494  * @since 1.0.8
495  * @param int $payment_id Payment ID to check against
496  * @return bool true if complete, false otherwise
497  */
498 function edd_is_payment_complete( $payment_id ) {
499     $payment = get_post( $payment_id );
500     if ( $payment )
501         if ( $payment->post_status == 'publish' )
502             return true;
503     return false;
504 }
505 
506 /**
507  * Get Total Sales
508  *
509  * @since 1.2.2
510  * @return int $count Total sales
511  */
512 function edd_get_total_sales() {
513     $args = apply_filters( 'edd_get_total_sales_args', array(
514         'post_type'              => 'edd_payment',
515         'nopaging'               => true,
516         'meta_key'               => '_edd_payment_mode',
517         'meta_value'             => 'live',
518         'fields'                 => 'ids',
519         'post_status'            => 'publish',
520         'update_post_meta_cache' => false,
521         'update_post_term_cache' => false
522     ) );
523 
524     $key   = md5( serialize( $args ) );
525     $count = get_transient( $key );
526 
527     if ( false === $count ) {
528         $sales = new WP_Query( $args );
529         $count = (int) $sales->post_count;
530         set_transient( $key, $count, 60 * 60 );
531 
532     }
533     return $count;
534 }
535 
536 /**
537  * Get Total Earnings
538  *
539  * @since 1.2
540  * @return float $total Total earnings
541  */
542 function edd_get_total_earnings() {
543 
544     $total = get_transient( 'edd_earnings_total' );
545 
546     if( false === $total ) {
547 
548         $total = (float) 0;
549 
550         $args = apply_filters( 'edd_get_total_earnings_args', array(
551             'offset' => 0,
552             'number' => -1,
553             'mode'   => 'live',
554             'status' => 'publish',
555             'fields' => 'ids'
556         ) );
557 
558         $payments = edd_get_payments( $args );
559         if ( $payments ) {
560             foreach ( $payments as $payment ) {
561                 $total += edd_get_payment_amount( $payment );
562             }
563         }
564 
565         // Cache results for 1 day. This cache is cleared automatically when a payment is made
566         set_transient( 'edd_earnings_total', $total, 86400 );
567     }
568     return apply_filters( 'edd_total_earnings', $total );
569 }
570 
571 /**
572  * Get Payment Meta for a specific Payment
573  *
574  * @since 1.2
575  * @param int $payment_id Payment ID
576  * @return array $meta Payment Meta
577  */
578 function edd_get_payment_meta( $payment_id ) {
579     $meta = get_post_meta( $payment_id, '_edd_payment_meta', true );
580 
581     // Payment meta was simplified in EDD v1.5, so these are here for backwards compatibility
582     if ( ! isset( $meta['key'] ) )
583         $meta['key'] = edd_get_payment_key( $payment_id );
584 
585     if ( ! isset( $meta['amount'] ) )
586         $meta['amount'] = edd_get_payment_amount( $payment_id );
587 
588     if ( ! isset( $meta['email'] ) )
589         $meta['email'] = edd_get_payment_user_email( $payment_id );
590 
591     if ( ! isset( $meta['date'] ) )
592         $meta['date'] = get_post_field( 'post_date', $payment_id );
593 
594     return apply_filters( 'edd_get_payment_meta', $meta, $payment_id );
595 }
596 
597 /**
598  * Get the user_info Key from Payment Meta
599  *
600  * @since 1.2
601  * @param int $payment_id Payment ID
602  * @return array $user_info User Info Meta Values
603  */
604 function edd_get_payment_meta_user_info( $payment_id ) {
605     $payment_meta = edd_get_payment_meta( $payment_id );
606     $user_info    = isset( $payment_meta['user_info'] ) ? maybe_unserialize( $payment_meta['user_info'] ) : false;
607 
608     return apply_filters( 'edd_payment_meta_user_info', $user_info );
609 }
610 
611 /**
612  * Get the downloads Key from Payment Meta
613  *
614  * @since 1.2
615  * @param int $payment_id Payment ID
616  * @return array $downloads Downloads Meta Values
617  */
618 function edd_get_payment_meta_downloads( $payment_id ) {
619     $payment_meta = edd_get_payment_meta( $payment_id );
620     $downloads    = maybe_unserialize( $payment_meta['downloads'] );
621 
622     return apply_filters( 'edd_payment_meta_downloads', $downloads );
623 }
624 
625 /**
626  * Get the cart_details Key from Payment Meta
627  *
628  * @since 1.2
629  * @param int $payment_id Payment ID
630  * @return array $cart_details Cart Details Meta Values
631  */
632 function edd_get_payment_meta_cart_details( $payment_id ) {
633     $payment_meta = edd_get_payment_meta( $payment_id );
634     $cart_details = maybe_unserialize( $payment_meta['cart_details'] );
635 
636     return apply_filters( 'edd_payment_meta_cart_details', $cart_details );
637 }
638 
639 /**
640  * Get the user email associated with a payment
641  *
642  * @since 1.2
643  * @param int $payment_id Payment ID
644  * @return string $email User Email
645  */
646 function edd_get_payment_user_email( $payment_id ) {
647     $email = get_post_meta( $payment_id, '_edd_payment_user_email', true );
648 
649     return apply_filters( 'edd_payment_user_email', $email );
650 }
651 
652 
653 /**
654  * Get the user ID associated with a payment
655  *
656  * @since 1.5.1
657  * @param int $payment_id Payment ID
658  * @return string $user_id User ID
659  */
660 function edd_get_payment_user_id( $payment_id ) {
661     $user_id = get_post_meta( $payment_id, '_edd_payment_user_id', true );
662 
663     return apply_filters( 'edd_payment_user_id', $user_id );
664 }
665 
666 /**
667  * Get the gateway associated with a payment
668  *
669  * @since 1.2
670  * @param int $payment_id Payment ID
671  * @return string $gateway Gateway
672  */
673 function edd_get_payment_gateway( $payment_id ) {
674     $gateway = get_post_meta( $payment_id, '_edd_payment_gateway', true );
675 
676     return apply_filters( 'edd_payment_gateway', $gateway );
677 }
678 
679 /**
680  * Get the purchase key for a purchase
681  *
682  * @since 1.2
683  * @param int $payment_id Payment ID
684  * @return string $key Purchase key
685  */
686 function edd_get_payment_key( $payment_id = 0 ) {
687     $key = get_post_meta( $payment_id, '_edd_payment_purchase_key', true );
688     return apply_filters( 'edd_payment_key', $key, $payment_id );
689 }
690 
691 /**
692  * Get the fully formatted payment amount. The payment amount is retrieved using
693  * edd_get_payment_amount() and is then sent through edd_currency_filter() and
694  * edd_format_amount() to format the amount correctly.
695  *
696  * @since 1.4
697  * @param int $payment_id Payment ID
698  * @return string $amount Fully formatted payment amount
699  */
700 function edd_payment_amount( $payment_id = 0 ) {
701     $amount = edd_get_payment_amount( $payment_id );
702     return edd_currency_filter( edd_format_amount( $amount ) );
703 }
704 /**
705  * Get the amount associated with a payment
706  *
707  * @access public
708  * @since 1.2
709  * @param int $payment_id Payment ID
710  * @return string $amount Payment amount
711  */
712 function edd_get_payment_amount( $payment_id ) {
713     $amount = get_post_meta( $payment_id, '_edd_payment_total', true );
714 
715     if ( empty( $amount ) && $amount != 0 ) {
716         $payment_meta = edd_get_payment_meta( $payment_id );
717         $amount       = $payment_meta['amount'];
718     }
719 
720     return apply_filters( 'edd_payment_amount', $amount );
721 }
722 
723 /**
724  * Retrieves subtotal for payment (this is the amount before taxes) and then
725  * returns a full formatted amount. This function essentialy calls
726  * edd_get_payment_subtotal()
727  *
728  * @since 1.3.3
729  * @see edd_get_payment_subtotal()
730  * @param int $payment_id Payment ID
731  * @param bool $payment_meta Payment Meta provided? (default: false)
732  * @return string $subtotal Fully formatted payment subtotal
733  */
734 function edd_payment_subtotal( $payment_id = 0, $payment_meta = false ) {
735     $subtotal = edd_get_payment_subtotal( $payment_id, $payment_meta );
736 
737     return edd_currency_filter( edd_format_amount( $subtotal ) );
738 }
739 
740 /**
741  * Retrieves subtotal for payment (this is the amount before taxes) and then
742  * returns a non formatted amount.
743  *
744  * @since 1.3.3
745  * @global $edd_options Array of all the EDD Options
746  * @param int $payment_id Payment ID
747  * @param bool $payment_meta Get payment meta?
748  * @return float $subtotal Subtotal for payment (non formatted)
749  */
750 function edd_get_payment_subtotal( $payment_id = 0, $payment_meta = false ) {
751     global $edd_options;
752 
753     if ( ! $payment_meta )
754         $payment_meta = edd_get_payment_meta( $payment_id );
755 
756     $subtotal = isset( $payment_meta['subtotal'] ) ? $payment_meta['subtotal'] : $payment_meta['amount'];
757 
758     $tax = edd_use_taxes() ? edd_get_payment_tax( $payment_id ) : 0;
759 
760     if (
761         ( isset( $edd_options['prices_include_tax'] ) && $edd_options['prices_include_tax'] == 'no' && ! edd_prices_show_tax_on_checkout() ) ||
762         ( isset( $edd_options['prices_include_tax'] ) && ! edd_prices_show_tax_on_checkout() && $edd_options['prices_include_tax'] == 'yes' )
763     ) {
764         $subtotal -= $tax;
765     }
766 
767     return apply_filters( 'edd_get_payment_subtotal', $subtotal, $payment_id );
768 }
769 
770 /**
771  * Retrieves taxed amount for payment and then returns a full formatted amount
772  * This function essentialy calls edd_get_payment_tax()
773  *
774  * @since 1.3.3
775  * @see edd_get_payment_tax()
776  * @param int $payment_id Payment ID
777  * @param bool $payment_meta Payment Meta provided? (default: false)
778  * @return string $subtotal Fully formatted payment subtotal
779  */
780 function edd_payment_tax( $payment_id = 0, $payment_meta = false ) {
781     $tax = edd_get_payment_tax( $payment_id, $payment_meta );
782 
783     return edd_currency_filter( edd_format_amount( $tax ) );
784 }
785 
786 /**
787  * Retrieves taxed amount for payment and then returns a non formatted amount
788  *
789  * @since 1.3.3
790  * @param int $payment_id Payment ID
791  * @param bool $payment_meta Get payment meta?
792  * @return float $subtotal Subtotal for payment (non formatted)
793  */
794 function edd_get_payment_tax( $payment_id = 0, $payment_meta = false ) {
795     if ( ! $payment_meta )
796         $payment_meta = edd_get_payment_meta( $payment_id );
797 
798     $tax = isset( $payment_meta['tax'] ) ? $payment_meta['tax'] : 0;
799 
800     return apply_filters( 'edd_get_payment_tax', $tax, $payment_id );
801 }
802 
803 /**
804  * Retrieves arbitrary fees for the payment
805  *
806  * @since 1.5
807  * @param int $payment_id Payment ID
808  * @param bool $payment_meta Get payment meta? (default: false)
809  * @return mixed array if payment fees found, false otherwise
810  */
811 function edd_get_payment_fees( $payment_id = 0, $payment_meta = false ) {
812     if ( ! $payment_meta )
813         $payment_meta = edd_get_payment_meta( $payment_id );
814 
815     $fees = array();
816     $payment_fees = isset( $payment_meta['fees'] ) ? $payment_meta['fees'] : false;
817 
818     if ( ! empty( $payment_fees ) ) {
819         foreach ( $payment_fees as $fee_id => $fee ) {
820             $fees[] = array(
821                 'id'     => $fee_id,
822                 'amount' => $fee['amount'],
823                 'label'  => $fee['label']
824             );
825         }
826     }
827 
828     return apply_filters( 'edd_get_payment_fees', $fees, $payment_id );
829 }
830 
831 /**
832  * Retrieve the purchase ID based on the purchase key
833  *
834  * @since 1.3.2
835  * @global object $wpdb Used to query the database using the WordPress
836  *   Database API
837  * @param string $key the purchase key to search for
838  * @return int $purchase Purchase ID
839  */
840 function edd_get_purchase_id_by_key( $key ) {
841     global $wpdb;
842 
843     $purchase = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_edd_payment_purchase_key' AND meta_value = %s LIMIT 1", $key ) );
844 
845     if ( $purchase != NULL )
846         return $purchase;
847 
848     return 0;
849 }
850 
851 /**
852  * Retrieve all notes attached to a purchase
853  *
854  * @since 1.4
855  * @param int $payment_id The payment ID to retrieve notes for
856  * @return array $notes Payment Notes
857  */
858 function edd_get_payment_notes( $payment_id = 0 ) {
859     if ( empty( $payment_id ) )
860         return false;
861 
862     remove_filter( 'comments_clauses', 'edd_hide_payment_notes', 10, 2 );
863 
864     $notes = get_comments( array( 'post_id' => $payment_id, 'order' => 'ASC' ) );
865 
866     add_filter( 'comments_clauses', 'edd_hide_payment_notes', 10, 2 );
867 
868     return $notes;
869 }
870 
871 /**
872  * Add a note to a payment
873  *
874  * @since 1.4
875  * @param int $payment_id The payment ID to store a note for
876  * @param string $note The note to store
877  * @return int The new note ID
878  */
879 function edd_insert_payment_note( $payment_id = 0, $note = '' ) {
880     if ( empty( $payment_id ) )
881         return false;
882 
883     do_action( 'edd_pre_insert_payment_note', $payment_id, $note );
884 
885     $note_id = wp_insert_comment( wp_filter_comment( array(
886         'comment_post_ID'      => $payment_id,
887         'comment_content'      => $note,
888         'user_id'              => is_admin() ? get_current_user_id() : 0,
889         'comment_date'         => current_time( 'mysql' ),
890         'comment_date_gmt'     => current_time( 'mysql', 1 ),
891         'comment_approved'     => 1,
892         'comment_parent'       => 0,
893         'comment_author'       => '',
894         'comment_author_IP'    => '',
895         'comment_author_url'   => '',
896         'comment_author_email' => '',
897         'comment_type'         => 'edd_payment_note'
898 
899     ) ) );
900 
901     do_action( 'edd_insert_payment_note', $note_id, $payment_id, $note );
902 
903     return $note_id;
904 }
905 
906 /**
907  * Exclude notes (comments) on edd_payment post type from showing in Recent
908  * Comments widgets
909  *
910  * @since 1.4.1
911  * @param array $clauses Comment clauses for comment query
912  * @param obj $wp_comment_query WordPress Comment Query Object
913  * @return array $clauses Updated comment clauses
914  */
915 function edd_hide_payment_notes( $clauses, $wp_comment_query ) {
916     global $wpdb;
917 
918     if ( ! $clauses['join'] )
919         $clauses['join'] = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
920 
921     if ( ! $wp_comment_query->query_vars['post_type' ] ) // only apply if post_type hasn't already been queried
922         $clauses['where'] .= $wpdb->prepare( " AND {$wpdb->posts}.post_type != %s", 'edd_payment' );
923 
924     return $clauses;
925 }
926 add_filter( 'comments_clauses', 'edd_hide_payment_notes', 10, 2 );
927 
928 /**
929  * Exclude notes (comments) on edd_payment post type from showing in comment feeds
930  *
931  * @since 1.5.1
932  * @param array $where
933  * @param obj $wp_comment_query WordPress Comment Query Object
934  * @return array $where
935  */
936 function edd_hide_payment_notes_from_feeds( $where, $wp_comment_query ) {
937     global $wpdb;
938 
939     if ( ! $wp_comment_query->query_vars['post_type' ] ) // only apply if post_type hasn't already been queried
940         $where .= $wpdb->prepare( " AND post_type != %s", 'edd_payment' );
941 
942     return $where;
943 }
944 add_filter( 'comment_feed_where', 'edd_hide_payment_notes_from_feeds', 10, 2 );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0