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,
));
Related
I have monthly subscription plan setup in Stripe.
Creating subscription and charging is working for month by month, but I would like to charge customer 2 months in advance.
Example: user should be subscribed 6 months. I would like to charge him 1st month and last month (6th). So he won't be paying 6th month.
I was trying to charge customer once and then another time but it says that I can use stripe token only once.
How can I make this deposit payments in stripe and PHP?
To solve this do the following: Make subscription, then from it take customer ID and charge it.
$customer = Customer::create([
'card' => $request->stripe_token,
'plan' => $planID,
'email' => Auth::user()->email
]);
This will subscribe user to plan. Then charge him again:
Charge::create([
'amount' => $depositPrice,
'currency' => 'usd',
'customer' => $customer->id,
"statement_descriptor" => "Company Name Deposit",
]);
I'm building a relatively simple subscription based app using Stripe.
My only real hangup at the moment is that the subscription requires an initial setup fee, and I'm really having a hard time with it.
Example:
A new user signs-up for Subscription-A; Subscription-A has a monthly interval price of $10. Upon signing up the new user gets charged a one-time fee of $1 and $10 for subscription, then the following months gets charged only $10.
Currently my code is:
// Stripe New customer
$customer = \Stripe\Customer::create(array(
"email" => $customer_email,
"source" => $token,
),
array("stripe_account" => $connected_account)
);
// Stripe Subscription
$sub = \Stripe\Subscription::create(array(
"customer" => $customer['id'],
"items" => array(
array(
"plan" => $plan_id,
),
),
),
array("stripe_account" => $connected_account)
);
Any idea? thx
After creating the customer, but before creating the subscription, create an invoice item for the setup fee.
When you create the subscription, a first invoice will be immediately created, and this invoice will include all pending invoice items for the customer.
For more information about invoice items, see https://stripe.com/docs/subscriptions/invoices#adding-invoice-items.
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 :)
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();
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.