function eddrle_send_license_email($license_id, $d_id, $payment_id, $type)
    {
        // Sanity check! Is EDD 1.2+ active?
        if (!function_exists('edd_get_payment_meta')) {
            return;
        }
        $payment_meta = edd_get_payment_meta($payment_id);
        // Bail early if we don't have an email address. Tough luck, man!
        if (empty($payment_meta['email'])) {
            return;
        }
        $license_key = get_post_meta($license_id, '_edd_sl_key', true);
        // Bail early if we don't have a license key, for some reason.
        if (empty($license_key)) {
            return;
        }
        $download_title = get_the_title($d_id);
        // Bail early if there's no title. There should be, though!
        if (empty($download_title)) {
            return;
        }
        // A quick message. Hey don't forget to change this text!
        $message = 'A license key has been generated for your purchase of ' . $download_title . '. Please use the license key below to take advantage of one-click updates.

' . $license_key . '

Let me know if you have any questions,

Nate Wright
Theme of the Crop
http://themeofthecrop.com';
        // Send the email
        wp_mail($payment_meta['email'], 'License key generated for ' . $download_title, $message);
    }
/**
 * Create a log for preapproval payments
 *
 * @since Astoundify Crowdfunding 0.1-alpha
 *
 * @param int $payment_id the ID number of the payment
 * @param string $new_status the status of the payment, probably "publish"
 * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending"
 * @return void
 */
function atcf_pending_purchase($payment_id, $new_status, $old_status)
{
    global $edd_logs;
    // Make sure that payments are only completed once
    if ($old_status == 'publish' || $old_status == 'complete') {
        return;
    }
    // Make sure the payment completion is only processed when new status is complete
    if ($new_status != 'preapproval' && $new_status != 'complete') {
        return;
    }
    if (edd_is_test_mode() && !apply_filters('edd_log_test_payment_stats', false)) {
        return;
    }
    $payment_data = edd_get_payment_meta($payment_id);
    $downloads = maybe_unserialize($payment_data['downloads']);
    $user_info = maybe_unserialize($payment_data['user_info']);
    if (!is_array($downloads)) {
        return;
    }
    foreach ($downloads as $download) {
        if (!isset($download['quantity'])) {
            $download['quantity'] = 1;
        }
        for ($i = 0; $i < $download['quantity']; $i++) {
            $edd_logs->insert_log(array('post_parent' => $download['id'], 'log_type' => 'preapproval'), array('payment_id' => $payment_id));
        }
    }
}
/**
 * Email the download link(s) and payment confirmation to the buyer in a
 * customizable Purchase Receipt
 *
 * @since 1.0
 * @param int $payment_id Payment ID
 * @param bool $admin_notice Whether to send the admin email notification or not (default: true)
 * @return void
 */
function edd_email_purchase_receipt($payment_id, $admin_notice = true)
{
    $payment_data = edd_get_payment_meta($payment_id);
    $from_name = edd_get_option('from_name', wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES));
    $from_name = apply_filters('edd_purchase_from_name', $from_name, $payment_id, $payment_data);
    $from_email = edd_get_option('from_email', get_bloginfo('admin_email'));
    $from_email = apply_filters('edd_purchase_from_address', $from_email, $payment_id, $payment_data);
    $to_email = edd_get_payment_user_email($payment_id);
    $subject = edd_get_option('purchase_subject', __('Purchase Receipt', 'edd'));
    $subject = apply_filters('edd_purchase_subject', wp_strip_all_tags($subject), $payment_id);
    $subject = edd_do_email_tags($subject, $payment_id);
    $heading = edd_get_option('purchase_heading', __('Purchase Receipt', 'edd'));
    $heading = apply_filters('edd_purchase_heading', $heading, $payment_id, $payment_data);
    $attachments = apply_filters('edd_receipt_attachments', array(), $payment_id, $payment_data);
    $message = edd_do_email_tags(edd_get_email_body_content($payment_id, $payment_data), $payment_id);
    $emails = EDD()->emails;
    $emails->__set('from_name', $from_name);
    $emails->__set('from_email', $from_email);
    $emails->__set('heading', $heading);
    $headers = apply_filters('edd_receipt_headers', $emails->get_headers(), $payment_id, $payment_data);
    $emails->__set('headers', $headers);
    $emails->send($to_email, $subject, $message, $attachments);
    if ($admin_notice && !edd_admin_notices_disabled($payment_id)) {
        do_action('edd_admin_sale_notice', $payment_id, $payment_data);
    }
}
/**
 * Increment the backer count for every download in this payment. 
 *
 * @param 	int 		$payment_id
 * @param 	string 		$direction
 * @return 	void
 * @since 	0.9
 */
function atcf_update_backer_count($payment_id, $direction)
{
    $payment_data = edd_get_payment_meta($payment_id);
    $downloads = maybe_unserialize($payment_data['downloads']);
    if (!is_array($downloads)) {
        return;
    }
    foreach ($downloads as $download) {
        $variable_pricing = edd_get_variable_prices($download['id']);
        foreach ($variable_pricing as $key => $value) {
            $what = isset($download['options']['price_id']) ? intval($download['options']['price_id']) : 0;
            if (!isset($variable_pricing[$what]['bought'])) {
                $variable_pricing[$what]['bought'] = 0;
            }
            $current = $variable_pricing[$what]['bought'];
            if ($key == $what) {
                if ('increase' == $direction) {
                    $variable_pricing[$what]['bought'] = $current + $download['quantity'];
                } else {
                    $variable_pricing[$what]['bought'] = $current - $download['quantity'];
                }
            }
        }
        update_post_meta($download['id'], 'edd_variable_prices', $variable_pricing);
    }
}
Example #5
0
 /**
  * Trigger the tickets email
  *
  * @param int $payment_id
  *
  * @return string
  */
 public function trigger($payment_id = 0)
 {
     global $edd_options;
     $payment_data = edd_get_payment_meta($payment_id);
     $user_id = edd_get_payment_user_id($payment_id);
     $user_info = maybe_unserialize($payment_data['user_info']);
     $email = edd_get_payment_user_email($payment_id);
     if (isset($user_id) && $user_id > 0) {
         $user_data = get_userdata($user_id);
         $name = $user_data->display_name;
     } elseif (isset($user_info['first_name']) && isset($user_info['last_name'])) {
         $name = $user_info['first_name'] . ' ' . $user_info['last_name'];
     } else {
         $name = $email;
     }
     $message = $this->get_content_html($payment_id);
     $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
     $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
     $subject = !empty($edd_options['ticket_subject']) ? wp_strip_all_tags($edd_options['ticket_subject'], true) : $this->default_subject;
     $subject = apply_filters('edd_ticket_receipt_subject', $subject, $payment_id);
     $subject = edd_email_template_tags($subject, $payment_data, $payment_id);
     $headers = 'From: ' . stripslashes_deep(html_entity_decode($from_name, ENT_COMPAT, 'UTF-8')) . " <{$from_email}>\r\n";
     $headers .= 'Reply-To: ' . $from_email . "\r\n";
     $headers .= "Content-Type: text/html; charset=utf-8\r\n";
     $headers = apply_filters('edd_ticket_receipt_headers', $headers, $payment_id, $payment_data);
     // Allow add-ons to add file attachments
     $attachments = apply_filters('edd_ticket_receipt_attachments', array(), $payment_id, $payment_data);
     if (apply_filters('edd_email_ticket_receipt', true)) {
         wp_mail($email, $subject, $message, $headers, $attachments);
     }
 }
