Example #1
0
function qpp_coupon_codes($id)
{
    qpp_change_form_update();
    if (isset($_POST['Submit']) && check_admin_referer("save_qpp")) {
        $arr = array('couponnumber', 'couponget', 'duplicate', 'couponerror');
        foreach ($arr as $item) {
            $coupon[$item] = stripslashes($_POST[$item]);
            $coupon[$item] = filter_var($coupon[$item], FILTER_SANITIZE_STRING);
        }
        $options = array('code', 'coupontype', 'couponpercent', 'couponfixed');
        if ($coupon['couponnumber'] < 1) {
            $coupon['couponnumber'] = 1;
        }
        for ($i = 1; $i <= $coupon['couponnumber']; $i++) {
            foreach ($options as $item) {
                $coupon[$item . $i] = stripslashes($_POST[$item . $i]);
            }
            if (!$coupon['coupontype' . $i]) {
                $coupon['coupontype' . $i] = 'percent' . $i;
            }
            if (!$coupon['couponpercent' . $i]) {
                $coupon['couponpercent' . $i] = '10';
            }
            if (!$coupon['couponfixed' . $i]) {
                $coupon['couponfixed' . $i] = '5';
            }
        }
        update_option('qpp_coupon' . $id, $coupon);
        if ($coupon['duplicate']) {
            $qpp_setup = qpp_get_stored_setup();
            $arr = explode(",", $qpp_setup['alternative']);
            foreach ($arr as $item) {
                update_option('qpp_coupon' . $item, $coupon);
            }
        }
        qpp_admin_notice("The coupon settings have been updated.");
    }
    if (isset($_POST['Reset']) && check_admin_referer("save_qpp")) {
        delete_option('qpp_coupon' . $id);
        qpp_admin_notice("The coupon settings have been reset.");
    }
    $qpp_setup = qpp_get_stored_setup();
    $id = $qpp_setup['current'];
    $currency = qpp_get_stored_curr();
    $before = array('USD' => '&#x24;', 'CDN' => '&#x24;', 'EUR' => '&euro;', 'GBP' => '&pound;', 'JPY' => '&yen;', 'AUD' => '&#x24;', 'BRL' => 'R&#x24;', 'HKD' => '&#x24;', 'ILS' => '&#x20aa;', 'MXN' => '&#x24;', 'NZD' => '&#x24;', 'PHP' => '&#8369;', 'SGD' => '&#x24;', 'TWD' => 'NT&#x24;', 'TRY' => '&pound;');
    $after = array('CZK' => 'K&#269;', 'DKK' => 'Kr', 'HUF' => 'Ft', 'MYR' => 'RM', 'NOK' => 'kr', 'PLN' => 'z&#322', 'RUB' => '&#1056;&#1091;&#1073;', 'SEK' => 'kr', 'CHF' => 'CHF', 'THB' => '&#3647;');
    foreach ($before as $item => $key) {
        if ($item == $currency[$id]) {
            $b = $key;
        }
    }
    foreach ($after as $item => $key) {
        if ($item == $currency[$id]) {
            $a = $key;
        }
    }
    $coupon = qpp_get_stored_coupon($id);
    $content = '<div class="qpp-settings"><div class="qpp-options">';
    if ($id) {
        $content .= '<h2>Coupons codes for ' . $id . '</h2>';
    } else {
        $content .= '<h2>Default form coupons codes</h2>';
    }
    $content .= qpp_change_form($qpp_setup);
    $content .= '<form method="post" action="">
    <p<span<b>Note:</b> Leave fields blank if you don\'t want to use them</span></p>
    <p>Number of Coupons: <input type="text" name="couponnumber" value="' . $coupon['couponnumber'] . '" style="width:4em"></p>
    <table>
    <tr><td>Coupon Code</td><td>Percentage</td><td>Fixed Amount</td></tr>';
    for ($i = 1; $i <= $coupon['couponnumber']; $i++) {
        $percent = $coupon['coupontype' . $i] == 'percent' . $i ? 'checked' : '';
        $fixed = $coupon['coupontype' . $i] == 'fixed' . $i ? 'checked' : '';
        $content .= '<tr><td><input type="text" name="code' . $i . '" value="' . $coupon['code' . $i] . '" /></td>
        <td><input style="margin:0; padding:0; border:none;" type="radio" name="coupontype' . $i . '" value="percent' . $i . '" ' . $percent . ' /> <input type="text" style="width:4em;padding:2px" label="couponpercent' . $i . '" name="couponpercent' . $i . '" value="' . $coupon['couponpercent' . $i] . '" /> %</td>
        <td><input style="margin:0; padding:0; border:none;" type="radio" name="coupontype' . $i . '" value="fixed' . $i . '" ' . $fixed . ' />&nbsp;' . $b . '&nbsp;<input type="text" style="width:4em;padding:2px" label="couponfixed' . $i . '" name="couponfixed' . $i . '" value="' . $coupon['couponfixed' . $i] . '" /> ' . $a . '</td></tr>';
    }
    $content .= '</table>
    <h2>Invalid Coupon Code Message</h2>
    <input id="couponerror" type="text" name="couponerror" value="' . $coupon['couponerror'] . '" /></p>
    <h2>Coupon Code Autofill</h2>
    <p>You can add coupon codes to URLs which will autofill the field. The URL format is: mysite.com/mypaymentpage/?coupon=code. The code you set will appear on the form with the following caption:<br>
    <input id="couponget" type="text" name="couponget" value="' . $coupon['couponget'] . '" /></p>
    <h2>Clone Coupon Settings</h2>
    <p><input type="checkbox" style="margin:0; padding: 0; border: none" name="duplicate" ' . $coupon['duplicate'] . ' value="checked" /> Duplicate coupon codes across all forms</p>
    <p><input type="submit" name="Submit" class="button-primary" style="color: #FFF;" value="Save Changes" /> <input type="submit" name="Reset" class="button-primary" style="color: #FFF;" value="Reset" onclick="return window.confirm( \'Are you sure you want to reset the coupon codes?\' );"/></p>';
    $content .= wp_nonce_field("save_qpp");
    $content .= '</form>
    </div>
    <div class="qpp-options" style="float:right;">
    <h2>Coupon Check</h2>
    <p>Test your coupon codes.</p>';
    if ($id) {
        $form = ' form="' . $id . '"';
    }
    $args = array('form' => $id, 'id' => '', 'amount' => '');
    $content .= qpp_loop($args);
    $content .= '<p>There are some more examples of payment forms <a href="http://quick-plugins.com/quick-paypal-payments/paypal-examples/" target="_blank">on this page</a>.</p>
    <p>And there are loads of shortcode options <a href="http://quick-plugins.com/quick-paypal-payments/paypal-payments-shortcodes/" target="_blank">on this page</a>.</p>
    </div></div>';
    echo $content;
}
function qpp_process_form($values, $id)
{
    $currency = qpp_get_stored_curr();
    $qpp = qpp_get_stored_options($id);
    $send = qpp_get_stored_send($id);
    $auto = qpp_get_stored_autoresponder($id);
    $coupon = qpp_get_stored_coupon($id);
    $address = qpp_get_stored_address($id);
    $style = qpp_get_stored_style($id);
    $qpp_setup = qpp_get_stored_setup();
    $page_url = qpp_current_page_url();
    $paypalurl = 'https://www.paypal.com/cgi-bin/webscr';
    if ($send['customurl']) {
        $paypalurl = $send['customurl'];
    }
    if ($qpp_setup['sandbox']) {
        $paypalurl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    }
    if (empty($send['thanksurl'])) {
        $send['thanksurl'] = $page_url;
    }
    if (empty($send['cancelurl'])) {
        $send['cancelurl'] = $page_url;
    }
    if ($send['target'] == 'newpage') {
        $target = ' target="_blank" ';
    }
    $custom = $qpp['custom'] ? $qpp['custom'] : md5(mt_rand());
    $email = $send['email'] ? $send['email'] : $qpp_setup['email'];
    if ($values['combine'] && $values['combine'] != 'initial') {
        $arr = explode('&', $values['reference']);
        $values['reference'] = $arr[0];
        $values['amount'] = $arr[1];
    }
    $check = qpp_format_amount($currency[$id], $qpp, $values['amount']);
    $quantity = $values['quantity'] < 1 ? '1' : strip_tags($values['quantity']);
    if ($qpp['useprocess'] && $qpp['processtype'] == 'processpercent') {
        $percent = preg_replace('/[^.,0-9]/', '', $qpp['processpercent']) / 100;
        $handling = $check * $quantity * $percent;
        $handling = qpp_format_amount($currency[$id], $qpp, $handling);
    }
    if ($qpp['useprocess'] && $qpp['processtype'] == 'processfixed') {
        $handling = preg_replace('/[^.,0-9]/', '', $qpp['processfixed']);
        $handling = qpp_format_amount($currency[$id], $qpp, $handling);
    }
    if ($qpp['usepostage'] && $qpp['postagetype'] == 'postagepercent') {
        $percent = preg_replace('/[^.,0-9]/', '', $qpp['postagepercent']) / 100;
        $packing = $check * $quantity * $percent;
        $packing = qpp_format_amount($currency[$id], $qpp, $packing);
    }
    if ($qpp['usepostage'] && $qpp['postagetype'] == 'postagefixed') {
        $packing = preg_replace('/[^.,0-9]/', '', $qpp['postagefixed']);
        $packing = qpp_format_amount($currency[$id], $qpp, $packing);
    }
    $qpp_messages = get_option('qpp_messages' . $id);
    if (!is_array($qpp_messages)) {
        $qpp_messages = array();
    }
    $sentdate = date_i18n('d M Y');
    $amounttopay = $check * $quantity + $handling + $packing;
    if ($send['combine']) {
        $check = $check + $handling + $packing;
    }
    if ($qpp['stock'] == $values['stock'] && !$qpp['fixedstock']) {
        $values['stock'] = '';
    }
    $arr = array('email', 'firstname', 'lastname', 'address1', 'address2', 'city', 'state', 'zip', 'country', 'night_phone_b');
    foreach ($arr as $item) {
        if ($address[$item] == $values[$item]) {
            $values[$item] = '';
        }
    }
    $custom = md5(mt_rand());
    $qpp_messages[] = array('field0' => $sentdate, 'field1' => $values['reference'], 'field2' => $values['quantity'], 'field3' => $amounttopay, 'field4' => $values['stock'], 'field5' => $values['option1'], 'field6' => $values['couponblurb'], 'field8' => $values['email'], 'field9' => $values['firstname'], 'field10' => $values['lastname'], 'field11' => $values['address1'], 'field12' => $values['address2'], 'field13' => $values['city'], 'field14' => $values['state'], 'field15' => $values['zip'], 'field16' => $values['country'], 'field17' => $values['night_phone_b'], 'field18' => $custom);
    update_option('qpp_messages' . $id, $qpp_messages);
    if ($auto['enable'] && $values['email'] && $auto['whenconfirm'] == 'aftersubmission' || $send['confirmmessage']) {
        $amounttopay = qpp_total_amount($currency, $qpp, $values);
        qpp_send_confirmation($values, $id, $amounttopay);
    }
    $content = '<h2 id="qpp_reload">' . $send['waiting'] . '</h2>
    <script type="text/javascript" language="javascript">
    document.querySelector("#qpp_reload").scrollIntoView();
    </script>
    <form action="' . $paypalurl . '" method="post" name="frmCart" id="frmCart" ' . $target . '>
    <input type="hidden" name="item_name" value="' . strip_tags($values['reference']) . '"/>
    <input type="hidden" name="custom" value="' . $custom . '"/>
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="business" value="' . $email . '">
    <input type="hidden" name="return" value="' . $send['thanksurl'] . '">
    <input type="hidden" name="cancel_return" value="' . $send['cancelurl'] . '">
    <input type="hidden" name="currency_code" value="' . $currency[$id] . '">
    <input type="hidden" name="custom" value="' . $custom . '">
    ';
    if ($qpp['userecurring']) {
        $content .= '<input type="hidden" name="cmd" value="_xclick-subscriptions">';
    } elseif ($send['donate']) {
        $content .= '<input type="hidden" name="cmd" value="_donations">';
    } else {
        $content .= '<input type="hidden" name="cmd" value="_xclick">';
    }
    if ($qpp['use_stock']) {
        $content .= '<input type="hidden" name="item_number" value="' . strip_tags($values['stock']) . '">';
    }
    if ($qpp['userecurring']) {
        $content .= '<input type="hidden" name="a3" value="' . $check . '">
        <input type="hidden" name="p3" value="' . $qpp['recurringhowmany'] . '">
        <input type="hidden" name="t3" value="' . $qpp['recurring'] . '">
        <input type="hidden" name="src" value="1">
        <input type="hidden" name="srt" value="' . $qpp['srt'] . '">';
    } else {
        $content .= '<input type="hidden" name="quantity" value="' . $quantity . '">
        <input type="hidden" name="amount" value="' . $check . '">';
        if ($qpp['use_options']) {
            $content .= '<input type="hidden" name="on0" value="' . $qpp['optionlabel'] . '" />
            <input type="hidden" name="os0" value="' . $values['option1'] . '" />';
        }
        if (!$send['combine']) {
            if ($qpp['useprocess']) {
                $content .= '<input type="hidden" name="handling" value="' . $handling . '">';
            }
            if ($qpp['usepostage']) {
                $content .= '<input type="hidden" name="shipping" value="' . $packing . '">';
            }
        }
    }
    if ($send['use_lc']) {
        $content .= '<input type="hidden" name="lc" value="' . $send['lc'] . '">
        <input type="hidden" name="country" value="' . $send['lc'] . '">';
    }
    if ($qpp['useaddress']) {
        $arr = array('email', 'firstname', 'lastname', 'address1', 'address2', 'city', 'state', 'zip', 'country', 'night_phone_b');
        foreach ($arr as $item) {
            if ($address[$item] == $values[$item]) {
                $values[$item] = '';
                $content .= '<input type="hidden" name="' . $item . '" value="' . strip_tags($values[$item]) . '">';
            }
        }
    }
    $content .= '</form>
    <script language="JavaScript">document.getElementById("frmCart").submit();</script>';
    echo $content;
}