2014-10-31 50 views
1

我为管理员制作了一个优惠券代码系统来创建新的优惠券。在表格上,我需要计算折扣后要支付的最后一笔金额。我在添加运输费用和处理付款之前编写了未定义的索引错误 - 如何设置折扣后的最后金额?

if(!empty($discountCode)) { 
    $amount = ($unitCost - $unitCost * $couponDiscount/100); 
} 

。我不知道这是否是正确的......

我得到未定义的索引错误的$电子邮件 - $数量 - $ cardName - $ cardAddress1 - $ cardAddress2 - $ cardCity - $ cardState - $ cardZipcode - $ shippingMethod - $ product - $ token - $ couponDiscount,奇怪但不是$ unitCost,$ intRate或$ domRate

我该如何解决这个问题?

这是我的形式preorder.php

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // Stores errors: 
    $errors = array(); 

    // Need a payment token: 
    if (isset($_POST['stripeToken'])) { 

     $token = $_POST['stripeToken']; 

     // Check for a duplicate submission, just in case: 
     // Uses sessions 
     if (isset($_SESSION['token']) && ($_SESSION['token'] == $token)) { 
      $errors['token'] = 'You have apparently resubmitted the form. Please do not do that.'; 
     } else { // New submission. 
      $_SESSION['token'] = $token; 
     } 

    } else { 
     $errors['token'] = 'The order cannot be processed. Please make sure you have JavaScript enabled and try again.'; 
    } 

    $unitCost  = 6995; 
    $intRate  = 1500; 
    $domRate  = 500; 

    //print_r($_POST); 
    $email   = $_POST['email']; 
    $qty   = $_POST['qty']; 
    $cardName  = $_POST['card-name']; 
    $cardAddress1 = $_POST['address']; 
    $cardAddress2 = $_POST['address2']; 
    $cardCity  = $_POST['city']; 
    $cardState  = $_POST['state']; 
    $cardZipcode = $_POST['zipcode']; 
    $shippingMethod = $_POST['shipping-method']; 
    $product  = $_POST['productColor']; 
    $token   = $_POST['stripeToken']; 
    $couponDiscount = $_POST['couponDiscount']; 

    if(!empty($discountCode)) { 
     $amount = ($unitCost - $unitCost * $couponDiscount/100); 
    } 

    if($shippingMethod == 'International') : 
     $amount = $qty * ($intRate + $unitCost); 
     $description = ''.$qty.' - products(s) in '.$product.'(+International Shipping)'; 
    else: 
     $amount = $qty * ($domRate + $unitCost); 
     $description = ''.$qty.' - products(s) in '.$product.'(+Domestic Shipping)'; 
    endif; 

    // Charge the order: 
    $charge = Stripe_Charge::create(array(
     "amount"   => $amount, // amount in cents, again 
     "currency"  => "usd", 
     "description"  => $description, 
     "customer"  => $customer->id 
    )); 

    // Check that it was paid: 
    if ($charge->paid == true) { 
     $amountReadable = $amount/100; // to add in decimal points 
     echo '<div class="alert alert-success">Your card was successfully billed for $'.$amountReadable.'</div>'; 
     $status = "paid"; 
     $tracking_num = ""; 

表单提交里面preorder.js优惠券的验证,其工作状态良好,正确检查代码一起做:

// Watch for the document to be ready: 
$(document).ready(function() { 

    // Watch for a form submission: 
    $("#preorder").submit(function(event) { 

     // Flag variable: 
     var error = false; 

     // disable the submit button to prevent repeated clicks: 
     $('#submitBtn').attr("disabled", "disabled"); 

     // Check for errors: 
     if (!error) { 
      Stripe.card.createToken({ 
       number: $('.card-number').val(), 
       cvc: $('.card-cvc').val(), 
       exp_month: $('.card-expiry-month').val(), 
       exp_year: $('.card-expiry-year').val() 
      }, stripeResponseHandler); 

     } 

     // Prevent the form from submitting: 
     return false; 

    }); // Form submission 

    //Coupon code validation 
    $("#coupon_code").keyup(function(){ 
     var value = $(this).val(); 
     var data = { 
      code:value, 
      validateCouponCode:true 
     } 
     $.post("core.php",data,function(response){ 
      //Since the response will be json_encode'd JSON string we parse it here 
      var callback = JSON.parse(response); 
      if(callback.status){ 
       $("#couponStatus").html(" <span style='color:green'>Coupon is valid =) "+callback.discount_rate+"% discount</span> "); 
      }else{ 
       $("#couponStatus").html(" <span style='color:red'>Coupon is not valid</span> "); 
      } 
     }) 
    }) 
    //Coupon Code validation END 

}); // Document ready. 

// Function handles the Stripe response: 
function stripeResponseHandler(status, response) { 

    // Check for an error: 
    if (response.error) { 

     reportError(response.error.message); 

    } else { // No errors, submit the form: 

     var f = $("#preorder"); 

     // Token contains id, last4, and card type: 
     var token = response['id']; 

     // Insert the token into the form so it gets submitted to the server 
     f.append("<input type='hidden' name='stripeToken' value='" + token + "' />"); 

     // Submit the form: 
     f.get(0).submit(); 

    } 

} // End of stripeResponseHandler() function. 

这里是core.php:

//For ajax requests create an empty respond object 
$respond = new stdClass(); 
$respond->status = false; 
//END 

$conn = mysql_connect("localhost",DB_USER,DB_PASSWORD); 
mysql_select_db(DB_NAME); 

//Execute the query 
$foo = mysql_query("SELECT * FROM coupons WHERE expire > NOW() OR expire IS NULL OR expire = '0000-00-00 00:00:00'"); 
//Create an empty array 
$rows = array(); 
while ($a=mysql_fetch_assoc($foo)) { 
    //Assign the rows fetched from query to the array 
    $rows[] = $a; 
} 
//Turn the array into an array of objects 
$coupons = json_decode(json_encode($rows)); 

if(@$_POST["validateCouponCode"]){ 
    foreach ($coupons as $coupon) { 
     if($coupon->coupon_code == $_POST["code"]){ 
      //Coupon found 
       $respond->status = true; 
       //Additional instances to the respond object 
       $respond->discount_rate = $coupon->coupon_discount; 
     } 
    } 
    echo json_encode($respond); 
} 
+0

优惠金额有误吗? – alu 2014-10-31 00:40:16

+0

@alu我不知道,不应该,对吧?我无法测试它,因为我无法将其放入沙箱并使用条纹测试卡进行测试。他们都没有工作..所以我想以某种方式看到它,并在结账前显示给客户会很好。 – Ekin 2014-10-31 00:48:36

+0

我想使用var_dump($金​​额)或print_r($金额),但不打印任何东西,或者我做错了 – Ekin 2014-10-31 01:12:49

回答

1

经过几个小时的练习,我终于找到解决方案,它的工作。

感谢大家对他们的建议。 仍然欢迎任何改善代码的建议。

// Check for a form submission: 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 


// Stores errors: 
$errors = array(); 

// Need a payment token: 
if (isset($_POST['stripeToken'])) { 

    $token = $_POST['stripeToken']; 

    // Check for a duplicate submission, just in case: 
    // Uses sessions, you could use a cookie instead. 
    if (isset($_SESSION['token']) && ($_SESSION['token'] == $token)) { 
    $errors['token'] = 'You have apparently resubmitted the form. Please do not do that.'; 
    } else { // New submission. 
    $_SESSION['token'] = $token; 
    } 

} else { 
    $errors['token'] = 'The order cannot be processed. Please make sure you have JavaScript enabled and try again.'; 
} 

$unitCost  = 4995; 
$intRate  = 1500; 
$domRate  = 500; 
//print_r($_POST); 


$email   = $_POST['email']; 
$qty   = $_POST['qty']; 
$cardName  = $_POST['card-name']; 
$cardAddress1 = $_POST['address']; 
$cardAddress2 = $_POST['address2']; 
$cardCity  = $_POST['city']; 
$cardState  = $_POST['state']; 
$cardZipcode = $_POST['zipcode']; 
$shippingMethod = $_POST['shipping-method']; 
$product  = $_POST['kloqeColor']; 
$token   = $_POST['stripeToken']; 
$couponDiscount = ''; 

$sql = "SELECT * FROM `------`.`coupons` WHERE `coupon_code` = '" .addslashes($_POST['coupon-code']) . "'"; 

//echo $sql; 
$query = $connectAdmin->Query($sql); 


if($query->num_rows > 0) { 

$results = $query->fetch_array(MYSQLI_ASSOC); 

$couponDiscount = $results['coupon_discount']; 
} 

//echo '<pre>' . print_r($_POST, true) . '</pre>'; 
$amount = $unitCost; 

if(!empty($couponDiscount)) { 
//$amount = ($unitCost - $unitCost * $couponDiscount/100); 
//echo 'Discount not empty<br>'; 
$amount = $unitCost * ((100-$couponDiscount)/100); 
} 

//echo $amount . '<br>'; 
0

请问您可以添加您的表格的HTML?你没有列出stripeToken作为未定义的索引,这是你的条件。因此,可以定义变量$ unitCost,$ intRate或$ domRate,因为您正在分配数字。问题是,为什么stripeToken被定义,而不是其他索引。形式及其领域可能存在问题?