/**
 * Add upsells to order details screen
 *
 * @since 1.0
*/
function edd_csau_view_order_details_upsells($payment_id)
{
    $item = get_post($payment_id);
    $payment_meta = edd_get_payment_meta($payment_id);
    $cart_items = edd_get_payment_meta_cart_details($payment_id);
    $user_info = edd_get_payment_meta_user_info($payment_id);
    $user_id = edd_get_payment_user_id($payment_id);
    $payment_date = strtotime($item->post_date);
    if (!get_post_meta($payment_id, '_edd_payment_upsell_total', true)) {
        return;
    }
    ?>
<div id="edd-purchased-files" class="postbox">
	<h3 class="hndle"><?php 
    _e('Upsells included with this payment', 'edd-csau');
    ?>
</h3>
	<div class="inside">
		<table class="wp-list-table widefat fixed" cellspacing="0">
			<tbody id="the-list">
				<?php 
    if ($cart_items) {
        $i = 0;
        foreach ($cart_items as $key => $cart_item) {
            $id = isset($payment_meta['cart_details']) ? $cart_item['id'] : $cart_item;
            $price_override = isset($payment_meta['cart_details']) ? $cart_item['price'] : null;
            $price = edd_get_download_final_price($id, $user_info, $price_override);
            if (!isset($cart_item['item_number']['upsell'])) {
                continue;
            }
            ?>
						<tr class="<?php 
            if ($i % 2 == 0) {
                echo 'alternate';
            }
            ?>
">
							<td class="name column-name">
								<?php 
            echo '<a href="' . admin_url('post.php?post=' . $id . '&action=edit') . '">' . get_the_title($id) . '</a>';
            if (isset($cart_items[$key]['item_number'])) {
                $price_options = $cart_items[$key]['item_number']['options'];
                if (isset($price_options['price_id'])) {
                    echo ' - ' . edd_get_price_option_name($id, $price_options['price_id'], $payment_id);
                }
            }
            ?>
							</td>
						</tr>
						<?php 
            $i++;
        }
    }
    ?>
			</tbody>
		</table>
	</div>
</div>
<?php 
}
Example #7
0
/**
 * Add the "Send Product Updates?" option to payment history page
 * 
 * @access public
 * @param mixed $payment_id
 * @return void
 */
function edd_pup_order_history($payment_id)
{
    $payment_meta = edd_get_payment_meta($payment_id);
    $sendupdates = isset($payment_meta['edd_send_prod_updates']) ? $payment_meta['edd_send_prod_updates'] : true;
    ob_start();
    ?>
			<div class="edd-admin-box-inside edd-send-updates">
				<p>
					<span class="label" title="<?php 
    _e('When checked, customer will receive product update emails.', 'edd-pup');
    ?>
"><i data-code="f463" class="dashicons dashicons-update"></i></span>&nbsp;
					<input type="checkbox" name="edd-send-product-updates" id="edd_send_product_updates" value="1"<?php 
    checked(true, $sendupdates, true);
    ?>
/>
					<label class="description" for="edd_send_product_updates"><?php 
    _e('Send Product Updates', 'edd-pup');
    ?>
</label>
				</p>
			</div>
			<?php 
    echo ob_get_clean();
}
/**
 * Has User Purchased
 *
 * Checks to see if a user has purchased a download.
 *
 * @access      public
 * @since       1.0
 * @param       int $user_id - the ID of the user to check
 * @param       array $downloads - Array of IDs to check if purchased. If an int is passed, it will be converted to an array
 * @param       int $variable_price_id - the variable price ID to check for
 * @return      boolean - true if has purchased, false otherwise
*/
function edd_has_user_purchased($user_id, $downloads, $variable_price_id = null)
{
    if (!is_user_logged_in()) {
        return false;
    }
    // at some point this should support email checking
    $users_purchases = edd_get_users_purchases($user_id);
    $return = false;
    if (!is_array($downloads)) {
        $downloads = array($downloads);
    }
    if ($users_purchases) {
        foreach ($users_purchases as $purchase) {
            $purchase_meta = edd_get_payment_meta($purchase->ID);
            $purchased_files = maybe_unserialize($purchase_meta['downloads']);
            if (is_array($purchased_files)) {
                foreach ($purchased_files as $download) {
                    if (in_array($download['id'], $downloads)) {
                        if (!is_null($variable_price_id) && $variable_price_id !== false) {
                            if ($variable_price_id == $download['options']['price_id']) {
                                return true;
                            } else {
                                $return = false;
                            }
                        } else {
                            $return = true;
                        }
                    }
                }
            }
        }
    }
    return $return;
}
/**
 * Build the purchase email.
 *
 * Figure out who to send to, who it's from, etc.
 *
 * @since Astoundify Crowdfunding 0.1-alpha
 *
 * @param int $payment_id The ID of the payment
 * @param boolean $admin_notice Alert admins, or not
 * @return void
 */
function atcf_email_pending_purchase_receipt($payment_id, $admin_notice = true)
{
    global $edd_options;
    $payment_data = edd_get_payment_meta($payment_id);
    $user_info = maybe_unserialize($payment_data['user_info']);
    if (isset($user_info['id']) && $user_info['id'] > 0) {
        $user_data = get_userdata($user_info['id']);
        $name = $user_data->display_name;
    } elseif (isset($user_info['first_name']) && isset($user_info['last_name'])) {
        $name = $user_info['first_name'] . ' ' . $user_info['last_name'];
    } else {
        $name = $user_info['email'];
    }
    $message = edd_get_email_body_header();
    $message .= atcf_get_email_body_content($payment_id, $payment_data);
    $message .= edd_get_email_body_footer();
    $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
    $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
    $subject = apply_filters('atcf_pending_purchase_subject', __('Your pledge has been received', 'atcf'), $payment_id);
    $subject = edd_email_template_tags($subject, $payment_data, $payment_id);
    $headers = "From: " . stripslashes_deep(html_entity_decode($from_name, ENT_COMPAT, 'UTF-8')) . " <{$from_email}>\r\n";
    $headers .= "Reply-To: " . $from_email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    // Allow add-ons to add file attachments
    $attachments = apply_filters('atcf_pending_receipt_attachments', array(), $payment_id, $payment_data);
    wp_mail($payment_data['email'], $subject, $message, $headers, $attachments);
    if ($admin_notice) {
        do_action('edd_admin_pending_purchase_notice', $payment_id, $payment_data);
    }
}
Example #10
0
/**
 * Shows the acquisition method on the payment details in admin
 *
 * @since  1.0
 * @param  int $payment_id The Payment ID
 * @return void
 */
function edd_acq_method_payment_details($payment_id)
{
    $acquisition_method = edd_get_payment_meta($payment_id, '_edd_payment_acquisition_method', true);
    if (!empty($acquisition_method)) {
        $current_methods = edd_acq_get_methods();
        foreach ($current_methods as $method) {
            if ($method['value'] === $acquisition_method) {
                $acquisition_name = $method['name'];
                break;
            }
        }
        if (empty($acquisition_name)) {
            $acquisition_name = $acquisition_method . ' - ' . __('inactive', 'edd-acquisition-survey');
        }
        ?>
		<div class="edd-order-acquisition-method edd-admin-box-inside">
			<p>
				<span class="label"><?php 
        _e('Acquisition:', 'edd-acquisition-survey');
        ?>
</span>&nbsp;
				<span><?php 
        echo $acquisition_name;
        ?>
</span>
			</p>
		</div>
		<?php 
    }
}
/**
 * Admin notification 
 */
