将我的表单提交到数据库,然后重定向到支付站点

时间:2016-01-12 作者:anotherchance

我正在向我的自定义表提交数据,然后希望重定向到处理付款的第三方。(不是贝宝)。我有以下代码将数据添加到数据库中。问题是,我想使用新创建的ID作为参考,以及过帐的金额,因为在将表单重定向到付款网站之前,必须在MD5计算中使用该ID。这是我的职责。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\');
这是我的。js文件

$ = 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;
}
这是我的表格

<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>
我可以在表单上回显我想要的所有内容,但我可以找出如何将其设置为变量,以便在计算中使用它。我使用了以下链接作为指导,Submitting post to database then redirecting to paypal

1 个回复
SO网友:anotherchance

下午好,我设法解决了这个问题。我已经修改了我的职能。php访问以下内容。

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\');
我的表格:

<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>
还有这个的js。

$ = 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;
}