Exemplo n.º 1
0
function mcs_payment_form()
{
    $ret = $form = '';
    if (isset($_GET['response_code'])) {
        $mcs = $_GET['response_code'];
        $provider = get_option('mcs_gateway') == 2 ? 'Authorize.net' : 'PayPal';
        switch ($mcs) {
            case 'thanks':
                $ret = "<p class='notice'>" . sprintf(__("Thank you for your purchase! You can view your purchase information at %s. You will receive an email with your payment key once your payment is finalized.", 'my-calendar-submissions'), $provider) . "</p>";
                break;
            case 'cancel':
                $ret = __("Sorry that you decided to cancel your purchase! Contact us if you have any questions!", 'my-calendar-submissions');
                break;
        }
    }
    if (mcs_payment_required()) {
        $price = mcs_get_price(is_user_logged_in());
        $currency = get_option('mcs_currency');
        $quantity = get_option('mcs_quantity');
        $discounts = mcs_check_discount();
        $discount_rate = (int) $discounts['rate'];
        $discount = $discount_rate != 0 ? true : false;
        if (isset($_GET['response_code'])) {
            $message = '';
        } else {
            $message = wpautop(jd_draw_template(array('price' => $price, 'currency' => $currency, 'discount' => $discount_rate, 'begins' => $discounts['begins'], 'ends' => $discounts['ends']), get_option('mcs_payment_message')));
        }
        $form = "<div class='mc-payments-form " . get_option('mcs_gateway') . "'>\n\t\t {$ret}\n\t\t {$message}";
        $nonce = wp_create_nonce('mcs-payments-nonce');
        if (get_option('mcs_gateway') == 'authorizenet') {
            if (get_option('mcs_quantity') != 'true' || (get_option('mcs_quantity') == 'true' && isset($_POST['mcs_quantity']) || isset($_GET['response_code']))) {
                require_once 'gateways/AuthorizeNet.php';
                // The SDK
                $url = mcs_replace_http(add_query_arg('mcsipn', 'true', get_permalink()));
                $rand = time() . rand(100000, 999999);
                $mcs_quantity = isset($_POST['mcs_quantity']) ? (int) $_POST['mcs_quantity'] : 1;
                $price = mcs_calculate_price($mcs_quantity, $price, $discount, $discount_rate);
                $form .= AuthorizeNetDPM::directPost($url, $price, $rand, $nonce);
            } else {
                $form .= mcs_set_quantity_form($price);
            }
        } else {
            $form .= mcs_paypal_form($price, $currency, $discount_rate, $discounts, $discount, $quantity);
        }
        $form .= "</div>";
    }
    return $form;
}
function mcs_processor($post)
{
    if (isset($post['mcs_submission'])) {
        $attach_id = false;
        $nonce = $post['event_nonce_name'];
        if (!wp_verify_nonce($nonce, 'event_nonce')) {
            return;
        }
        // honeypot - only bots should complete this field;
        $honeypot = isset($_POST['your_name']) && $_POST['your_name'] != '' ? true : false;
        if ($honeypot) {
            return;
        }
        // if files being uploaded, upload file and convert to a string for $post
        if (!empty($_FILES['event_image'])) {
            require_once ABSPATH . '/wp-admin/includes/file.php';
            require_once ABSPATH . '/wp-admin/includes/image.php';
            $file = $_FILES['event_image'];
            $upload = wp_handle_upload($file, array('test_form' => false));
            if (!isset($upload['error']) && isset($upload['file'])) {
                $filetype = wp_check_filetype(basename($upload['file']), null);
                $title = $file['name'];
                $ext = strrchr($title, '.');
                $title = $ext !== false ? substr($title, 0, -strlen($ext)) : $title;
                $attachment = array('post_mime_type' => $filetype['type'], 'post_title' => addslashes($title), 'post_content' => '', 'post_status' => 'inherit');
                $alt = isset($_POST['event_image_alt']) ? sanitize_text_field($_POST['event_image_alt']) : '';
                $attach_id = wp_insert_attachment($attachment, $upload['file']);
                update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
                $attach_data = wp_generate_attachment_metadata($attach_id, $upload['file']);
                wp_update_attachment_metadata($attach_id, $attach_data);
                $post['event_image'] = $upload['url'];
            }
        }
        // end file upload
        $check = mc_check_data('add', $post, 0);
        $message = '';
        if (mcs_payment_required()) {
            $key = isset($post['mcs_key']) ? $post['mcs_key'] : false;
            $quantity = mcs_check_key($key);
            if (!$quantity) {
                $reason = mcs_key_status($key);
                return array("<div class='notice error'><p>" . sprintf(__('That was not a valid payment key: %s', 'my-calendar-submissions'), $reason) . "</p></div>", $check[1], false);
            } else {
                $message = sprintf("<div class='notice error'><p>" . __('%d submissions remaining with this payment key.', 'my-calendar-submissions') . "</p></div>", $quantity - 1);
            }
        }
        if ($check[0]) {
            if (!isset($_POST['event_edit'])) {
                $response = my_calendar_save('add', $check);
                $action = 'add';
            } else {
                $response = my_calendar_save('edit', $check, (int) $_POST['event_edit']);
                $action = 'edit';
            }
            $event_id = $response['event_id'];
            $response = $response['message'];
            $event = mc_get_event_core($event_id);
            $post_id = $event->event_post;
            set_post_thumbnail($post_id, $attach_id);
            if ($message != '') {
                $response .= " {$message}";
            }
            $return = array($response, array(), true);
        } else {
            $return = array($check[3], $check[1], false);
            return $return;
        }
        if ($event_id) {
            $name = $post['mcs_name'];
            $email = $post['mcs_email'];
            if (mcs_payment_required()) {
                // Note: payments will be processed on both submissions & on edits.
                mcs_update_key_quantity($key, $quantity);
            }
            // if no errors and NOT SPAM send notifications.
            if (mc_event_is_spam($event_id)) {
                do_action('mcs_spam_submission', $name, $email, $event_id);
            } else {
                do_action('mcs_complete_submission', $name, $email, $event_id, $action);
            }
        }
        return $return;
    } else {
        return false;
    }
}