private function fee_selection() { // Cancel renewal? if (isset($_POST['cancel-renewal'])) { $this->listing->remove_category($this->category->id, true); if (!$this->listing->get_categories('all')) { $this->listing->delete(); } return wpbdp_render_msg(_x('Your renewal was successfully cancelled.', 'renewal', 'WPBDM')); } $fees = wpbdp_get_fees_for_category($this->category->id); if (isset($_POST['fees']) && isset($_POST['fees'][$this->category->id])) { $fee_id = intval($_POST['fees'][$this->category->id]); if ($fee = wpbdp_get_fee($fee_id)) { $payment = new WPBDP_Payment(array('listing_id' => $this->listing->get_id())); $payment->add_item('fee', $fee->amount, sprintf(_x('Fee "%s" renewal for category "%s"', 'listings', 'WPBDM'), $fee->label, wpbdp_get_term_name($this->category->id)), array('fee_id' => $fee_id, 'fee_days' => $fee->days, 'fee_images' => $fee->images), $this->category->id, $fee_id); $payment->save(); $this->category->payment_id = $payment->get_id(); return $this->checkout(); } } return wpbdp_render('renew-listing', array('listing' => $this->listing, 'category' => $this->category, 'fees' => $fees)); }
protected function step_save() { $listing = $this->state->editing ? WPBDP_Listing::get($this->state->listing_id) : WPBDP_Listing::create($this->state); $listing->set_field_values($this->state->fields); $listing->set_images($this->state->images); $listing->set_thumbnail_id($this->state->thumbnail_id); if (!$this->state->editing) { // Generate payment for the listing. $payment = new WPBDP_Payment(array('listing_id' => $listing->get_id())); if (!$this->state->editing) { $payment->tag('initial'); } foreach ($this->state->categories as $cat_id => $fee_id) { $category_info = $listing->get_category_info($cat_id); if (!$category_info) { $fee = wpbdp_get_fee($fee_id); if (!$fee) { continue; } $payment->add_item($this->state->autorenew_fees ? 'recurring_fee' : 'fee', $fee->amount, sprintf(_x('Fee "%s" for category "%s"%s', 'listings', 'WPBDM'), $fee->label, wpbdp_get_term_name($cat_id), $this->state->autorenew_fees ? ' ' . _x('(recurring)', 'listings', 'WPBDM') : ''), array('fee_id' => $fee_id, 'fee_days' => $fee->days, 'fee_images' => $fee->images), $cat_id, $fee_id); } } if ($this->state->upgrade_to_sticky) { $payment->add_item('upgrade', wpbdp_get_option('featured-price'), _x('Listing upgrade to featured', 'submit', 'WPBDM')); } $payment->set_submit_state_id($this->state->id); if (current_user_can('administrator')) { $payment->set_status(WPBDP_Payment::STATUS_COMPLETED); } $payment->save(); $this->state->listing_id = $listing->get_id(); $this->state->payment_id = $payment->get_id(); } do_action_ref_array('wpbdp_listing_form_extra_sections_save', array(&$this->state)); $listing->save(); $listing->set_post_status($this->state->editing ? wpbdp_get_option('edit-post-status') : wpbdp_get_option('new-post-status')); $this->state->advance(false); // This step is 'invisible'. return $this->dispatch(); }
public function add_category_fee_item($category_id, $fee) { if (is_int($fee)) { $fee = wpbdp_get_fee($fee); if (!$fee) { return false; } } $this->add_item('fee', $fee->amount, sprintf(_x('Fee "%s" for category "%s"', 'listings', 'WPBDM'), $fee->label, wpbdp_get_term_name($category_id)), array('fee_id' => $fee->id, 'fee_days' => $fee->days, 'fee_images' => $fee->images), $category_id, $fee->id); return true; }
if (isset($payment) && $payment) { ?> <form action="<?php echo esc_url($payment->get_checkout_url()); ?> " method="POST"> <input type="submit" value="<?php _ex('Proceed to Checkout', 'renewal', 'WPBDM'); ?> " /> </form> <?php } else { ?> <p><?php printf(_x('You are about to renew your listing "%s" publication inside category "%s".', 'templates', 'WPBDM'), esc_html($listing->get_title()), esc_html(wpbdp_get_term_name($category->id))); ?> </p> <p><?php _ex('Please select a fee option or click "Do not renew my listing" to cancel your renewal.', 'WPBDM'); ?> </p> <form id="wpbdp-renewlisting-form" method="POST" action=""> <?php echo wpbdp_render('parts/category-fee-selection', array('category' => get_term($category->id, WPBDP_CATEGORY_TAX), 'category_fees' => $fees, 'current_fee' => $category->fee_id, 'multiple_categories' => false), false); ?> <input type="submit" class="submit" name="submit" value="<?php _ex('Continue', 'templates', 'WPBDM'); ?> " />
public function upgrade_to_3_7_migrate_payments() { global $wpdb; $status_msg = ''; // Remove/update listing fees. if (!$wpdb->get_col($wpdb->prepare("SHOW COLUMNS FROM {$wpdb->prefix}wpbdp_listing_fees LIKE %s", 'migrated'))) { $wpdb->query("ALTER TABLE {$wpdb->prefix}wpbdp_listing_fees ADD migrated tinyint(1) DEFAULT 0"); } $n_fees = intval($wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wpbdp_listing_fees")); $n_fees_migrated = intval($wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}wpbdp_listing_fees WHERE migrated = %d", 1))); $fees_done = $n_fees_migrated == $n_fees ? true : false; if (!$fees_done) { $status_msg = sprintf(_x('Cleaning up listing fees information... %d/%d', 'installer', 'WPBDM'), $n_fees_migrated, $n_fees); $fees = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wpbdp_listing_fees WHERE migrated = %d ORDER BY id ASC LIMIT 50", 0), ARRAY_A); foreach ($fees as &$f) { // Delete fee if category does not exist. if (!term_exists(intval($f['category_id']), WPBDP_CATEGORY_TAX)) { $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wpbdp_listing_fees WHERE id = %d", $f['id'])); } else { // Delete duplicated listing fees. $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wpbdp_listing_fees WHERE id < %d AND category_id = %d AND listing_id = %d", $f['id'], $f['category_id'], $f['listing_id'])); $f['fee'] = (array) unserialize($f['fee']); $f['fee_days'] = abs(intval($f['fee']['days'])); $f['fee_images'] = abs(intval($f['fee']['images'])); $f['fee_id'] = intval($f['fee']['id']); $f['fee'] = ''; $f['migrated'] = 1; unset($f['fee']); if (!$f['expires_on']) { unset($f['expires_on']); } $wpdb->update($wpdb->prefix . 'wpbdp_listing_fees', $f, array('id' => $f['id'])); } } } // Migrate transactions. $transactions_done = false; if ($fees_done) { if (!$wpdb->get_col($wpdb->prepare("SHOW COLUMNS FROM {$wpdb->prefix}wpbdp_payments LIKE %s", 'migrated'))) { $wpdb->query("ALTER TABLE {$wpdb->prefix}wpbdp_payments ADD migrated tinyint(1) DEFAULT 0"); } $n_transactions = intval($wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wpbdp_payments")); $n_transactions_migrated = intval($wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}wpbdp_payments WHERE migrated = %d", 1))); $transactions_done = $n_transactions_migrated == $n_transactions ? true : false; if ($transactions_done) { $wpdb->query("ALTER TABLE {$wpdb->prefix}wpbdp_payments DROP payment_type"); $wpdb->query("ALTER TABLE {$wpdb->prefix}wpbdp_payments DROP migrated"); $wpdb->query("ALTER TABLE {$wpdb->prefix}wpbdp_listing_fees DROP fee"); $wpdb->query("ALTER TABLE {$wpdb->prefix}wpbdp_listing_fees DROP migrated"); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s", '_wpbdp[payment_status]')); } else { $status_msg = sprintf(_x('Migrating previous transactions to new Payments API... %d/%d', 'installer', 'WPBDM'), $n_transactions_migrated, $n_transactions); $transactions = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wpbdp_payments WHERE migrated = %d ORDER BY id ASC LIMIT 50", 0), ARRAY_A); foreach ($transactions as &$t) { $t['status'] = 'approved' == $t['status'] ? 'completed' : ('pending' == $t['status'] ? 'pending' : 'rejected'); $t['currency_code'] = get_option('wpbdp-currency'); $t['migrated'] = 1; if (!isset($t['processed_on']) || empty($t['processed_on'])) { unset($t['processed_on']); } if (!isset($t['created_on']) || empty($t['created_on'])) { unset($t['created_on']); } if (!isset($t['listing_id']) || empty($t['listing_id'])) { $t['listing_id'] = 0; } if (!isset($t['amount']) || empty($t['amount'])) { $t['amount'] = '0.0'; } // TODO: delete duplicated pending transactions (i.e. two renewals for the same category & listing ID that are 'pending'). switch ($t['payment_type']) { case 'initial': $wpdb->insert($wpdb->prefix . 'wpbdp_payments_items', array('payment_id' => $t['id'], 'amount' => $t['amount'], 'item_type' => 'charge', 'description' => _x('Initial listing payment (BD < 3.4)', 'installer', 'WPBDM'))); $wpdb->update($wpdb->prefix . 'wpbdp_payments', $t, array('id' => $t['id'])); break; case 'edit': $wpdb->insert($wpdb->prefix . 'wpbdp_payments_items', array('payment_id' => $t['id'], 'amount' => $t['amount'], 'item_type' => 'charge', 'description' => _x('Listing edit payment (BD < 3.4)', 'installer', 'WPBDM'))); $wpdb->update($wpdb->prefix . 'wpbdp_payments', $t, array('id' => $t['id'])); break; case 'renewal': $data = unserialize($t['extra_data']); $fee_info = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wpbdp_listing_fees WHERE id = %d", $data['renewal_id'])); if (!$fee_info || !term_exists(intval($fee_info->category_id), WPBDP_CATEGORY_TAX)) { $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wpbdp_payments WHERE id = %d", $t['id'])); continue; } $fee_info->fee = unserialize($fee_info->fee); $item = array(); $item['payment_id'] = $t['id']; $item['amount'] = $t['amount']; $item['item_type'] = 'fee'; $item['description'] = sprintf(_x('Renewal fee "%s" for category "%s"', 'installer', 'WPBDM'), $fee_info->fee['label'], wpbdp_get_term_name($fee_info->category_id)); $item['data'] = serialize(array('fee' => $fee_info->fee)); $item['rel_id_1'] = $fee_info->category_id; $item['rel_id_2'] = $fee_info->fee['id']; $wpdb->insert($wpdb->prefix . 'wpbdp_payments_items', $item); $wpdb->update($wpdb->prefix . 'wpbdp_payments', $t, array('id' => $t['id'])); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wpbdp_listing_fees WHERE id = %d", $data['renewal_id'])); break; case 'upgrade-to-sticky': $wpdb->insert($wpdb->prefix . 'wpbdp_payments_items', array('payment_id' => $t['id'], 'amount' => $t['amount'], 'item_type' => 'upgrade', 'description' => _x('Listing upgrade to featured', 'installer', 'WPBDM'))); $wpdb->update($wpdb->prefix . 'wpbdp_payments', $t, array('id' => $t['id'])); break; default: $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}wpbdp_payments WHERE id = %d", $t['id'])); break; } } } } $res = array('ok' => true, 'done' => $transactions_done, 'status' => $status_msg); return $res; }