编程语言
首页 > 编程语言> > PHP-Stripe 400错误“无效的正整数”

PHP-Stripe 400错误“无效的正整数”

作者:互联网

我有个问题.我正在尝试为非营利组织建立一个捐赠页面,该页面通过Stripe向人们收费.我每次尝试收费都会给我一个错误,内容为:

解析的请求发布正文

{
    "card"        : "TOKEN WAS HERE",
    "description" : "Donation by   ()",
    "amount"      : "0",
    "currency"    : "usd"
}

响应主体:

{
    "error" :
    {
        "type"    : "invalid_request_error",
        "message" : "Invalid positive integer",
        "param"   : "amount"
    }
}

我认为我传递给Stripe的金额有问题(很明显,Stripe认为它收到的金额为0-我只是不确定它是否确实是)-但我绝对无法确定它是什么.这是我页面上代码的全部范围:

<?php
    require( 'Stripe/init.php' );

    // Load configuration settings
    $config = require( 'config.php' );

    // Force https
    if ( $config[ 'test-mode' ] &&
         $_SERVER[ 'HTTPS' ] != 'on' )
    {
        header( 'HTTP/1.1 301 Moved Permanently' );
        header( 'Location: https://' .
                    $_SERVER[ "SERVER_NAME" ] .
                    $_SERVER[ "REQUEST_URI" ] );
        exit;
    }

    if ( $_POST )
    {
        \Stripe\Stripe::setApiKey( $config[ 'secret-key' ] );

        // POSTed Variables
        $token      = $_POST[ 'stripeToken' ];
        $first_name = $_POST[ 'first-name' ];
        $last_name  = $_POST[ 'last-name' ];
        $name       = $first_name .
                          ' ' .
                          $last_name;
        $address    = $_POST[ 'address' ] .
                          "\n" .
                          $_POST[ 'city' ] .
                          ', ' .
                          $_POST[ 'state' ] .
                          ' ' .
                          $_POST[ 'zip' ];
        $email      = $_POST[ 'email' ];
        $phone      = $_POST[ 'phone' ];
        $amount     = (float) $_POST[ 'amount' ];

        try
        {
            if ( ! isset( $_POST[ 'stripeToken' ] ) )
            {
                throw new Exception( "The Stripe Token was not generated correctly" );
            }

            // Charge the card
            $donation = \Stripe\Charge::create( array( 'card'        => $token,
                                                       'description' => 'Donation by ' .
                                                                            $name .
                                                                            ' (' .
                                                                            $email .
                                                                            ')',
                                                                        'amount' => $amount * 100,
                                                                        'currency' => 'usd' ) );

            // Build and send the email
            $headers = 'From: ' .
                           $config[ 'email-from' ];

            $headers .= "\r\nBcc: " .
                            $config[ 'email-bcc' ] .
                            "\r\n\r\n";

            // Find and replace values
            $find    = array( '%name%',
                              '%amount%' );

            $replace = array( $name,
                              '$' .
                                  $amount );

            $message = str_replace( $find,
                                    $replace,
                                    $config[ 'email-message' ] ) .
                                        "\n\n";
            $message .= 'Amount: $' .
                            $amount .
                            "\n";

            $message .= 'Address: ' .
                            $address .
                            "\n";

            $message .= 'Phone: ' .
                            $phone .
                            "\n";

            $message .= 'Email: ' .
                            $email .
                            "\n";

            $message .= 'Date: ' .
                            date( 'M j, Y, g:ia',
                                  $donation[ 'created' ] ) .
                                  "\n";

            $message .= 'Transaction ID: ' .
                            $donation['id'] .
                            "\n\n\n";

            $subject = $config[ 'email-subject' ];

            // Send it
            if ( !$config[ 'test-mode' ] )
            {
                mail( $email,
                      $subject,
                      $message,
                      $headers );
            }

            // Forward to "Thank You" page
            header( 'Location: ' .
                    $config[ 'thank-you' ] );
            exit;
        }
        catch ( Exception $e )
        {
            $error = $e->getMessage();
        }
    }