function edd_pre_approval_emails_send_admin_email($payment_id = 0, $payment_data = array())
{
    $payment_id = absint($payment_id);
    // get payment data from payment ID
    $payment_data = edd_get_payment_meta($payment_id);
    if (empty($payment_id)) {
        return;
    }
    if (!edd_get_payment_by('id', $payment_id)) {
        return;
    }
    $from_name = edd_get_option('from_name', wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES));
    $from_name = apply_filters('edd_purchase_from_name', $from_name, $payment_id, $payment_data);
    $from_email = edd_get_option('from_email', get_bloginfo('admin_email'));
    $from_email = apply_filters('edd_purchase_from_address', $from_email, $payment_id, $payment_data);
    $subject = sprintf(__('New pledge - Order #%1$s', 'edd-pre-approval-emails'), $payment_id);
    $subject = wp_strip_all_tags($subject);
    $subject = edd_do_email_tags($subject, $payment_id);
    $headers = "From: " . stripslashes_deep(html_entity_decode($from_name, ENT_COMPAT, 'UTF-8')) . " <{$from_email}>\r\n";
    $headers .= "Reply-To: " . $from_email . "\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    $headers = apply_filters('edd_admin_pledge_notification_headers', $headers, $payment_id, $payment_data);
    $attachments = apply_filters('edd_admin_pledge_notification_attachments', array(), $payment_id, $payment_data);
    $message = edd_pre_approval_emails_get_admin_email_body($payment_id, $payment_data);
    if (class_exists('EDD_Emails')) {
        $emails = EDD()->emails;
        $emails->__set('from_name', $from_name);
        $emails->__set('from_email', $from_email);
        $emails->__set('headers', $headers);
        $emails->__set('heading', __('New Pledge!', 'edd-pre-approval-emails'));
        $emails->send(edd_get_admin_notice_emails(), $subject, $message, $attachments);
    }
}
/**
 * Helper Functions
 */
function wc_edd_email_purchase_receipt($payment_id, $download_id)
{
    $payment_data = edd_get_payment_meta($payment_id);
    $download = get_post($download_id);
    $license = edd_software_licensing()->get_license_by_purchase($payment_id, $download_id);
    $from_name = edd_get_option('from_name', wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES));
    $from_name = apply_filters('edd_purchase_from_name', $from_name, $payment_id, $payment_data);
    $from_email = edd_get_option('from_email', get_bloginfo('admin_email'));
    $from_email = apply_filters('edd_purchase_from_address', $from_email, $payment_id, $payment_data);
    $to_email = edd_get_payment_user_email($payment_id);
    $subject = edd_get_option('purchase_subject', __('New License Key', 'edd'));
    $subject = apply_filters('edd_purchase_subject', wp_strip_all_tags($subject), $payment_id);
    $subject = edd_do_email_tags($subject, $payment_id);
    $message = "Dear " . edd_email_tag_first_name($payment_id) . ",\n\n";
    $message .= "As you have updated " . $download->post_title . ", please use following new license key to continue getting future updates: \n\n";
    $message .= "License Key : " . edd_software_licensing()->get_license_key($license->ID) . "\n\n";
    $message .= "Sorry for inconvenience.";
    $emails = EDD()->emails;
    $emails->__set('from_name', $from_name);
    $emails->__set('from_email', $from_email);
    $emails->__set('heading', __('Purchase Receipt', 'edd'));
    $headers = apply_filters('edd_receipt_headers', $emails->get_headers(), $payment_id, $payment_data);
    $emails->__set('headers', $headers);
    $emails->send($to_email, $subject, $message, array());
    if ($admin_notice && !edd_admin_notices_disabled($payment_id)) {
        do_action('edd_admin_sale_notice', $payment_id, $payment_data);
    }
}
Example #13
0
function edd_ti_metabox($payment_id)
{
    $tracking_id = edd_ti_get_payment_tracking_id($payment_id);
    $was_sent = edd_get_payment_meta($payment_id, 'edd_tracking_info_sent', true);
    ?>
	<div id="edd-payment-tracking" class="postbox">
		<h3 class="hndle"><span><?php 
    _e('Tracking Info', 'edd-tracking-info');
    ?>
</span></h3>
		<div class="inside">
			<strong class="order-data-tracking-id"><?php 
    _e('Tracking ID:', 'edd-tracking-info');
    ?>
</strong><br/>
			<input type="text" name="edd_payment_tracking_id" value="<?php 
    echo $tracking_id;
    ?>
" class="regular-text" />
			<?php 
    if (!empty($tracking_id)) {
        ?>
			<?php 
        wp_nonce_field('edd-ti-send-tracking', 'edd-ti-send-tracking', false, true);
        ?>
			<?php 
        $notify_button_text = empty($was_sent) ? __('Send Tracking Info', 'edd-tracking-info') : __('Resend Tracking Info', 'edd-tracking-info');
        ?>
			<span class="button-secondary" id="edd-tracking-info-notify-customer" data-payment="<?php 
        echo $payment_id;
        ?>
"><?php 
        echo $notify_button_text;
        ?>
</span>
			<span class="edd-tracking-info-email-message"></span>
			<span class="spinner"></span>
			<p>
				<?php 
        _e('Track shipment', 'edd-tracking-info');
        ?>
:&nbsp;<a href="<?php 
        echo edd_ti_get_payment_tracking_link($payment_id);
        ?>
" target="_blank"><?php 
        echo $tracking_id;
        ?>
</a>
			</p>
			<?php 
    }
    ?>
			<div class="clear"></div>
		</div><!-- /.inside -->
	</div><!-- /#edd-payment-notes -->
	<?php 
}
 /**
  * Get the Export Data
  *
  * @access public
  * @since 1.4.4
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @return array $data The data for the CSV file
  */
 public function get_data()
 {
     global $wpdb, $edd_options;
     $data = array();
     $payments = edd_get_payments(array('offset' => 0, 'number' => -1, 'mode' => edd_is_test_mode() ? 'test' : 'live', 'status' => isset($_POST['edd_export_payment_status']) ? $_POST['edd_export_payment_status'] : 'any', 'month' => isset($_POST['month']) ? absint($_POST['month']) : date('n'), 'year' => isset($_POST['year']) ? absint($_POST['year']) : date('Y')));
     foreach ($payments as $payment) {
         $payment_meta = edd_get_payment_meta($payment->ID);
         $user_info = edd_get_payment_meta_user_info($payment->ID);
         $downloads = edd_get_payment_meta_cart_details($payment->ID);
         $total = isset($payment_meta['amount']) ? $payment_meta['amount'] : 0.0;
         $user_id = isset($user_info['id']) && $user_info['id'] != -1 ? $user_info['id'] : $user_info['email'];
         $products = '';
         $skus = '';
         if ($downloads) {
             foreach ($downloads as $key => $download) {
                 // Download ID
                 $id = isset($payment_meta['cart_details']) ? $download['id'] : $download;
                 // If the download has variable prices, override the default price
                 $price_override = isset($payment_meta['cart_details']) ? $download['price'] : null;
                 $price = edd_get_download_final_price($id, $user_info, $price_override);
                 // Display the Downoad Name
                 $products .= get_the_title($id) . ' - ';
                 if (edd_use_skus()) {
                     $sku = edd_get_download_sku($id);
                     if (!empty($sku)) {
                         $skus .= $sku;
                     }
                 }
                 if (isset($downloads[$key]['item_number']) && isset($downloads[$key]['item_number']['options'])) {
                     $price_options = $downloads[$key]['item_number']['options'];
                     if (isset($price_options['price_id'])) {
                         $products .= edd_get_price_option_name($id, $price_options['price_id']) . ' - ';
                     }
                 }
                 $products .= html_entity_decode(edd_currency_filter($price));
                 if ($key != count($downloads) - 1) {
                     $products .= ' / ';
                     if (edd_use_skus()) {
                         $skus .= ' / ';
                     }
                 }
             }
         }
         if (is_numeric($user_id)) {
             $user = get_userdata($user_id);
         } else {
             $user = false;
         }
         $data[] = array('id' => $payment->ID, 'email' => $payment_meta['email'], 'first' => $user_info['first_name'], 'last' => $user_info['last_name'], 'products' => $products, 'skus' => $skus, 'amount' => html_entity_decode(edd_format_amount($total)), 'tax' => html_entity_decode(edd_get_payment_tax($payment->ID, $payment_meta)), 'discount' => isset($user_info['discount']) && $user_info['discount'] != 'none' ? $user_info['discount'] : __('none', 'edd'), 'gateway' => edd_get_gateway_admin_label(get_post_meta($payment->ID, '_edd_payment_gateway', true)), 'key' => $payment_meta['key'], 'date' => $payment->post_date, 'user' => $user ? $user->display_name : __('guest', 'edd'), 'status' => edd_get_payment_status($payment, true));
         if (!edd_use_skus()) {
             unset($data['skus']);
         }
     }
     $data = apply_filters('edd_export_get_data', $data);
     $data = apply_filters('edd_export_get_data_' . $this->export_type, $data);
     return $data;
 }
