0

I am submitting data to my custom table and would then like to redirect to a third party who will handle the payment. (not PayPal). I have the following code which adds the data to the database. The problem is I want to use the newly created ID as a reference as well as the amount that was posted as it has to be used in a MD5 calculation before the form can be redirected to the payment site. This is in my functions.php

function mcPaySubmit(){
global $wpdb;

$last = wp_get_recent_posts(array('numberposts' => '1', 'post_type' => 'post', 'post_status' => 'pending', 'author' => $user_id));
$purchase_post_id = $last['0']['ID'];
$purchase_days = $_POST['dext'];
$purchase_date = $_POST['mcTransactionDate'];
$purchase_amount = $_POST['totalDue'];

if($wpdb->insert('ac_purchases', array(
    'purchase_post_id' => $purchase_post_id,
    'purchase_days' => $purchase_days,
    'purchase_date' => $purchase_date,
    'purchase_amount' => $purchase_amount,
    'purchase_status' => 'pending'
    ))===FALSE) {

    echo "ERROR";
    } else {

    $response = str_replace('.','', $purchase_amount);

    echo $response;
    }
    wp_die();
}
add_action('wp_ajax_mcPaySubmit', 'mcPaySubmit');
add_action('wp_ajax_nopriv_mcPaySubmit', 'mcPaySubmit');

This is in my .js file

$ = jQuery.noConflict();

$(document).ready(function() {
$('#mcPayment').submit(submit_mcPayment);
});

function submit_mcPayment(){

$.ajax({
    url: myAjax.ajaxurl,
    type: "POST",
    data: $(this).serialize(),
    dataType: 'json',
    success: function(data){
        $("#feedback").html(data);
        }
    });
    return false;
}

This is my form

<script>
function payCalc(){
    var dext = document.getElementById("dext").value;
    var sumDext = document.getElementById("sumDext").value = Math.round(24.99 * dext * 100)/100;
    var totalDue = document.getElementById("totalDue").value = Math.round((sumDext + 79.99) * 100)/100;
}
</script>

<form method="POST" id="mcPayment" name="mcPayment">
<table>
    <tr>
        <td colspan="3"><b>Please note that the amounts are quoted in South African Rands (ZAR)</b></td>
    </tr>
    <tr>
        <td colspan="2"><label for="mcPublish"><b>Publish for 3 Days</b></label></td>
        <td><input type="text" id="mcPublish" name="mcPublish" readonly value="79.99" /></td>
    </tr>
    <tr>
        <td><label for="daysToExtend" /><b>Number of Days to Extend</b></label></td>
        <td><input type="number" id="dext" oninput="payCalc()" name="dext" min="0" value="1" /></td>
        <td><input type="text" id="sumDext" name="sumDext" readonly value="24.99" /></td>
    </tr>
    <tr>
        <td colspan="2"><label for="totalDue"><b>Total Due<b></label>
        <td><input type="text" id="totalDue" name="totalDue" readonly value="104.98" /></td>
    </tr>
    <tr>
        <td colspan="3"><input type="hidden" id="mcTransactionDate" name="mcTransactionDate" value="<?=$mcTransactionDate ?>"></td>
    </tr>
    <tr>
        <td><input type="hidden" name="action" value="mcPaySubmit" /></td>
        <td><input type="submit" id="mcPaySubmit" name="mcPaySubmit" value="Proceed to Pay" /></td>
        <td></td>
    </tr>
</table>
</form>
<div id="feedback"></div>

I can echo everything I want to the on the form, but I can figure out how to set it as a variable so that I can use it in the calculation. I have used the following link as a guide, Submitting post to database then redirecting to paypal

0

Good afternoon, I managed to sort this out. I have amended my functions.php to the following.

