Using a variable for amount in Stripe Payment Gateway - php

I am trying to integrate Stripe payment onto my site. I have a php variable '$val' set with a price amount sent from a form
$val = $_POST['myFieldName'];
I'm trying to use this variable to charge the amount it holds with stripe
Stripe_Charge::create(array(
"amount" => $val ,
"currency" => "gbp",
"card" => $_POST['stripeToken']));
But it seems Stripe wont except this variable. it puts a 'Missing required param: amount' error on the page It only wants to accept a actuale integer such as '1234' I tried cast to int using => intval($var) nut with no joy. Could someone tell me where Im going wrong
Thnks

you should try this
Stripe_Charge::create(array(
"amount" => 400,
"currency" => "usd",
"card" => "tok_14igbg2eZvKYlo2Cm0Akcrhg", // obtained with Stripe.js
"description" => "Charge for test#example.com"
));
description field is missing as per the documentation

I face same issue but its my coding fault
when you land on payment page
in the end just add this line before form submit button
" name="myFieldName">

Related

stripe payment get payment description

Hello got this code how can i return stripe payment description to a php variable so that i can later on can use it in sql query.
\Stripe\Stripe::setApiKey("STRIPE API");
try {
//Charge the Card
$newcard = \Stripe\Charge::create(array(
"customer" => $userstrip,
"amount" => 400,
"currency" => "USD",
'capture' => false),
);
$newcards = $newcard->description; // Tried like this
Okay if you just use id instead of description it also gets the payment id! :)

How to get the charge ID from Stripe using PHP?

First of all, I am aware that this question has been asked but they didn't explain how to get the information using
charge.succeeded
I am creating an auth and capture charge using Stripe. I need the charge ID to confirm/capture the charge.
The following code creates the charge but doesn't charge until it's been approved.
\Stripe\Charge::create(array(
"amount" => $price,
"currency" => "usd",
"customer" => $custID, // obtained with Stripe.js
"description" => "Charge for test#example.com",
"capture" => false
));
I assumed that I could get the charge ID by doing:
$chargeID = $charge->id;
because that's how I get the customer ID when I create a customer. But I get nothing when I echo out the variable, which doesn't help since I want to save it in a database. I've looked everywhere but can't find anything concrete or recent.
Then to complete the charge. I have to capture it this way:
$ch = \Stripe\Charge::retrieve("$chargeID");
$ch->capture();
This is all from the Stripe API Reference but it doesn't explain how to get the charge ID. It doesn't even tell you how to get the customer ID, luckily Stack Overflow came through with that one.
This is all done using PHP.
According to Stripe's Documentation, when creating a new charge you get Stripe\Charge JSON object in return (or an exception if something went wrong).
You can var_dump the object you just created to see what your object looks like, but according the the API you can use the id of the returned object:
$charge = \Stripe\Charge::create(array(
"amount" => $price,
"currency" => "usd",
"customer" => $custID, // obtained with Stripe.js
"description" => "Charge for test#example.com",
"capture" => false
));
// you can use the var_dump function to see what's inside the $charge object
// var_dump($charge);
$chargeID = $charge->id;
(And you don't need to call the Charge::retrieve again, you can use the current $charge object).

Stripe, verify card and only charge it after an action

I'm working on enabling payments for a website. I'm using Stripe as the provider. I wanted to know how I could charge the card if 2 conditions are true. When the user pays, I want to change a value in the database, and charge the card. But I don't want to charge the card if the database query fails. Similarly, I don't want to query if the card is invalid. I need both, the card to be valid and for the query to be successful. How do I do that?
Here's the code for charging the card
try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "cad",
"source" => $token,
"description" => $description)
);
} catch(\Stripe\Error\Card $e) {
// The card has been declined
}
Instead of charging the card, you should think about charging a customer.
By this I mean:
1. Create a customer
$customer = \Stripe\Customer::create(array(
"description" => "Customer for test#example.com",
"source" => "tok_15gDQhLIVeeEqCzasrmEKuv8" // obtained with Stripe.js
));
2. Create a card
$card = $customer->sources->create(array("source" => "tok_15gDQhLIVeeEqCzasrmEKuv8"));
From Stripe API Reference:
source | external_account
REQUIRED
When adding a card to a customer, the parameter name is source. The value can either be a token, like the ones returned by our Stripe.js, or a dictionary containing a user’s credit card details. Stripe will automatically validate the card.
By creating a card, Stripe will automatically validate it. So having a valid credit card object you can then perform whatever query you want on your db and if success, charge the customer.
3. Charge
\Stripe\Charge::create(array(
"amount" => 400,
"currency" => "usd",
"source" => "tok_15gDQhLIVeeEqCzasrmEKuv8", // obtained with Stripe.js,
// "customer" => $cusomer->id // the customer created above
"metadata" => array("order_id" => "6735")
));
When charging, you can either pass the source (the token obtained with Stripe.js) or the customer id we just created.
Also don't forget to try...catch everything.
Ok, so after reading through the API, I found that this is acheivable by setting the capture parameter to false
Like this:
$charge = \Stripe\Charge::create(array(
"amount" => $amount, // amount in cents, again
"currency" => "cad",
"source" => $token,
"description" => $description,
"capture" => false)
);
And this will authorize the payment and the card but not create a charge. After you do the querying and make sure it's successful, you can charge the customer (capture the charge) using this
$ch = \Stripe\Charge::retrieve({$charge->id});
$ch->capture();

Use plan and quantity to add customer using stripe

I created a customer and charge that customer by using amount instead of plan. I checked their documentation but have no idea how to do that. Can anyone help me with my below code,
// create customer
$customer = Stripe_Customer::create(array(
"card" => $_POST['stripeToken'],
"description" => "This is testing mode",
"email" => "test#mail.com",
));
// Charge the Customer
Stripe_Charge::create(array(
"amount" => 3000,
"currency" => "usd",
"customer" => $customer->id)
);
Your code looks fine, and follows that outlined here:
https://stripe.com/docs/tutorials/charges
Is there a specific question or problem you're having?
Best,
Larry
PS I work on Support at Stripe.

Getting stripe gateway to accept a variable as a amount for charge

Hi I have a variable called $total which is set from a hidden field in a form on another page
$total = $_POST['myHiddenValue'];
In Stripe I'm attempting to use this variable as an amount parameter
Stripe_Charge::create(array(
"amount" => $total,
"currency" => "gbp",
"card" => $_POST['stripeToken']));
this throws an error of 'missing amount param'
if I set $total before hand as:
$total = 5000;
it works but I don't want to hard code in a amount.
using below doesn't work either, it throws and 'Invalid positive integer' error
"amount" => (int)$total,
Does anyone know Stripe well to offer me some help.
thanks
'amount' is in cents and should be an integer and not a decimal.
$Total = '50.43';
'amount' => $Total*100,

Categories