/**
 * Email Download Purchase Receipt
 *
 * Email the download link(s) and payment confirmation to the buyer.
 *
 * @access      private
 * @since       1.0
 * @return      void
*/
function edd_email_purchase_receipt($payment_id, $admin_notice = true)
{
    global $edd_options;
    $payment_data = edd_get_payment_meta($payment_id);
    $user_info = maybe_unserialize($payment_data['user_info']);
    if (isset($user_info['id']) && $user_info['id'] > 0) {
        $user_data = get_userdata($user_info['id']);
        $name = $user_data->display_name;
    } elseif (isset($user_info['first_name']) && isset($user_info['last_name'])) {
        $name = $user_info['first_name'] . ' ' . $user_info['last_name'];
    } else {
        $name = $user_info['email'];
    }
    $message = edd_get_email_body_header();
    $message .= edd_get_email_body_content($payment_id, $payment_data);
    $message .= edd_get_email_body_footer();
    $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
    $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
    $subject = isset($edd_options['purchase_subject']) && strlen(trim($edd_options['purchase_subject'])) > 0 ? edd_email_template_tags($edd_options['purchase_subject'], $payment_data, $payment_id) : __('Purchase Receipt', 'edd');
    $headers = "From: " . stripslashes_deep(html_entity_decode($from_name, ENT_COMPAT, 'UTF-8')) . " <{$from_email}>\r\n";
    $headers .= "Reply-To: " . $from_email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    // allow add-ons to add file attachments
    $attachments = apply_filters('edd_receipt_attachments', array(), $payment_id, $payment_data);
    wp_mail($payment_data['email'], $subject, $message, $headers, $attachments);
    if ($admin_notice) {
        /* send an email notification to the admin */
        $admin_email = edd_get_admin_notice_emails();
        $admin_subject = apply_filters('edd_admin_purchase_notification_subject', __('New download purchase', 'edd'), $payment_id, $payment_data);
        $admin_message = __('Hello', 'edd') . "\n\n" . sprintf(__('A %s purchase has been made', 'edd'), edd_get_label_plural()) . ".\n\n";
        $admin_message .= sprintf(__('%s sold:', 'edd'), edd_get_label_plural()) . "\n\n";
        $download_list = '';
        $downloads = maybe_unserialize($payment_data['downloads']);
        if (is_array($downloads)) {
            foreach ($downloads as $download) {
                $id = isset($payment_data['cart_details']) ? $download['id'] : $download;
                $download_list .= html_entity_decode(get_the_title($id), ENT_COMPAT, 'UTF-8') . "\n";
            }
        }
        $gateway = edd_get_gateway_admin_label(get_post_meta($payment_id, '_edd_payment_gateway', true));
        $admin_message .= $download_list . "\n";
        $admin_message .= __('Purchased by: ', 'edd') . " " . html_entity_decode($name, ENT_COMPAT, 'UTF-8') . "\n";
        $admin_message .= __('Amount: ', 'edd') . " " . html_entity_decode(edd_currency_filter(edd_format_amount($payment_data['amount'])), ENT_COMPAT, 'UTF-8') . "\n\n";
        $admin_message .= __('Payment Method: ', 'edd') . " " . $gateway . "\n\n";
        $admin_message .= __('Thank you', 'edd');
        $admin_message = apply_filters('edd_admin_purchase_notification', $admin_message, $payment_id, $payment_data);
        $admin_headers = apply_filters('edd_admin_purchase_notification_headers', array(), $payment_id, $payment_data);
        $admin_attachments = apply_filters('edd_admin_purchase_notification_attachments', array(), $payment_id, $payment_data);
        wp_mail($admin_email, $admin_subject, $admin_message, $admin_headers, $admin_attachments);
    }
}
 /**
  * Get the data being exported
  *
  * @return array $data
  */
 public function get_data()
 {
     global $wpdb;
     $data = array();
     $campaign = $this->campaign;
     $campaign = atcf_get_campaign($campaign);
     $backers = $campaign->backers();
     if (empty($backers)) {
         return $data;
     }
     foreach ($backers as $log) {
         $payment_id = get_post_meta($log->ID, '_edd_log_payment_id', true);
         $payment = get_post($payment_id);
         $payment_meta = edd_get_payment_meta($payment_id);
         $user_info = edd_get_payment_meta_user_info($payment_id);
         $downloads = edd_get_payment_meta_cart_details($payment_id);
         $total = edd_get_payment_amount($payment_id);
         $user_id = isset($user_info['id']) && $user_info['id'] != -1 ? $user_info['id'] : $user_info['email'];
         $products = '';
         if ($downloads) {
             foreach ($downloads as $key => $download) {
                 // Download ID
                 $id = isset($payment_meta['cart_details']) ? $download['id'] : $download;
                 // If the download has variable prices, override the default price
                 $price_override = isset($payment_meta['cart_details']) ? $download['price'] : null;
                 $price = edd_get_download_final_price($id, $user_info, $price_override);
                 // Display the Downoad Name
                 $products .= get_the_title($id) . ' - ';
                 if (isset($downloads[$key]['item_number'])) {
                     $price_options = $downloads[$key]['item_number']['options'];
                     if (isset($price_options['price_id'])) {
                         $products .= edd_get_price_option_name($id, $price_options['price_id']) . ' - ';
                     }
                 }
                 $products .= html_entity_decode(edd_currency_filter($price));
                 if ($key != count($downloads) - 1) {
                     $products .= ' / ';
                 }
             }
         }
         if (is_numeric($user_id)) {
             $user = get_userdata($user_id);
         } else {
             $user = false;
         }
         $shipping = isset($payment_meta['shipping']) ? $payment_meta['shipping'] : null;
         $data[] = apply_filters('atcf_csv_cols_values', array('id' => $payment_id, 'email' => $payment_meta['email'], 'first' => $user_info['first_name'], 'last' => $user_info['last_name'], 'shipping' => isset($shipping) ? implode("\n", $shipping) : '', 'products' => $products, 'amount' => html_entity_decode(edd_currency_filter(edd_format_amount($total))), 'tax' => html_entity_decode(edd_payment_tax($payment_id, $payment_meta)), 'discount' => isset($user_info['discount']) && $user_info['discount'] != 'none' ? $user_info['discount'] : __('none', 'atcf'), 'gateway' => edd_get_gateway_admin_label(get_post_meta($payment_id, '_edd_payment_gateway', true)), 'key' => $payment_meta['key'], 'date' => date_i18n(get_option('date_format'), strtotime($payment->post_date)), 'user' => $user ? $user->display_name : __('guest', 'atcf'), 'status' => edd_get_payment_status($payment, true)), $payment_id);
     }
     $data = apply_filters('edd_export_get_data', $data);
     $data = apply_filters('edd_export_get_data_' . $this->export_type, $data);
     return $data;
 }
 /**
  * Add points for purchase
  * 
  * Handles to add points for purchases
  *
  * @package Easy Digital Downloads - Points and Rewards
  * @since 1.0.0
  */
 public function edd_points_add_point_for_complete_purchase($payment_id)
 {
     global $edd_options, $current_user;
     //get payment data
     $paymentdata = edd_get_payment_meta($payment_id);
     $userdata = edd_get_payment_meta_user_info($payment_id);
     $user_id = isset($userdata['id']) && !empty($userdata['id']) ? $userdata['id'] : 0;
     //get discount towards points
     $gotdiscount = $this->model->edd_points_get_payment_discount($payment_id);
     //check user has redeemed points or not & user_id should not empty
     if (isset($gotdiscount) && !empty($gotdiscount) && !empty($user_id)) {
         //get discounte price from points
         $discountedpoints = $this->model->edd_points_calculate_points($gotdiscount);
         //update user points
         edd_points_minus_points_from_user($discountedpoints, $user_id);
         //points label
         $pointslable = $this->model->edd_points_get_points_label($discountedpoints);
         //record data logs for redeem for purchase
         $post_data = array('post_title' => sprintf(__('Redeem %s for purchase', 'eddpoints'), $pointslable), 'post_content' => sprintf(__('%s redeemed for purchasing download by redeeming the points and get discounts.', 'eddpoints'), $pointslable), 'post_author' => $user_id);
         //log meta array
         $log_meta = array('userpoint' => $discountedpoints, 'events' => 'redeemed_purchase', 'operation' => 'minus');
         //insert entry in log
         $this->logs->edd_points_insert_logs($post_data, $log_meta);
         // set order meta, regardless of whether any points were earned, just so we know the process took place
         update_post_meta($payment_id, '_edd_points_order_redeemed', $discountedpoints);
     }
     //end if to check points redeemed taken by buyer or not
     // get cartdata from older order
     $cartdata = edd_get_payment_meta_cart_details($payment_id);
     //get bought points for points downloads types
     $boughtpoints = $this->model->edd_points_get_bought_points($cartdata);
     //get cart points from cartdata and payment discount given to user
     $cartpoints = $this->model->edd_points_get_user_checkout_points($cartdata, $gotdiscount);
     //add bought points to cart points
     $cartpoints = !empty($boughtpoints) ? $cartpoints + $boughtpoints : $cartpoints;
     //check checkout points earned points or user id is not empty
     if (!empty($cartpoints) && !empty($user_id)) {
         //points label
         $pointslable = $this->model->edd_points_get_points_label($cartpoints);
         //get user points after subtracting the redemption points
         $userpoints = edd_points_get_user_points();
         $post_data = array('post_title' => sprintf(__('%s earned for purchasing the downloads.', 'eddpoints'), $pointslable), 'post_content' => sprintf(__('Get %s for purchasing the downloads.', 'eddpoints'), $pointslable), 'post_author' => $user_id);
         $log_meta = array('userpoint' => $cartpoints, 'events' => 'earned_purchase', 'operation' => 'add');
         //insert entry in log
         $this->logs->edd_points_insert_logs($post_data, $log_meta);
         //update user points
         edd_points_add_points_to_user($cartpoints, $user_id);
         // set order meta, regardless of whether any points were earned, just so we know the process took place
         update_post_meta($payment_id, '_edd_points_order_earned', $cartpoints);
     }
     //end if to check checkout points should not empty
 }
 /**
  * {@inheritdoc}
  *
  * @param $object_id
  *
  * @return bool
  */
 public function triggered($object_id = null)
 {
     if ($this->options['implicit']) {
         return true;
     }
     if (!$object_id) {
         return false;
     }
     $meta = edd_get_payment_meta($object_id);
     if (is_array($meta) && isset($meta['_mc4wp_optin']) && $meta['_mc4wp_optin']) {
         return true;
     }
     return false;
 }
