dq1230123 2015-11-05 01:43
浏览 46
已采纳

条带支付网关 - 可变金额

I have found lots of questions for this topic, but none seems to answer my question...

NB. Using Larry Ullman's Blog at http://www.larryullman.com/2012/11/28/creating-a-form-for-handling-payments-with-stripe/

Trying to use

  $amount = $_REQUEST['amount']; 

instead of

  $amount = 23400; //EXAMPLE ONLY

But the value of $_REQUEST['amount']; is being ignored?

<?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>
  • 写回答

1条回答 默认 最新

  • dpsfay2510 2015-12-13 20:36
    关注

    Thx Guys

    I emailed STRIPE and based on feedback, the issue was that I should have been using SESSIONS....

    PHP Manual Page on Session Handling

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程