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 Actions
  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  * Complete a purchase
 17  *
 18  * Performs all necessary actions to complete a purchase.
 19  * Triggered by the edd_update_payment_status() function.
 20  *
 21  * @since 1.0.8.3
 22  * @param int $payment_id the ID number of the payment
 23  * @param string $new_status the status of the payment, probably "publish"
 24  * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending"
 25  * @return void
 26 */
 27 function edd_complete_purchase( $payment_id, $new_status, $old_status ) {
 28     if ( $old_status == 'publish' || $old_status == 'complete' )
 29         return; // Make sure that payments are only completed once
 30 
 31     // Make sure the payment completion is only processed when new status is complete
 32     if ( $new_status != 'publish' && $new_status != 'complete' )
 33         return;
 34 
 35     if ( edd_is_test_mode() && ! apply_filters( 'edd_log_test_payment_stats', false ) )
 36         return;
 37 
 38     $payment_data   = edd_get_payment_meta( $payment_id );
 39     $downloads      = maybe_unserialize( $payment_data['downloads'] );
 40     $user_info      = maybe_unserialize( $payment_data['user_info'] );
 41     $cart_details   = maybe_unserialize( $payment_data['cart_details'] );
 42 
 43     if ( is_array( $downloads ) ) {
 44         // Increase purchase count and earnings
 45         foreach ( $downloads as $download ) {
 46             edd_record_sale_in_log( $download['id'], $payment_id, $user_info );
 47             edd_increase_purchase_count( $download['id'] );
 48             $amount = null;
 49 
 50             if ( is_array( $cart_details ) ) {
 51                 foreach ( $cart_details as $key => $item ) {
 52                     if ( array_search( $download['id'], $item ) ) {
 53                         $cart_item_id = $key;
 54                     }
 55                 }
 56 
 57                 $amount = isset( $cart_details[$cart_item_id]['price'] ) ? $cart_details[$cart_item_id]['price'] : null;
 58             }
 59 
 60             $amount = edd_get_download_final_price( $download['id'], $user_info, $amount );
 61             edd_increase_earnings( $download['id'], $amount );
 62         }
 63 
 64         // Clear the total earnings cache
 65         delete_transient( 'edd_earnings_total' );
 66     }
 67 
 68     if ( isset( $user_info['discount'] ) && $user_info['discount'] != 'none' ) {
 69         edd_increase_discount_usage( $user_info['discount'] );
 70     }
 71 
 72     // Empty the shopping cart
 73     edd_empty_cart();
 74 }
 75 add_action( 'edd_update_payment_status', 'edd_complete_purchase', 100, 3 );
 76 
 77 /**
 78  * Record payment status change
 79  *
 80  * @since 1.4.3
 81  * @param int $payment_id the ID number of the payment
 82  * @param string $new_status the status of the payment, probably "publish"
 83  * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending"
 84  * @return void
 85  */
 86 function edd_record_status_change( $payment_id, $new_status, $old_status ) {
 87     if ( $new_status == 'publish' )
 88         $new_status = 'complete';
 89 
 90     if ( $old_status == 'publish' )
 91         $old_status = 'complete';
 92 
 93     $status_change = sprintf( __( 'Status changed from %s to %s', 'edd' ), $old_status, $new_status );
 94 
 95     edd_insert_payment_note( $payment_id, $status_change );
 96 }
 97 add_action( 'edd_update_payment_status', 'edd_record_status_change', 100, 3 );
 98 
 99 /**
100  * Update Edited Purchase
101  *
102  * Updates the purchase data for a payment.
103  * Used primarily for adding new downloads to a purchase.
104  *
105  * @since 1.0
106  * @param $data Arguments passed
107  * @return void
108  */
109 function edd_update_edited_purchase( $data ) {
110     if ( wp_verify_nonce( $data['edd-payment-nonce'], 'edd_payment_nonce' ) ) {
111         $payment_id = $_POST['payment-id'];
112 
113         $payment_data = edd_get_payment_meta( $payment_id );
114 
115         if ( isset( $_POST['edd-purchased-downloads'] ) ) {
116             $download_list = array();
117 
118             foreach ( $_POST['edd-purchased-downloads'] as $key => $download ) {
119                 if ( isset ( $download['options']['price_id'] ) ) {
120                     $download_list[] = array(
121                         'id' => $key,
122                         'options' => array(
123                             'price_id' => $download['options']['price_id']
124                         )
125                     );
126                 } else {
127                     $download_list[] = array( 'id' => $download );
128                 }
129             }
130 
131             $payment_data['downloads'] = serialize( $download_list );
132         }
133 
134         $user_info                 = maybe_unserialize( $payment_data['user_info'] );
135         $user_info['email']        = strip_tags( $_POST['edd-buyer-email'] );
136         $user_info['user_id']      = strip_tags( intval( $_POST['edd-buyer-user-id'] ) );
137         $payment_data['user_info'] = serialize( $user_info );
138 
139         update_post_meta( $payment_id, '_edd_payment_meta', $payment_data );
140         update_post_meta( $payment_id, '_edd_payment_user_email', strip_tags( $_POST['edd-buyer-email'] ) );
141         update_post_meta( $payment_id, '_edd_payment_user_id', strip_tags( intval( $_POST['edd-buyer-user-id'] ) ) );
142 
143         if ( ! empty( $_POST['edd-payment-note'] ) ) {
144             $note    = wp_kses( $_POST['edd-payment-note'], array() );
145             $note_id = edd_insert_payment_note( $payment_id, $note );
146         }
147 
148         if ( $_POST['edd-old-status'] != $_POST['edd-payment-status'] ) {
149             edd_update_payment_status( $payment_id, $_POST['edd-payment-status'] );
150         }
151 
152         if ( $_POST['edd-payment-status'] == 'publish' && isset( $_POST['edd-payment-send-email'] ) ) {
153             // Send the purchase receipt
154             edd_email_purchase_receipt( $payment_id, false );
155         }
156 
157         do_action( 'edd_update_edited_purchase', $payment_id );
158     }
159 }
160 add_action( 'edd_edit_payment', 'edd_update_edited_purchase' );
161 
162 /**
163  * Trigger a Purchase Deletion
164  *
165  * @since 1.3.4
166  * @param $data Arguments passed
167  * @return void
168  */
169 function edd_trigger_purchase_delete( $data ) {
170     if ( wp_verify_nonce( $data['_wpnonce'], 'edd_payment_nonce' ) ) {
171         $payment_id = absint( $data['purchase_id'] );
172         edd_delete_purchase( $payment_id );
173         wp_redirect( admin_url( '/edit.php?post_type=download&page=edd-payment-history&edd-message=payment_deleted' ) );
174         exit;
175     }
176 }
177 add_action( 'edd_delete_payment', 'edd_trigger_purchase_delete' );
178 
179 /**
180  * Flushes the total earning cache when a new payment is created
181  *
182  * @since 1.2
183  * @param int $payment Payment ID
184  * @param array $payment_data Payment Data
185  * @return void
186  */
187 function edd_clear_earnings_cache( $payment, $payment_data ) {
188     delete_transient( 'edd_total_earnings' );
189 }
190 add_action( 'edd_insert_payment', 'edd_clear_earnings_cache', 10, 2 );
191 
192 /**
193  * Flushes the current user's purchase history transient when a payment status
194  * is updated
195  *
196  * @since 1.2.2
197  * @param int $payment Payment ID
198  * @param string $new_status the status of the payment, probably "publish"
199  * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending"
200  * @return void
201  */
202 function edd_clear_user_history_cache( $payment_id, $new_status, $old_status ) {
203     $user_info = edd_get_payment_meta_user_info( $payment_id );
204 
205     delete_transient( 'edd_user_' . $user_info['id'] . '_purchases' );
206     delete_transient( md5( 'edd_customer_total_' . $user_info['email'] ) );
207 }
208 add_action( 'edd_update_payment_status', 'edd_clear_user_history_cache', 10, 3 );
209 
210 /**
211  * Updates all old payments, prior to 1.2, with new
212  * meta for the total purchase amount
213  *
214  * This is so that payments can be queried by their totals
215  *
216  * @since 1.2
217  * @param array $data Arguments passed
218  * @return void
219 */
220 function edd_update_old_payments_with_totals( $data ) {
221     if ( ! wp_verify_nonce( $data['_wpnonce'], 'edd_upgrade_payments_nonce' ) )
222         return;
223 
224     if ( get_option( 'edd_payment_totals_upgraded' ) )
225         return;
226 
227     $payments = edd_get_payments( array(
228         'offset' => 0,
229         'number' => -1,
230         'mode'   => 'all'
231     ) );
232 
233     if ( $payments ) {
234         foreach ( $payments as $payment ) {
235             $meta = edd_get_payment_meta( $payment->ID );
236             update_post_meta( $payment->ID, '_edd_payment_total', $meta['amount'] );
237         }
238     }
239 
240     add_option( 'edd_payment_totals_upgraded', 1 );
241 }
242 add_action( 'edd_upgrade_payments', 'edd_update_old_payments_with_totals' );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0