/**
 * Email template tags
 *
 * @param $message
 * @param $payment_data
 * @param $payment_id
 *
 * @return mixed
 */
function edd_pfui_email_template_tags($message, $payment_data, $payment_id)
{
    $payment_meta = edd_get_payment_meta($payment_id);
    $company = isset($payment_meta['company']) ? $payment_meta['company'] : 'none';
    // $first = isset( $payment_meta['first'] ) ? $payment_meta['first'] : 'none';
    // $last = isset( $payment_meta['last'] ) ? $payment_meta['last'] : 'none';
    $street = isset($payment_meta['street']) ? $payment_meta['street'] : 'none';
    $number = isset($payment_meta['number']) ? $payment_meta['number'] : 'none';
    $addition = isset($payment_meta['addition']) ? $payment_meta['addition'] : 'none';
    $zip = isset($payment_meta['zip']) ? $payment_meta['zip'] : 'none';
    $city = isset($payment_meta['city']) ? $payment_meta['city'] : 'none';
    $phone = isset($payment_meta['phone']) ? $payment_meta['phone'] : 'none';
    $mobile = isset($payment_meta['mobile']) ? $payment_meta['mobile'] : 'none';
    $fax = isset($payment_meta['fax']) ? $payment_meta['fax'] : 'none';
    // $email = isset( $payment_meta['email'] ) ? $payment_meta['email'] : 'none';
    $vat = isset($payment_meta['vat']) ? $payment_meta['vat'] : 'none';
    $event = isset($payment_meta['event']) ? $payment_meta['event'] : 'none';
    $location = isset($payment_meta['location']) ? $payment_meta['location'] : 'none';
    $reference = isset($payment_meta['reference']) ? $payment_meta['reference'] : 'none';
    $date_begin = isset($payment_meta['date_begin']) ? $payment_meta['date_begin'] : 'none';
    $time_begin = isset($payment_meta['time_begin']) ? $payment_meta['time_begin'] : 'none';
    $date_end = isset($payment_meta['date_end']) ? $payment_meta['date_end'] : 'none';
    $time_end = isset($payment_meta['time_end']) ? $payment_meta['time_end'] : 'none';
    $comments = isset($payment_meta['comments']) ? $payment_meta['comments'] : 'none';
    $message = str_replace('{company}', $company, $message);
    // $message = str_replace( '{first}', $first, $message );
    // $message = str_replace( '{last}', $last, $message );
    $message = str_replace('{street}', $street, $message);
    $message = str_replace('{number}', $number, $message);
    $message = str_replace('{addition}', $addition, $message);
    $message = str_replace('{zip}', $zip, $message);
    $message = str_replace('{city}', $city, $message);
    $message = str_replace('{phone}', $phone, $message);
    $message = str_replace('{mobile}', $mobile, $message);
    $message = str_replace('{fax}', $fax, $message);
    // $message = str_replace( '{email}', $email, $message );
    $message = str_replace('{vat}', $vat, $message);
    $message = str_replace('{event}', $event, $message);
    $message = str_replace('{location}', $location, $message);
    $message = str_replace('{reference}', $reference, $message);
    $message = str_replace('{date_begin}', $date_begin, $message);
    $message = str_replace('{time_begin}', $time_begin, $message);
    $message = str_replace('{date_end}', $date_end, $message);
    $message = str_replace('{time_end}', $time_end, $message);
    $message = str_replace('{comments}', $comments, $message);
    return $message;
}
/**
 * Track new users
 *
 * @since       1.0.0
 * @param       int $payment_id The ID of a given payment
 * @return      void
 */
