2015-11-05 380 views
0

我發現了很多關於此主題的問題,但似乎沒有人回答我的問題...條紋支付網關 - 可變金額

注意:使用拉里·厄爾曼的博客:http://www.larryullman.com/2012/11/28/creating-a-form-for-handling-payments-with-stripe/

嘗試使用

$amount = $_REQUEST['amount']; 

代替

$amount = 23400; //EXAMPLE ONLY 

但是$ _REQUEST值[ '量'];被忽略?

<?php 

// 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']; 
    $StripeAmount = $_REQUEST['amount']; // $20, in cents 


    // 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.'; 
} 

// Set the order amount somehow: 
// $20, in cents 

$amount = $_REQUEST['amount']; // in cents 

// Validate other form data! 

// If no errors, process the order: 
if (empty($errors)) { 

    // create the charge on Stripe's servers - this will charge the user's card 
    try { 

     // Include the Stripe library: 
     // Assumes you've installed the Stripe PHP library using Composer! 
     require_once('stripe/init.php'); 

     // set your secret key: remember to change this to your live secret key in production 
     // see your keys here https://manage.stripe.com/account 
     \Stripe\Stripe::setApiKey(STRIPE_PRIVATE_KEY); 

     // Charge the order: 
     $charge = \Stripe\Charge::create(array(
      "amount" => $amount, // amount in cents, again 
      "currency" => "aud", 
      "source" => $token, 
      "description" => $email 
      ) 
     ); 

     // Check that it was paid: 
     if ($charge->paid == true) { 
      $OrderID = $_GET['order_id']; //reference from shop OrderId 
      $txn_id = $_GET['id']; //transaction number form payment Gateway 

      $objBooking->updateVisitorOrderRecord($OrderID, $txn_id); 
      header('location:visitor_payment_thanks.php'); 

     } else { // Charge was not paid! 
      header('location:visitor_payment_error.php'); 
     } 

    } catch (\Stripe\Error\Card $e) { 
     // Card was declined. 
     $e_json = $e->getJsonBody(); 
     $err = $e_json['error']; 
     $errors['stripe'] = $err['message']; 
    } catch (\Stripe\Error\ApiConnection $e) { 
     // Network problem, perhaps try again. 
    } catch (\Stripe\Error\InvalidRequest $e) { 
     // You screwed up in your programming. Shouldn't happen! 
    } catch (\Stripe\Error\Api $e) { 
     // Stripe's servers are down! 
    } catch (\Stripe\Error\Base $e) { 
     // Something else that's not the customer's fault. 
    } 

} // A user form submission error occurred, handled below. 

} // Form submission. 

// Set the Stripe key: 
// Uses STRIPE_PUBLIC_KEY from the config file. 
echo '<script type="text/javascript">Stripe.setPublishableKey("' . STRIPE_PUBLIC_KEY . '");</script>'; 
?> 

<h1>Buy This Thing</h1> 

<form action="buy.php" method="POST" id="payment-form"> 

    <?php // Show PHP errors, if they exist: 
    if (isset($errors) && !empty($errors) && is_array($errors)) { 
     echo '<div class="alert alert-error"><h4>Error!</h4>The following error(s) occurred:<ul>'; 
     foreach ($errors as $e) { 
      echo "<li>$e</li>"; 
     } 
     echo '</ul></div>'; 
    }?> 

    <div id="payment-errors"></div> 

    <span class="help-block">You can pay using: Mastercard, Visa, American Express, JCB, Discover, and Diners Club.</span> 

    <div class="alert alert-info"><h4>JavaScript Required!</h4>For security purposes, JavaScript is required in order to complete an order.</div> 

    <label>Card Number</label> 
    <input type="text" size="20" autocomplete="off" class="card-number input-medium"> 
    <span class="help-block">Enter the number without spaces or hyphens.</span> 
    <label>CVC</label> 
    <input type="text" size="4" autocomplete="off" class="card-cvc input-mini"> 
    <label>Expiration (MM/YYYY)</label> 
    <input type="text" size="2" class="card-expiry-month input-mini"> 
    <span>/</span> 
    <input type="text" size="4" class="card-expiry-year input-mini"> 

    <button type="submit" class="btn" id="submitBtn">Submit Payment</button> 

</form> 

<script src="buy.js"></script> 
+0

你有雙 - 檢查'$ amount'的確切值? –

+0

嗨Nathan Yep ..如果我輸入$ amount = $ _REQUEST ['amount'];高於上面的代碼 - 顯示所需的數量。 – Gazza

+0

那麼'$ StripeAmount'只能設置,不能使用。可能還有其他類似的故障。 –

回答

0

THX專家

我通過電子郵件條紋,並根據反饋意見,問題是,我本來應該使用會話....

PHP手冊上Session Handling