/** * Display column contents * * @since 1.0.0 * @param string $value The default value for the column * @param int $item_id The ID of the row item * @return string $value The updated value for the column */ function edd_wallet_column_data($value, $item_id) { $customer = new EDD_Customer($item_id); if ($customer->user_id < 1) { return ''; } $value = edd_wallet()->wallet->balance($customer->user_id); $value = edd_currency_filter(edd_format_amount((double) $value)); // Build the wallet link $value = '<a href="' . admin_url('edit.php?post_type=download&page=edd-customers&view=wallet&id=' . $item_id) . '" title="' . __('View user wallet', 'edd-wallet') . '">' . $value . '</a>'; return $value; }
/** * Withdraw funds from wallet * * @access public * @since 1.0.1 * @param mixed $user The user ID or email * @param float $amount The amount to withdraw * @param string $type The type of deposit * @param int $payment_id The ID of a given payment * @return mixed */ public function withdraw($user, $amount, $type = 'withdrawal', $payment_id = 0) { if (is_email($user) || strpos($user, '@') !== false) { $user = get_user_by('email', $user); $user = $user->ID; } $value = $this->balance($user); $value -= $amount; // Update the user wallet update_user_meta($user, '_edd_wallet_value', $value); // Record the deposit $args = array('user_id' => $user, 'payment_id' => $payment_id, 'type' => $type, 'amount' => $amount); $item = edd_wallet()->db->add($args); // Override customer value increase $customer = new EDD_Customer($user); $customer->decrease_value($amount); do_action('edd_wallet_withdraw', $args); return $item; }
/** * Display widget * * @access public * @since 1.1.0 * @param array $args Arguements for the widget * @param array $instance This widget instance * @return void */ public function widget($args, $instance) { // Bail if user isn't logged in if (!is_user_logged_in()) { return; } $args['id'] = isset($args['id']) ? $args['id'] : 'edd_wallet_widget'; $instance['title'] = isset($instance['title']) ? $instance['title'] : ''; $title = apply_filters('widget_title', $instance['title'], $instance, $args['id']); echo $args['before_widget']; if ($title) { echo $args['before_title'] . $title . $args['after_title']; } do_action('edd_before_wallet_widget'); $current_user = wp_get_current_user(); $value = edd_wallet()->wallet->balance($current_user->ID); $value = edd_currency_filter(edd_format_amount($value)); $value = '<span class="edd-wallet-value">' . $value . '</span>'; echo $value; do_action('edd_after_wallet_widget'); echo $args['after_widget']; }
/** * View the wallet of a customer * * @since 1.0.0 * @param object $customer The customer being displayed * @return void */ function edd_customer_wallet_view($customer) { settings_errors('edd-notices'); ?> <div id="wallet-header-wrapper" class="customer-section"> <?php echo get_avatar($customer->email, 30); ?> <span><?php echo sprintf(__('%s - Wallet', 'edd-wallet'), $customer->name); ?> </span> </div> <?php if ($customer->user_id < 1) { ?> <div class="error"><p><?php _e('This customer must be attached to a user account in order to edit their wallet', 'edd-wallet'); ?> </p></div> <?php } ?> <?php do_action('edd_customer_wallet_before_stats', $customer); ?> <?php if ($customer->user_id >= 1) { ?> <?php $value = edd_wallet()->wallet->balance($customer->user_id); ?> <div id="wallet-stats-wrapper" class="customer-section"> <ul> <li> <span class="dashicons dashicons-money"></span> <?php echo sprintf(__('%s Available', 'edd-wallet'), edd_currency_filter(edd_format_amount($value))); ?> </li> </ul> </div> <?php do_action('edd_customer_wallet_before_tables_wrapper', $customer); ?> <div id="wallet-tables-wrapper" class="customer-section"> <?php do_action('edd_customer_wallet_before_tables', $customer); ?> <h3><?php _e('Recent Activity', 'edd-wallet'); ?> </h3> <?php $activity = edd_wallet_get_activity($customer->user_id); ?> <table class="wp-list-table widefat striped activity"> <thead> <tr> <th><?php _e('ID', 'edd-wallet'); ?> </th> <th><?php _e('Type', 'edd-wallet'); ?> </th> <th><?php _e('Amount', 'edd-wallet'); ?> </th> <th><?php _e('Date', 'edd-wallet'); ?> </th> <th><?php _e('Status', 'edd-wallet'); ?> </th> <th><?php _e('Actions', 'edd-wallet'); ?> </th> </tr> </thead> <tbody> <?php if ($activity) { foreach ($activity as $item) { // Setup item type switch ($item->type) { case 'deposit': $type = __('Deposit', 'edd-wallet'); $item_id = $item->id . ' (' . $item->payment_id . ')'; $actions = '<a title="' . __('View Details for Payment', 'edd-wallet') . ' ' . $item->payment_id . '" href="' . admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $item->payment_id) . '">' . __('View Details', 'edd-wallet') . '</a>'; $status = edd_get_payment_status(get_post($item->payment_id), true); break; case 'withdrawal': $type = __('Withdraw', 'edd-wallet'); $item_id = $item->id . ' (' . $item->payment_id . ')'; $actions = '<a title="' . __('View Details for Payment', 'edd-wallet') . ' ' . $item->payment_id . '" href="' . admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $item->payment_id) . '">' . __('View Details', 'edd-wallet') . '</a>'; $status = edd_get_payment_status(get_post($item->payment_id), true); break; case 'admin-deposit': $type = __('Admin Deposit', 'edd-wallet'); $item_id = $item->id; $actions = ''; $status = __('Complete', 'edd-wallet'); break; case 'admin-withdraw': $type = __('Admin Withdraw', 'edd-wallet'); $item_id = $item->id; $actions = ''; $status = __('Complete', 'edd-wallet'); break; case 'refund': $type = __('Refund', 'edd-wallet'); $item_id = $item->id; $actions = ''; $status = __('Complete', 'edd-wallet'); break; default: $type = apply_filters('edd_wallet_activity_type', $item->type, $item); $item_id = apply_filters('edd_wallet_activity_item_id', $item->id, $item); $actions = apply_filters('edd_wallet_activity_actions', '', $item); $status = apply_filters('edd_wallet_activity_status', __('Complete', 'edd-wallet'), $item); break; } ?> <tr> <td><?php echo $item_id; ?> </td> <td><?php echo $type; ?> </td> <td><?php echo edd_currency_filter(edd_format_amount($item->amount)); ?> </td> <td><?php echo $item->date_created; ?> </td> <td><?php echo $status; ?> </td> <td> <?php echo $actions; ?> <?php do_action('edd_wallet_recent_history_actions', $customer, $item); ?> </td> </tr> <?php } } else { echo '<tr><td colspan="6">' . __('No Activity Found', 'edd-wallet') . '</td></tr>'; } ?> </tbody> </table> <div class="edd-wallet-edit-wallet"> <a class="button-secondary" href="<?php echo esc_url(add_query_arg(array('page' => 'edd-wallet-edit', 'id' => $customer->id, 'edd-message' => false), admin_url('options.php'))); ?> "><?php _e('Edit Wallet', 'edd-wallet'); ?> </a> </div> <?php } ?> <?php do_action('edd_customer_wallet_after_tables', $customer); ?> </div> <?php do_action('edd_customer_wallet_card_bottom', $customer); }
/** * Email template tag: value * The total value of the purchase * * @since 1.0.0 * @param int $payment_id * @return string value */ function edd_wallet_email_tag_value($payment_id) { if (get_post_type($payment_id) == 'edd_payment') { $value = edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment_id)), edd_get_payment_currency_code($payment_id)); } else { $item = edd_wallet()->db->get_customer_wallet_item($payment_id); $value = edd_currency_filter(edd_format_amount($item->amount)); } return html_entity_decode($value, ENT_COMPAT, 'UTF-8'); }
/** * Process refunds * * @access public * @since 1.0.0 * @param int $payment_id The ID of a payment * @param string $new_status The new status of the payment * @param string $old_status The old status of the payment * @return void */ public function process_refund($payment_id, $new_status, $old_status) { if ($old_status != 'publish' && $old_status != 'revoked') { return; } if ($new_status != 'refunded') { return; } if (edd_get_payment_gateway($payment_id) !== 'wallet') { return; } $user_id = edd_get_payment_user_id($payment_id); $refund_amount = edd_get_payment_amount($payment_id); // Deposit the funds edd_wallet()->wallet->deposit($user_id, $refund_amount, 'refund'); // Insert payment note edd_insert_payment_note($payment_id, __('Refund completed to Wallet.', 'edd-wallet')); }
/** * Build a list of wallet activity * * @since 1.0.0 * @param int $user_id The ID of the user to lookup * @return array $activity The wallet activity */ function edd_wallet_get_activity($user_id) { $activity = edd_wallet()->db->get_customer_wallet($user_id); return $activity; }
public static function create_payment($data) { if (wp_verify_nonce($data['edd_create_payment_nonce'], 'edd_create_payment_nonce')) { global $edd_options; $data['downloads'] = array_values($data['downloads']); if ($data['downloads'][0]['id'] == 0) { wp_die(sprintf(__('Please select at least one %s to add to the payment.', 'edd-manual-purchases'), edd_get_label_singular())); } $by_user_id = false; if (!empty($data['email'])) { $user = strip_tags(trim($data['email'])); $by_user_id = false; } elseif (empty($data['email']) && !empty($data['customer'])) { $user = strip_tags(trim($data['customer'])); } else { $user = null; } if (null == $user) { wp_die(__('Please select a customer or create a new one.', 'edd-manual-purchases')); } $payment = new EDD_Payment(); $customer = new EDD_Customer($user, $by_user_id); $user_id = $by_user_id == true ? $user : 0; $email = $by_user_id == false ? $user : ''; $first = isset($data['first']) ? sanitize_text_field($data['first']) : ''; $last = isset($data['last']) ? sanitize_text_field($data['last']) : ''; if (!$customer->id > 0) { $user = $by_user_id == false ? get_user_by('email', $user) : get_user_by('id', $user); if ($user) { $user_id = $user->ID; $email = $user->user_email; } $customer->create(array('email' => $email, 'name' => $first . ' ' . $last, 'user_id' => $user_id)); } else { $email = $customer->email; } $total = 0.0; $payment->customer_id = $customer->id; $payment->user_id = $user_id; $payment->first_name = $first; $payment->last_name = $last; $payment->email = $email; // Make sure the user info data is set $payment->user_info = array('first_name' => $first, 'last_name' => $last, 'id' => $user_id, 'email' => $email); $cart_details = array(); $total = 0; foreach ($data['downloads'] as $key => $download) { // calculate total purchase cost if (isset($download['price_id']) && empty($download['amount'])) { $prices = get_post_meta($download['id'], 'edd_variable_prices', true); $price_key = $download['options']['price_id']; $item_price = $prices[$download['price_id']]['amount']; } elseif (empty($download['amount'])) { $item_price = edd_get_download_price($download['id']); } $item_tax = $args = array('quantity' => !empty($download['quantity']) ? absint($download['quantity']) : 1, 'price_id' => isset($download['price_id']) ? $download['price_id'] : null, 'item_price' => !empty($download['amount']) ? edd_sanitize_amount($download['amount']) : $item_price); $args['tax'] = !empty($download['tax']) ? edd_sanitize_amount($download['tax'] * $args['quantity']) : 0; $payment->add_download($download['id'], $args); $total += $args['item_price'] * $args['quantity']; } if (!empty($data['amount'])) { $total = edd_sanitize_amount(strip_tags(trim($data['amount']))); $payment->total = $total; } // if we are using Wallet, ensure the customer can afford this purchase if (!empty($data['wallet']) && class_exists('EDD_Wallet') && $user_id > 0) { $wallet_value = edd_wallet()->wallet->balance($user_id); if ($wallet_value < $total) { wp_die(__('The customer does not have sufficient funds in their wallet to pay for this purchase.', 'edd-manual-purchases')); } } $date = !empty($data['date']) ? date('Y-m-d H:i:s', strtotime(strip_tags(trim($data['date'])))) : false; if (!$date) { $date = date('Y-m-d H:i:s', current_time('timestamp')); } if (strtotime($date, time()) > time()) { $date = date('Y-m-d H:i:s', current_time('timestamp')); } $payment->date = $date; $payment->status = 'pending'; $payment->currency = edd_get_currency(); $payment->gateway = sanitize_text_field($_POST['gateway']); $payment->mode = edd_is_test_mode() ? 'test' : 'live'; if (!empty($_POST['transaction_id'])) { $payment->transaction_id = sanitize_text_field($_POST['transaction_id']); } $payment->save(); if (!isset($data['receipt'])) { remove_action('edd_complete_purchase', 'edd_trigger_purchase_receipt', 999); } if (isset($_POST['status']) && 'pending' !== $_POST['status']) { $payment->status = $_POST['status']; $payment->save(); } if (!empty($data['wallet']) && class_exists('EDD_Wallet') && $user_id > 0) { // Update the user wallet edd_wallet()->wallet->withdraw($user_id, $total, 'withdrawal', $payment->ID); } if (!empty($data['shipped'])) { update_post_meta($payment->ID, '_edd_payment_shipping_status', '2'); } wp_redirect(admin_url('edit.php?post_type=download&page=edd-payment-history&edd-message=payment_created')); exit; } }
/** * Wallet value shortcode * * @since 1.0.0 * @param array $atts Shortcode attributes * @param string $content Shortcode content * @return string The wallet value */ function edd_wallet_value_shortcode($atts, $content = null) { $atts = shortcode_atts(array('wrapper' => true, 'wrapper_class' => 'edd-wallet-value'), $atts, 'edd_wallet_value'); // Bail if user isn't logged in if (!is_user_logged_in()) { return; } $current_user = wp_get_current_user(); $value = edd_wallet()->wallet->balance($current_user->ID); $value = edd_currency_filter(edd_format_amount($value)); if ($atts['wrapper']) { $value = '<span class="' . $atts['wrapper_class'] . '">' . $value . '</span>'; } return $value; }