How to create variable subscriptions in Stripe - php

I am using stripe to capture credit cards. I have my forms so that they are variable inputs, and it works great. I am passing the information from my <input> to a charge.php file and it captures successfully.
When I try to use this information to create subscriptions, I am unable to use variable amounts. I can only create a subscription that has a set amount.
I was hoping to use the $finalamount to set the amount of the subscription.
I am okay with the name and id to be the same as the amount.
How can I create a variable subscription including custom amount, name, and id based on what the user inputs?
<?php
require_once('init.php');
\Stripe\Stripe::setApiKey("sk_test_***********");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$amount = $_POST['amount'];
$finalamount = $amount * 100;
\Stripe\Plan::create(array(
"amount" => $finalamount, //this does not work. It only works if there is a present amount.
"interval" => "month",
"name" => "Green Plan",
"currency" => "usd",
"id" => "green")
);
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $token,
"plan" => "green",
"description" => "Description",
"email" => $email)
);
// Charge the Customer instead of the card
\Stripe\Charge::create(array(
"amount" => $finalamount, // amount in cents, again
"currency" => "usd",
"customer" => $customer->id)
);
?>

You need to remove this code
\Stripe\Charge::create(array(
"amount" => $finalamount, // amount in cents, again
"currency" => "usd",
"customer" => $customer->id)
);
because when you create a customer specifying plan your customer is subscribed and charged automatically.
hope it helps :)

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! :)

stripe charge with existing consumer

I use this code to make a payment and create a new customer on my stripe dashboard.
$customer = \Stripe\Customer::create([
"name" => $utente,
"source" => $_POST['stripeToken'],
"email" => $row['mail'],
]);
$charge = \Stripe\Charge::create([
"customer" => $customer->id,
"amount" => $amount_cents,
"currency" => "eur",
"description" => $description
]);
The information such as the name or email the use to create a new customer, are taken from the dashboard of my site when a user is registered and connected to it.
This concern the user's first payment .php file.
on the second user's payment, the site redirect to a second .php file, where I need to add a second payment to the customer previously created. the problem is that if I keep the same code, on the stripe dashboard I find myself two equal customers. I don't know how to get the customer id when, when I redirect to the second payment page, as user information I only have the mail and the name .. how can I do?
I'll try something like that by searching on stripe api reference:
$customer = \Stripe\Customer::all([
'email' => $row['mail']
]);
$charge = \Stripe\Charge::create([
"customer" => $customer['data']->id,
"amount" => $amount_cents,
"currency" => "eur",
"description" => $description
]);
but it give me the error
Must provide source or customer..
Listing customers will return an object with a data property. data is a list of customers so you'll need to index into that list and grab one of the customers before looking at their ID. It should look something like:
$customer = \Stripe\Customer::all([
'email' => $row['mail']
]);
$charge = \Stripe\Charge::create([
'customer' => $customer->data[0]->id,
'amount' => $amount_cents,
'currency' => 'eur',
'description' => $description
]);

add subscription for stripe payment gateways for future dates

I'm building stripe payment gateway in PHP, all things working. but I need to create first subscription for future dates(that user select). its always starts with current date when I submit details. Please help...
I'm not able to find there is date parameter for start subscription from specific date. Please help so I can complete payment gateway.
https://stripe.com/docs/api#subscriptions
$charge = \Stripe\Charge::create(array(
"amount" => 1,
"currency" => "usd",
"customer" => $stripe_custmerid,
"description" => $_SESSION['user_name']
));
$subscription = \Stripe\Subscription::create(array(
"customer" => $stripe_custmerid,
"plan" => $plan_id,
));

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.

Categories