?>

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8">

        <title>Title</title>

        <link rel="stylesheet" type="text/css" href="style.css" media="all">
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
        <link rel="icon" href="/favicon.ico" type="image/x-icon">

        <script src  = "https://js.stripe.com/v2"
                type = "text/javascript">
        </script>

        <script src  = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
                type = "text/javascript">
        </script>

        <script type = "text/javascript">
            Stripe.setPublishableKey( '<?php echo $config[ 'publishable-key' ] ?>' );
        </script>

        <script src  = "script.js"
                type = "text/javascript" >
        </script>
    </head>

    <body>
        <div class = "wrapper">
            <div class = "messages">
                <!-- Error messages go here go here -->
            </div>

            <form action = "#"
                  class  = "donation-form"
                  id     = "donor"
                  method = "POST">
                <fieldset>
                    <legend>
                        Contact Information
                    </legend>

                    <div class = "form-row form-first-name">
                        <label>
                            First Name
                        </label>

                        <input class = "first-name text"
                               name  = "first-name"
                               type  = "text">
                    </div>

                    <div class = "form-row form-last-name">
                        <label>
                            Last Name
                        </label>

                        <input class = "last-name text"
                               name  = "last-name"
                               type  = "text">
                    </div>

                    <div class = "form-row form-email">
                        <label>
                            Email
                        </label>

                        <input class = "email text"
                               name  = "email"
                               type  = "text">
                    </div>

                    <div class = "form-row form-phone">
                        <label>
                            Phone
                        </label>

                        <input class = "phone text"
                               name  = "phone"
                               type  = "text">
                    </div>

                    <div class = "form-row form-address">
                        <label>
                            Address
                        </label>

                        <textarea class = "address text"
                                  name  = "address"
                                  cols  = "30"
                                  rows  = "2">
                        </textarea>
                    </div>

                    <div class = "form-row form-city">
                        <label>
                            City
                        </label>

                        <input class = "city text"
                               name  = "city"
                               type  = "text">
                    </div>

                    <div class = "form-row form-state">
                        <label>
                            State
                        </label>

                        <select name = "state" class="state text">
                            <option value = "AL">
                                AL
                            </option>

                            <option value = "AK">
                                AK
                            </option>

                            <option value = "AZ">
                                AZ
                            </option>

                            <option value = "AR">
                                AR
                            </option>

                            <option value = "CA">
                                CA
                            </option>

                            <option value = "CO">
                                CO
                            </option>

                            <option value = "CT">
                                CT
                            </option>

                            <option value = "DE">
                                DE
                            </option>

                            <option value = "DC">
                                DC
                            </option>

                            <option value = "FL">
                                FL
                            </option>

                            <option value = "GA">
                                GA
                            </option>

                            <option value = "HI">
                                HI
                            </option>

                            <option value = "ID">
                                ID
                            </option>

                            <option value = "IL">
                                IL
                            </option>

                            <option value = "IN">
                                IN
                            </option>

                            <option value = "IA">
                                IA
                            </option>

                            <option value = "KS">
                                KS
                            </option>

                            <option value = "KY">
                                KY
                            </option>

                            <option value = "LA">
                                LA
                            </option>

                            <option value = "ME">
                                ME
                            </option>

                            <option value = "MD">
                                MD
                            </option>

                            <option value = "MA">
                                MA
                            </option>

                            <option value = "MI">
                                MI
                            </option>

                            <option value = "MN">
                                MN
                            </option>

                            <option value = "MS">
                                MS
                            </option>

                            <option value = "MO">
                                MO
                            </option>

                            <option value = "MT">
                                MT
                            </option>

                            <option value = "NE">
                                NE
                            </option>

                            <option value = "NV">
                                NV
                            </option>

                            <option value = "NH">
                                NH
                            </option>

                            <option value = "NJ">
                                NJ
                            </option>

                            <option value = "NM">
                                NM
                            </option>

                            <option value = "NY">
                                NY
                            </option>

                            <option value = "NC">
                                NC
                            </option>

                            <option value = "ND">
                                ND
                            </option>

                            <option value = "OH">
                                OH
                            </option>

                            <option value = "OK">
                                OK
                            </option>

                            <option value = "OR">
                                OR
                            </option>

                            <option value = "PA">
                                PA
                            </option>

                            <option value = "RI">
                                RI
                            </option>

                            <option value = "SC">
                                SC
                            </option>

                            <option value = "SD">
                                SD
                            </option>

                            <option value = "TN">
                                TN
                            </option>

                            <option value = "TX">
                                TX
                            </option>

                            <option value = "UT">
                                UT
                            </option>

                            <option value = "VT">
                                VT
                            </option>

                            <option value = "VA">
                                VA
                            </option>

                            <option value = "WA">
                                WA
                            </option>

                            <option value = "WV">
                                WV
                            </option>

                            <option value = "WI">
                                WI
                            </option>

                            <option value = "WY">
                                WY
                            </option>
                        </select>
                    </div>

                    <div class = "form-row form-zip">
                        <label>
                            Zip
                        </label>

                        <input class = "zip text"
                               name  = "zip"
                               type  = "text">
                    </div>
                </fieldset>

                <fieldset>
                    <legend>
                        Your Generous Donation
                    </legend>

                    <div class = "form-row form-amount">
                        <label>
                            <input class = "set-amount"
                                   name  = "amount"
                                   type  = "radio"
                                   value = "10">
                                $10
                        </label>

                        <label>
                            <input class = "set-amount"
                                   name  = "amount"
                                   type  = "radio"
                                   value = "27">
                                $27
                        </label>

                        <label>
                            <input class = "set-amount"
                                   name  = "amount"
                                   type  = "radio"
                                   value = "50">
                                $50
                        </label>

                        <label>
                            <input class = "set-amount"
                                   name  = "amount"
                                   type  = "radio"
                                   value = "100">
                                $100
                        </label>

                        <label>
                            <input class = "other-amount"
                                   name  = "amount"
                                   type  = "radio"
                                   value = "0">
                                Other:
                        </label>

                        <input type="text" class="amount text" />
                    </div>

                    <div class="form-row form-number">
                        <label>
                            Card Number
                        </label>

                        <input class        = "card-number text"
                               autocomplete = "off"
                               type         = "text"
                               value        = "">
                    </div>

                    <div class="form-row form-cvc">
                        <label>
                            CVC
                        </label>

                        <input class        = "card-cvc text"
                               autocomplete = "off"
                               type         = "text"
                               value        = "">
                    </div>

                    <div class = "form-row form-expiry">
                        <label>
                            Expiration Date
                        </label>

                        <select class="card-expiry-month text">
                            <option selected
                                    value = "01">
                                January
                            </option>

                            <option value = "02">
                                February
                            </option>

                            <option value = "03">
                                March
                            </option>

                            <option value = "04">
                                April
                            </option>

                            <option value = "05">
                                May
                            </option>

                            <option value = "06">
                                June
                            </option>

                            <option value = "07">
                                July
                            </option>

                            <option value = "08">
                                August
                            </option>

                            <option value = "09">
                                September
                            </option>

                            <option value = "10">
                                October
                            </option>

                            <option value = "11">
                                November
                            </option>

                            <option value = "12">
                                December
                            </option>
                        </select>

                        <select class = "card-expiry-year text">
                            <option selected
                                    value = "2017">
                                2017
                            </option>

                            <option value = "2018">
                                2018
                            </option>

                            <option value = "2019">
                                2019
                            </option>

                            <option value = "2020">
                                2020
                            </option>

                            <option value = "2021">
                                2021
                            </option>

                            <option value = "2022">
                                2022
                            </option>

                            <option value = "2023">
                                2023
                            </option>

                            <option value = "2024">
                                2024
                            </option>

                            <option value = "2025">
                                2025
                            </option>

                            <option value = "2026">
                                2026
                            </option>
                        </select>
                    </div>

                    <div class = "form-row form-submit">
                        <input class = "submit-button"
                               type  = "submit"
                               value = "Submit Donation">
                    </div>
                </fieldset>
            </form>
        </div>

        <script>
            if ( window.Stripe ) $('.donation-form').show()
        </script>

        <noscript>
            <p>
                JavaScript is required for the donation form.
            </p>
        </noscript>
    </body>

    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">
    </script>

    <script src = 'google-sheet.js'>
    </script>
</html>

解决方法:

我不知道php,所以在这里可能还很遥远,但是好像您是在将金额声明为浮点数.乘以100不应更改此值,因此您可能需要在发送之前将其实际转换为整数.

(我猜您是在发送123.0000 ….而不是123)

标签:stripe-payments,php
来源: https://codeday.me/bug/20191111/2020324.html