function edd_customerio_connect_register_user($payment_id)
{
    // Bail if API isn't setup
    if (!edd_customerio_connect()->api) {
        return;
    }
    // Setup the request body
    $user_info = edd_get_payment_meta_user_info($payment_id);
    $payment_meta = edd_get_payment_meta($payment_id);
    $cart_items = isset($payment_meta['cart_details']) ? maybe_unserialize($payment_meta['cart_details']) : false;
    $user_name = false;
    if ($payment_meta['user_info']['first_name']) {
        $user_name = $payment_meta['user_info']['first_name'];
        if ($payment_meta['user_info']['last_name']) {
            $user_name .= ' ' . $payment_meta['user_info']['last_name'];
        }
    }
    $body = array('email' => $payment_meta['user_info']['email'], 'created_at' => $payment_meta['date']);
    if ($user_name) {
        $body['name'] = $user_name;
    }
    $response = edd_customerio_connect()->api->call($payment_meta['user_info']['id'], $body);
    // Track the purchases
    if (empty($cart_items) || !$cart_items) {
        $cart_items = maybe_unserialize($payment_meta['downloads']);
    }
    if ($cart_items) {
        $body = array('name' => 'purchased', 'data' => array('discount' => $payment_meta['user_info']['discount']));
        foreach ($cart_items as $key => $cart_item) {
            $item_id = isset($payment_meta['cart_details']) ? $cart_item['id'] : $cart_item;
            $price = $cart_item['price'];
            $body['data']['items'][$cart_item['id']] = array('price' => $price, 'product_id' => $cart_item['id'], 'product_name' => esc_attr($cart_item['name']));
            if (edd_has_variable_prices($cart_item['id'])) {
                $body['data']['items'][$cart_item['id']]['price_id'] = $cart_item['item_number']['options']['price_id'];
                $body['data']['items'][$cart_item['id']]['price_name'] = edd_get_price_option_name($cart_item['id'], $cart_item['item_number']['options']['price_id']);
                $body['data']['items'][$cart_item['id']]['quantity'] = $cart_item['item_number']['quantity'];
            } else {
                $body['data']['items'][$cart_item['id']]['quantity'] = $cart_item['quantity'];
            }
            if (edd_use_taxes()) {
                $body['data']['items'][$cart_item['id']]['tax'] = $cart_item['tax'];
            }
        }
        $response = edd_customerio_connect()->api->call($payment_meta['user_info']['id'], $body, 'POST', 'events');
    }
}
Example #21
0
 /**
  * @param int $payment_id The ID of the payment
  *
  * @return bool|string
  */
 public function subscribe_from_edd($payment_id)
 {
     $meta = edd_get_payment_meta($payment_id);
     if (!is_array($meta) || !isset($meta['_mc4wp_optin']) || !$meta['_mc4wp_optin']) {
         return false;
     }
     $email = (string) edd_get_payment_user_email($payment_id);
     $merge_vars = array();
     // add first and last name to merge vars, if given
     $user_info = (array) edd_get_payment_meta_user_info($payment_id);
     if (isset($user_info['first_name']) && isset($user_info['last_name'])) {
         $merge_vars['NAME'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
     }
     if (isset($user_info['first_name'])) {
         $merge_vars['FNAME'] = $user_info['first_name'];
     }
     if (isset($user_info['last_name'])) {
         $merge_vars['LNAME'] = $user_info['last_name'];
     }
     return $this->subscribe($email, $merge_vars, $this->type, $payment_id);
 }
 */
global $edd_receipt_args;
$payment = get_post($edd_receipt_args['id']);
if (empty($payment)) {
    ?>

	<div class="edd_errors edd-alert edd-alert-error">
		<?php 
    _e('The specified receipt ID appears to be invalid', 'easy-digital-downloads');
    ?>
	</div>

<?php 
    return;
}
$meta = edd_get_payment_meta($payment->ID);
$cart = edd_get_payment_meta_cart_details($payment->ID, true);
$user = edd_get_payment_meta_user_info($payment->ID);
$email = edd_get_payment_user_email($payment->ID);
$status = edd_get_payment_status($payment, true);
?>
<table id="edd_purchase_receipt">
	<thead>
		<?php 
do_action('edd_payment_receipt_before', $payment, $edd_receipt_args);
?>

		<?php 
if ($edd_receipt_args['payment_id']) {
    ?>
		<tr>
 /**
  * Save the shipping details on payment edit
  *
  * @since 1.5
  *
  * @access private
  * @return void
  */
 public function save_payment($payment_id = 0)
 {
     $address = isset($_POST['edd-payment-shipping-address']) ? $_POST['edd-payment-shipping-address'] : false;
     if (!$address) {
         return;
     }
     $meta = edd_get_payment_meta($payment_id);
     $user_info = maybe_unserialize($meta['user_info']);
     $user_info['shipping_info'] = $address[0];
     $meta['user_info'] = $user_info;
     update_post_meta($payment_id, '_edd_payment_meta', $meta);
     if (isset($_POST['edd-payment-shipped'])) {
         update_post_meta($payment_id, '_edd_payment_shipping_status', '2');
     } elseif (get_post_meta($payment_id, '_edd_payment_shipping_status', true)) {
         update_post_meta($payment_id, '_edd_payment_shipping_status', '1');
     }
 }
 /**
  * Send purchase details to MailChimp's Ecommerce360 extension.
  *
  * @param  integer $payment_id    [description]
  * @return bool
  */
 public function record_ecommerce360_purchase($payment_id = 0)
 {
     // Make sure an API key has been entered
     if (empty($this->key)) {
         return FALSE;
     }
     // Don't record details if we're in test mode
     if (edd_is_test_mode()) {
         return FALSE;
     }
     $payment = edd_get_payment_meta($payment_id);
     $user_info = edd_get_payment_meta_user_info($payment_id);
     $amount = edd_get_payment_amount($payment_id);
     $cart_details = edd_get_payment_meta_cart_details($payment_id);
     $tax = edd_get_payment_tax($payment_id);
     if (is_array($cart_details)) {
         $items = array();
         // Increase purchase count and earnings
         foreach ($cart_details as $index => $download) {
             // Get the categories that this download belongs to, if any
             $post = edd_get_download($download['id']);
             $terms = get_the_terms($download['id'], 'download_category');
             if ($terms && !is_wp_error($terms)) {
                 $categories = array();
                 foreach ($terms as $term) {
                     $categories[] = $term->name;
                 }
                 $category_id = $terms[0]->term_id;
                 $category_name = join(" - ", $categories);
             } else {
                 $category_id = 1;
                 $category_name = 'Download';
             }
             // "bundle" or "default"
             $download_type = edd_get_download_type($download['id']);
             $download['sku'] = edd_get_download_sku($download['id']);
             // if ( 'bundle' == $download_type ) {
             //   $downloads = edd_get_bundled_products( $download_id );
             //   if ( $downloads ) {
             //     foreach ( $downloads as $d_id ) {
             //       # Do something
             //     }
             //   }
             // }
             $item = array('line_num' => $index + 1, 'product_id' => (int) $download['id'], 'product_name' => $download['name'], 'category_id' => $category_id, 'category_name' => $category_name, 'qty' => $download['quantity'], 'cost' => $download['subtotal']);
             if ($download['sku'] !== '-') {
                 $item['sku'] = $download['sku'];
                 // optional, 30 char limit
             }
             $items[] = $item;
         }
         $order = array('id' => (string) $payment_id, 'email' => $user_info['email'], 'total' => $amount, 'store_id' => self::_edd_ec360_get_store_id(), 'store_name' => home_url(), 'items' => $items, 'order_date' => get_the_date('Y-n-j', $payment_id));
         // Set Ecommerce360 variables if they exist
         $campaign_id = get_post_meta($payment_id, '_edd_mc_campaign_id', true);
         $email_id = get_post_meta($payment_id, '_edd_mc_email_id', true);
         if (!empty($campaign_id)) {
             $order['campaign_id'] = $campaign_id;
         }
         if (!empty($email_id)) {
             $order['email_id'] = $email_id;
         }
         if ($tax != 0) {
             $order['tax'] = $tax;
             // double, optional
         }
         // Send to MailChimp
         $options = array('CURLOPT_FOLLOWLOCATION' => false);
         $mailchimp = new EDD_MailChimp_API($this->key, $options);
         try {
             $result = $mailchimp->call('ecomm/order-add', array('order' => $order));
             edd_insert_payment_note($payment_id, __('Order details have been added to MailChimp successfully', 'eddmc'));
         } catch (Exception $e) {
             edd_insert_payment_note($payment_id, __('MailChimp Ecommerce360 Error: ', 'eddmc') . $e->getMessage());
             return FALSE;
         }
         return TRUE;
     } else {
         return FALSE;
     }
 }
 *
 * @since 1.6
 * @return void
*/
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    wp_die(__('Payment ID not supplied. Please try again', 'edd'), __('Error', 'edd'));
}
// Setup the variables
$payment_id = absint($_GET['id']);
$number = edd_get_payment_number($payment_id);
$item = get_post($payment_id);
// Sanity check... fail if purchase ID is invalid
if (!is_object($item) || $item->post_type != 'edd_payment') {
    wp_die(__('The specified ID does not belong to a payment. Please try again', 'edd'), __('Error', 'edd'));
}
$payment_meta = edd_get_payment_meta($payment_id);
$transaction_id = esc_attr(edd_get_payment_transaction_id($payment_id));
$cart_items = edd_get_payment_meta_cart_details($payment_id);
$user_id = edd_get_payment_user_id($payment_id);
$customer_id = edd_get_payment_customer_id($payment_id);
$payment_date = strtotime($item->post_date);
$unlimited = edd_payment_has_unlimited_downloads($payment_id);
$user_info = edd_get_payment_meta_user_info($payment_id);
$address = !empty($user_info['address']) ? $user_info['address'] : array('line1' => '', 'line2' => '', 'city' => '', 'country' => '', 'state' => '', 'zip' => '');
$gateway = edd_get_payment_gateway($payment_id);
$currency_code = edd_get_payment_currency_code($payment_id);
?>
<div class="wrap edd-wrap">
	<h2><?php 
printf(__('Payment %s', 'edd'), $number);
?>
/**
 * Get Downloads Of Purchase
 *
 * Retrieves an array of all files purchased.
 *
 * @since 1.0
 * @deprecated 1.4
 *
 * @param int  $payment_id ID number of the purchase
 * @param null $payment_meta
 * @return bool|mixed
 */
function edd_get_downloads_of_purchase($payment_id, $payment_meta = null)
{
    $backtrace = debug_backtrace();
    _edd_deprecated_function(__FUNCTION__, '1.4', 'edd_get_payment_meta_downloads', $backtrace);
    if (is_null($payment_meta)) {
        $payment_meta = edd_get_payment_meta($payment_id);
    }
    $downloads = maybe_unserialize($payment_meta['downloads']);
    if ($downloads) {
        return $downloads;
    }
    return false;
}
/**
 * Listens to the updated_postmeta hook for our backwards compatible payment_meta updates, and runs through them
 *
 * @since  2.3
 * @param  int $meta_id    The Meta ID that was updated
 * @param  int $object_id  The Object ID that was updated (post ID)
 * @param  string $meta_key   The Meta key that was updated
 * @param  string|int|float $meta_value The Value being updated
 * @return bool|int             If successful the number of rows updated, if it fails, false
 */
function edd_update_payment_backwards_compat($meta_id, $object_id, $meta_key, $meta_value)
{
    $meta_keys = array('_edd_payment_meta', '_edd_payment_tax');
    if (!in_array($meta_key, $meta_keys)) {
        return;
    }
    global $wpdb;
    switch ($meta_key) {
        case '_edd_payment_meta':
            $meta_value = maybe_unserialize($meta_value);
            if (!isset($meta_value['tax'])) {
                return;
            }
            $tax_value = $meta_value['tax'];
            $data = array('meta_value' => $tax_value);
            $where = array('post_id' => $object_id, 'meta_key' => '_edd_payment_tax');
            $data_format = array('%f');
            $where_format = array('%d', '%s');
            break;
        case '_edd_payment_tax':
            $tax_value = !empty($meta_value) ? $meta_value : 0;
            $current_meta = edd_get_payment_meta($object_id, '_edd_payment_meta', true);
            $current_meta['tax'] = $tax_value;
            $new_meta = maybe_serialize($current_meta);
            $data = array('meta_value' => $new_meta);
            $where = array('post_id' => $object_id, 'meta_key' => '_edd_payment_meta');
            $data_format = array('%s');
            $where_format = array('%d', '%s');
            break;
    }
    $updated = $wpdb->update($wpdb->postmeta, $data, $where, $data_format, $where_format);
    if (!empty($updated)) {
        // Since we did a direct DB query, clear the postmeta cache.
        wp_cache_delete($object_id, 'post_meta');
    }
    return $updated;
}
 /**
  * Generate license keys for a purchase
  *
  * Generates ( if needed ) a license key for the buyer at time of purchase
  * This key will be used to activate all products for this purchase
  *
  * @access      private
  * @since       1.5
  * @return      void
  */
 function generate_license($download_id = 0, $payment_id = 0, $type = 'default', $cart_item = array(), $cart_index = 0)
 {
     // Bail if this cart item is for a renewal
     if (!empty($cart_item['item_number']['options']['is_renewal'])) {
         return;
     }
     // Bail if this cart item is for an upgrade
     if (!empty($cart_item['item_number']['options']['is_upgrade'])) {
         return;
     }
     $keys = array();
     $user_info = edd_get_payment_meta_user_info($payment_id);
     $bundle_licensing = 'bundle' == $type && (bool) get_post_meta($download_id, '_edd_sl_enabled', true);
     $parent_license_id = 0;
     $activation_limit = false;
     if ($type == 'bundle') {
         $downloads = array();
         if ($bundle_licensing) {
             $downloads[] = $download_id;
         }
         $downloads = array_merge($downloads, edd_get_bundled_products($download_id));
         if (edd_has_variable_prices($download_id)) {
             $activation_limit = edd_software_licensing()->get_price_activation_limit($download_id, $cart_item['item_number']['options']['price_id']);
             $is_lifetime = edd_software_licensing()->get_price_is_lifetime($download_id, $cart_item['item_number']['options']['price_id']);
         }
     } else {
         if (edd_has_variable_prices($download_id)) {
             $is_lifetime = edd_software_licensing()->get_price_is_lifetime($download_id, $cart_item['item_number']['options']['price_id']);
         }
         $downloads = array();
         $downloads[] = $download_id;
     }
     if (!is_array($downloads)) {
         return;
     }
     foreach ($downloads as $d_id) {
         if (!get_post_meta($d_id, '_edd_sl_enabled', true)) {
             continue;
         }
         $license_title = get_the_title($d_id) . ' - ' . $user_info['email'];
         $license_args = array('post_type' => 'edd_license', 'post_title' => $license_title, 'post_status' => 'publish', 'post_date' => get_post_field('post_date', $payment_id, 'raw'));
         if ($parent_license_id) {
             $license_args['post_parent'] = $parent_license_id;
         }
         $license_id = wp_insert_post(apply_filters('edd_sl_insert_license_args', $license_args));
         if ($bundle_licensing && $download_id == $d_id && !$parent_license_id) {
             $parent_license_id = $license_id;
         }
         $keys[] = $license_id;
         $license_key = self::get_new_download_license_key($d_id);
         if (!$license_key) {
             // No predefined license key available, generate a random one
             $license_key = self::generate_license_key($license_id, $d_id, $payment_id, $cart_index);
         }
         $price_id = isset($cart_item['item_number']['options']['price_id']) ? (int) $cart_item['item_number']['options']['price_id'] : false;
         add_post_meta($license_id, '_edd_sl_download_id', $d_id);
         if (false !== $price_id) {
             add_post_meta($license_id, '_edd_sl_download_price_id', $price_id);
         }
         add_post_meta($license_id, '_edd_sl_cart_index', $cart_index);
         add_post_meta($license_id, '_edd_sl_payment_id', $payment_id);
         add_post_meta($license_id, '_edd_sl_key', $license_key);
         add_post_meta($license_id, '_edd_sl_user_id', $user_info['id']);
         add_post_meta($license_id, '_edd_sl_status', 'inactive');
         add_post_meta($license_id, '_edd_sl_site_count', 0);
         if ($parent_license_id && !empty($activation_limit)) {
             add_post_meta($license_id, '_edd_sl_limit', $activation_limit);
         }
         // Get the purchase date so we can set the correct license expiration date
         $payment_meta = edd_get_payment_meta($payment_id);
         $purchase_date = null;
         if (!empty($payment_meta['date'])) {
             $purchase_date = strtotime($payment_meta['date']);
         }
         // Get license length
         $license_length = self::get_license_length($license_id, $payment_id, $d_id);
         if (empty($is_lifetime) && 'lifetime' !== $license_length) {
             // Set license expiration date
             $expiration = strtotime($license_length, $purchase_date);
             if ($expiration > strtotime('+24 hours')) {
                 // Force it to end of day if expiration is more than 24 hours in the future
                 $expiration = date('Y-n-d 23:59:59', $expiration);
                 // Convert back into timestamp
                 $expiration = strtotime($expiration);
             }
             self::set_license_expiration($license_id, $expiration);
         } else {
             self::set_license_as_lifetime($license_id);
         }
         do_action('edd_sl_store_license', $license_id, $d_id, $payment_id, $type);
     }
     return $keys;
 }
/**
 * Process web accept (one time) payment IPNs
 *
 * @since 1.3.4
 * @param array   $data IPN Data
 * @return void
 */
function edd_process_paypal_web_accept_and_cart($data, $payment_id)
{
    if ($data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded') {
        return;
    }
    if (empty($payment_id)) {
        return;
    }
    // Collect payment details
    $purchase_key = isset($data['invoice']) ? $data['invoice'] : $data['item_number'];
    $paypal_amount = $data['mc_gross'];
    $payment_status = strtolower($data['payment_status']);
    $currency_code = strtolower($data['mc_currency']);
    $business_email = isset($data['business']) && is_email($data['business']) ? trim($data['business']) : trim($data['receiver_email']);
    $payment_meta = edd_get_payment_meta($payment_id);
    if (edd_get_payment_gateway($payment_id) != 'paypal') {
        return;
        // this isn't a PayPal standard IPN
    }
    // Verify payment recipient
    if (strcasecmp($business_email, trim(edd_get_option('paypal_email', false))) != 0) {
        edd_record_gateway_error(__('IPN Error', 'edd'), sprintf(__('Invalid business email in IPN response. IPN data: %s', 'edd'), json_encode($data)), $payment_id);
        edd_update_payment_status($payment_id, 'failed');
        edd_insert_payment_note($payment_id, __('Payment failed due to invalid PayPal business email.', 'edd'));
        return;
    }
    // Verify payment currency
    if ($currency_code != strtolower($payment_meta['currency'])) {
        edd_record_gateway_error(__('IPN Error', 'edd'), sprintf(__('Invalid currency in IPN response. IPN data: %s', 'edd'), json_encode($data)), $payment_id);
        edd_update_payment_status($payment_id, 'failed');
        edd_insert_payment_note($payment_id, __('Payment failed due to invalid currency in PayPal IPN.', 'edd'));
        return;
    }
    if (!edd_get_payment_user_email($payment_id)) {
        // This runs when a Buy Now purchase was made. It bypasses checkout so no personal info is collected until PayPal
        // No email associated with purchase, so store from PayPal
        edd_update_payment_meta($payment_id, '_edd_payment_user_email', $data['payer_email']);
        // Setup and store the customers's details
        $address = array();
        $address['line1'] = !empty($data['address_street']) ? sanitize_text_field($data['address_street']) : false;
        $address['city'] = !empty($data['address_city']) ? sanitize_text_field($data['address_city']) : false;
        $address['state'] = !empty($data['address_state']) ? sanitize_text_field($data['address_state']) : false;
        $address['country'] = !empty($data['address_country_code']) ? sanitize_text_field($data['address_country_code']) : false;
        $address['zip'] = !empty($data['address_zip']) ? sanitize_text_field($data['address_zip']) : false;
        $user_info = array('id' => '-1', 'email' => sanitize_text_field($data['payer_email']), 'first_name' => sanitize_text_field($data['first_name']), 'last_name' => sanitize_text_field($data['last_name']), 'discount' => '', 'address' => $address);
        $payment_meta['user_info'] = $user_info;
        edd_update_payment_meta($payment_id, '_edd_payment_meta', $payment_meta);
    }
    if ($payment_status == 'refunded' || $payment_status == 'reversed') {
        // Process a refund
        edd_process_paypal_refund($data, $payment_id);
    } else {
        if (get_post_status($payment_id) == 'publish') {
            return;
            // Only complete payments once
        }
        // Retrieve the total purchase amount (before PayPal)
        $payment_amount = edd_get_payment_amount($payment_id);
        if (number_format((double) $paypal_amount, 2) < number_format((double) $payment_amount, 2)) {
            // The prices don't match
            edd_record_gateway_error(__('IPN Error', 'edd'), sprintf(__('Invalid payment amount in IPN response. IPN data: %s', 'edd'), json_encode($data)), $payment_id);
            edd_update_payment_status($payment_id, 'failed');
            edd_insert_payment_note($payment_id, __('Payment failed due to invalid amount in PayPal IPN.', 'edd'));
            return;
        }
        if ($purchase_key != edd_get_payment_key($payment_id)) {
            // Purchase keys don't match
            edd_record_gateway_error(__('IPN Error', 'edd'), sprintf(__('Invalid purchase key in IPN response. IPN data: %s', 'edd'), json_encode($data)), $payment_id);
            edd_update_payment_status($payment_id, 'failed');
            edd_insert_payment_note($payment_id, __('Payment failed due to invalid purchase key in PayPal IPN.', 'edd'));
            return;
        }
        if ('completed' == $payment_status || edd_is_test_mode()) {
            edd_insert_payment_note($payment_id, sprintf(__('PayPal Transaction ID: %s', 'edd'), $data['txn_id']));
            edd_set_payment_transaction_id($payment_id, $data['txn_id']);
            edd_update_payment_status($payment_id, 'publish');
        } else {
            if ('pending' == $payment_status && isset($data['pending_reason'])) {
                // Look for possible pending reasons, such as an echeck
                $note = '';
                switch (strtolower($data['pending_reason'])) {
                    case 'echeck':
                        $note = __('Payment made via eCheck and will clear automatically in 5-8 days', 'edd');
                        break;
                    case 'address':
                        $note = __('Payment requires a confirmed customer address and must be accepted manually through PayPal', 'edd');
                        break;
                    case 'intl':
                        $note = __('Payment must be accepted manually through PayPal due to international account regulations', 'edd');
                        break;
                    case 'multi-currency':
                        $note = __('Payment received in non-shop currency and must be accepted manually through PayPal', 'edd');
                        break;
                    case 'paymentreview':
                    case 'regulatory_review':
                        $note = __('Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'edd');
                        break;
                    case 'unilateral':
                        $note = __('Payment was sent to non-confirmed or non-registered email address.', 'edd');
                        break;
                    case 'upgrade':
                        $note = __('PayPal account must be upgraded before this payment can be accepted', 'edd');
                        break;
                    case 'verify':
                        $note = __('PayPal account is not verified. Verify account in order to accept this payment', 'edd');
                        break;
                    case 'other':
                        $note = __('Payment is pending for unknown reasons. Contact PayPal support for assistance', 'edd');
                        break;
                }
                if (!empty($note)) {
                    edd_insert_payment_note($payment_id, $note);
                }
            }
        }
    }
}
/**
 * Looks up purchases by email that match the registering user
 *
 * This is for users that purchased as a guest and then came
 * back and created an account.
 *
 * @access      public
 * @since       1.6
 * @param       $user_id INT - the new user's ID
 * @return      void
 */
function edd_add_past_purchases_to_new_user($user_id)
{
    $email = get_user_meta($user_id, 'user_email', true);
    $mode = edd_is_test_mode() ? 'test' : 'live';
    $payments = edd_get_payments(array('s' => $email, 'mode' => $mode));
    if ($payments) {
        foreach ($payments as $payment) {
            if (intval(edd_get_payment_user_id($payment->ID)) > 0) {
                continue;
            }
            // This payment already associated with an account
            $meta = edd_get_payment_meta($payment->ID);
            $meta['user_info'] = maybe_unserialize($meta['user_info']);
            $meta['user_info']['id'] = $user_id;
            $meta['user_info'] = serialize($meta['user_info']);
            // Store the updated user ID in the payment meta
            update_post_meta($payment->ID, '_edd_payment_meta', $meta);
        }
    }
}