I have a real bugger of a question. I am trying to build a donations page for a nonprofit which charges people through Stripe. Every charge I try to make gives me an error which reads :
Parsed Request Post Body
{
"card" : "TOKEN WAS HERE",
"description" : "Donation by ()",
"amount" : "0",
"currency" : "usd"
}
Response Body:
{
"error" :
{
"type" : "invalid_request_error",
"message" : "Invalid positive integer",
"param" : "amount"
}
}
I think there is an issue with the amount I am passing to Stripe (clearly Stripe thinks it is receiving an amount of 0
- I am just not sure if it ACTUALLY is) - but I absolutely CANNOT figure out what it is. Here is the full extent of the code on my page :
<?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>
You are sending amount 0 which stripe take as a non-positive number. Even mathematically 0 is not a positive integer neither is a negative integer. I just faced this issue and turns out that I was also sending 0 in amount. Sending amount greater than 0 works fine for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With