Adding items to Stripe invoices with PHP - php

I followed the API reference here but when I try to create and invoice, I get this error:
missing an item
So I created some items and now I want to add an item to the invoice but I don't understand how to write it in the invoice array.
Right now I have this code. It creates the invoice but with ALL the items I created until now. How do I specify the item within the function?
try {
require_once('./Stripe/init.php');
\Stripe\Stripe::setApiKey("sk_test_iP2aEsMDAc0ZCh5XGdE6AOnt");
$customer = \Stripe\Invoice::create(array(
"customer" => "cus_CML6eYLJif4EJ5",
"billing" => "charge_automatically",
"description" => "Testing invoices"
));
echo 'Invoice created';
}

More info
\Stripe\Stripe::setApiKey("sk_test_iP2aEsMDAc0ZCh5XGdE6AOnt");
\Stripe\InvoiceItem::create(array(
"customer" => "cus_CML6eYLJif4EJ5",
"amount" => 2500,
"currency" => "usd",
"description" => "One-time setup fee")
);

Related

Stripe Bi-Weekly Subscription Description

Using Stripe's PHP API I was able to create a bi-weekly subscription for my customers but I'm having an issue, the "description" on all subscriptions is defaulting to "Subscription creation" and I can't seem to find a way to add a description although I thought the following code worked in the past (I updated the API since then). Please see my code below
case "BiWeekly":
try {
$product = \Stripe\Product::create([
"name" => "NEO Bi-Weekly Payments for $cname",
"type" => "service",
"statement_descriptor" => "NEO",
]);
$plan = \Stripe\Plan::create([
"product" => $product->id,
"amount" => $totalAmount,
"currency" => "usd",
"interval" => "week",
"interval_count" => 2,
"usage_type" => "licensed",
]);
$subscription = \Stripe\Subscription::create([
"customer" => $customer->id,
"items" => [["plan" => $plan->id]],
"metadata" => array("Name" => $cname, "For" => "NEO Bi-Wkly Pymts")
]);
} catch(\Stripe\Error\Card $e) {
$body = $e->getJsonBody();
$err = $body['error'];
header("Location: https://www.neo.com/declined/");
exit();
};
break;
Any help would be greatly appreciated!
As clarified in the comments - description='Subscription creation' is on the corresponding PaymentIntent (not the Subscription).
There's no easy way to do this - it's not possible to specify a description when creating the Subscription to automatically populate on the corresponding PaymentIntent.
What I suggest is to :
Create the Subscription with metadata
listen for the invoice.payment_succeeded webhook event - https://stripe.com/docs/webhooks
The invoice.payment_succeeded will contain the metadata from step 1 in lines and the payment_intent
Example
...
"lines": {
"object": "list",
"data": [
{
"id": "il_...",
"object": "line_item",
...
"metadata": {
"subscription_metadata": "some_value"
},
...
"payment_intent": "pi_...",
...
using the data from step 3, make a request to update the PaymentIntent's description
On a side note, you should no longer be creating Plans (which is deprecated), but should be creating Prices instead.

Stripe Create Invoice and auto charge

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
]
]);

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
]);

How to create variable subscriptions in Stripe

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

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