2014-10-31 46 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被定義,而不是其他索引。形式及其領域可能存在問題?