function mcPaySubmit(){
global $wpdb;

include('paygate.php');
$last = wp_get_recent_posts(array('numberposts' => '1', 'post_type' => 'post', 'post_status' => 'pending', 'author' => $user_id));
$purchase_post_id = $last['0']['ID'];
$purchase_days = $_POST['dext'];
$purchase_date = $_POST['mcTransactionDate'];
$purchase_amount = $_POST['totalDue'];

if($wpdb->insert('ac_purchases', array(
    'purchase_post_id' => $purchase_post_id,
    'purchase_days' => $purchase_days,
    'purchase_date' => $purchase_date,
    'purchase_amount' => $purchase_amount,
    'purchase_status' => 'pending'
    ))===FALSE) {

    echo "ERROR";
    } else {

    $reference = $wpdb -> insert_id;
    $amount = str_replace('.','',$purchase_amount);
    $checksum_source = $PAYGATE_ID."|".$reference."|".$amount."|".$CURRENCY."|".$RETURN_URL."|".$purchase_date."|".$EMAIL."|".$encryption_key;
    $CHECKSUM = md5($checksum_source);
    $response = array('AMOUNT' => $amount, 'TRANSACTION_DATE' => $purchase_date, 'REFERENCE' => $reference, 'CHECKSUM' => $CHECKSUM, 'PAYGATE_ID' => $PAYGATE_ID, 'CURRENCY' => $CURRENCY, 'RETURN_URL' => $RETURN_URL, 'EMAIL' => $EMAIL);
    echo json_encode($response);
    }
    wp_die();
}
add_action('wp_ajax_mcPaySubmit', 'mcPaySubmit');
add_action('wp_ajax_nopriv_mcPaySubmit', 'mcPaySubmit');

my form to this:

<script>
function payCalc(){
    var dext = document.getElementById("dext").value;
    var sumDext = document.getElementById("sumDext").value = Math.round(24.99 * dext * 100)/100;
    var totalDue = document.getElementById("totalDue").value = Math.round((sumDext + 79.99) * 100)/100;
}
</script>

<form method="POST" id="mcPayment" name="mcPayment" action="https://www.paygate.co.za/paywebv2/process.trans">
<table>
    <tr>
        <td colspan="3"><b>Please note that the amounts are quoted in South African Rands (ZAR)</b></td>
    </tr>
    <tr>
        <td colspan="2"><label for="mcPublish"><b>Publish for 3 Days</b></label></td>
        <td><input type="text" id="mcPublish" name="mcPublish" readonly value="79.99" /></td>
    </tr>
    <tr>
        <td><label for="daysToExtend" /><b>Number of Days to Extend</b></label></td>
        <td><input type="number" id="dext" oninput="payCalc()" name="dext" min="0" value="1" /></td>
        <td><input type="text" id="sumDext" name="sumDext" readonly value="24.99" /></td>
    </tr>
    <tr>
        <td colspan="2"><label for="totalDue"><b>Total Due<b></label>
        <td><input type="text" id="totalDue" name="totalDue" readonly value="104.98" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="mcTransactionDate" name="mcTransactionDate" value="<?=$TRANSACTION_DATE?>"></td>
        <td><input type="hidden" id="my-hidden-field" name="my-hidden-field" value="this is hidden" />
    </tr>
    <tr>
        <td><input type="hidden" name="action" value="mcPaySubmit" /></td>
        <td><input type="submit" id="mcPaySubmit" name="mcPaySubmit" value="Proceed to Pay" /></td>
        <td></td>
    </tr>
    <tr>
        <td><input type="hidden" id="PAYGATE_ID" name="PAYGATE_ID" value="<?=$PAYGATE_ID?>" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="REFERENCE" name="REFERENCE" value="<?=$REFERENCE?>" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="AMOUNT" name="AMOUNT" value="<?=$AMOUNT?>") /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="CURRENCY" name="CURRENCY" value="<?=$CURRENCY?>" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="RETURN_URL" name="RETURN_URL" value="<?=$RETURN_URL?>" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="TRANSACTION_DATE" name="TRANSACTION_DATE" value="<?=$TRANSACTION_DATE?>" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="EMAIL" name="EMAIL" value="<?=$EMAIL?>" /></td>
    </tr>
    <tr>
        <td><input type="hidden" id="CHECKSUM" name="CHECKSUM" value="<?=$CHECKSUM?>" /></td>
    </tr>
</table>
</form>

and the js to this.

$ = jQuery.noConflict();

$(document).ready(function() {
$('#mcPayment').submit(submit_mcPayment);
});

function submit_mcPayment(){

$.ajax({
    url: myAjax.ajaxurl,
    type: "POST",
    data: $(this).serialize(),
    dataType: 'json',
    success: function mcPaymentSuccess(response) {
            $('#AMOUNT').val(response.AMOUNT);
            $('#TRANSACTION_DATE').val(response.TRANSACTION_DATE);
            $('#REFERENCE').val(response.REFERENCE);
            $('#CHECKSUM').val(response.CHECKSUM);
            $('#PAYGATE_ID').val(response.PAYGATE_ID);
            $('#CURRENCY').val(response.CURRENCY);
            $('#RETURN_URL').val(response.RETURN_URL);
            $('#EMAIL').val(response.EMAIL);
            document.forms['mcPayment'].submit();
        }
    });
    return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.