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
]);
Related
I'm using Stripe with PHP, I want to create subscription to customer after a payment made with Stripe Checkout Session.
But, the problem is that when we do a one shot payment with Stripe Checkout Session, Stripe does not attach the source (Credit card) to the customer, and when I try to subscribe this customer to a plan, Stripe return me the error :
This customer has no attached payment source or default payment method.
I don't understand why Stripe don't attach the card to the customer with one shot payment because Stripe do it when we use a Checkout Session with Subscription.
How I create checkout session :
$checkout_session = Session::create([
'payment_method_types' => ['card'],
'mode' => 'payment',
'customer' => 'cus_xxxxxxxx',
'line_items' => [[
'price' => 'price_xxxxx', // Oneshot payment
'quantity' => 1,
]]
]);
How I try to subscribe customer (After successful payment) :
$subscription = Subscription::create([
'customer' => 'cus_xxxxxxxx',
'items' => [
['price' => 'price_XXXXXXX'] // Recurrent payment
],
'trial_from_plan' => true,
]);
Thank you for your help
When accepting a one-time payment with Checkout, it won't create a Customer or save card details by default. What you have to do is configure Checkout to do this for you during the payment. This is covered in their documentation here.
What you need to do is set the payment_intent_data[setup_future_usage] parameter like this:
$checkout_session = Session::create([
'payment_method_types' => ['card'],
'mode' => 'payment',
'customer' => 'cus_xxxxxxxx',
'line_items' => [[
'price' => 'price_xxxxx', // Oneshot payment
'quantity' => 1,
]],
'payment_intent_data' => [
'setup_future_usage' => 'off_session',
]
]);
After that, you will have a Customer cus_123 and a PaymentMethod pm_123 that you can re-use. That PaymentMethod will not be the default one though so you need to make sure that you pass its id in the default_payment_method parameter when you create the Subscription like this:
$subscription = Subscription::create([
'customer' => 'cus_xxxxxxxx',
'items' => [
['price' => 'price_XXXXXXX'] // Recurrent payment
],
'trial_from_plan' => true,
'default_payment_method' => 'pm_12345',
]);
Also note that you can start a Subscription directly from Checkout as documented here.
I am creating an invoice and auto charging a customer via their PHP package. The issue is though the invoice is created but the card on file is not charged, it says fail. This is odd to me because if I go into the dashboard online, find the invoice the is listed as "failed" and manually click the "retry charge" it works. Why would this be? Its clearly creating the invoice correctly and trying to charge the card on file but the card seems to only clear on ALL of these invoices if I go in and manually retry the charge.
$stripe = new \Stripe\StripeClient(config('XXXXXXXXXXXXXXXXX'));
$item = $stripe->invoiceItems->create([
'customer' => $subscription->stripe_customer,
'amount' => $total,
'currency' => 'usd',
'description' => 'Erro Coffee Subscription ' . $subscription_type->bags
]);
$invoice = $stripe->invoices->create([
'customer' => $subscription->stripe_customer,
'collection_method' => 'charge_automatically',
'auto_advance' => true,
"metadata" => [
"order_type" => "subscription",
"subscription_refill" => "true",
"subscription_id" => $subscription->id
]
]);
here is my code to create a subscription:
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id, //customer id from previous lines after creating customer
"plan" => 'premium-plan',
'metadata' => ['user_id' => $userId]
));
here is my code to update plans:
$subscriptionUpdate = \Stripe\Subscription::retrieve($subscriptionIdFromDatabase);
$subscriptionUpdate->plan = 'best-premium-plan';
$subscriptionUpdate->save();
How can I add metadata to an invoice if the user wants to update plans?
if the user wants to update their plan using the second block of code, it will generate an invoice. how can i assign metadata to that invoice when user changes plans?
Perform an update on the invoice.
Invoice::update($invoiceId, ['metadata' => [
'my_data' => $someVar
]]);
https://stripe.com/docs/api/metadata
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 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.