I'm using Stripe PHP SDK to create a subscription plan dynamically.
use \Stripe\Plan;
Plan::create(array(
"amount" => 2000,
"interval" => "month",
"name" => "Amazing Gold Plan",
"currency" => "usd",
"id" => "gold")
);
I wanted to ask is there a way to mention/handle the duration of a plan. Like I wanted to make a subscription by the plan for a limited time suppose 3 months and after 3 months I wanted the plan to automatically get removed from the subscription. I don't want to cancel the whole subscription as in my case there could be multiple plans associated with a subscription. SO I simply want to remove/detach the plan from the subscription.
I had gone through the Stripe SDK docs to find this but to no avail.
A Subscription Schedule [1] will be what you require here to automate changes to the underlying Subscription using phases [2].
The use of a schedule will allow you to specify, for example, a monthly Price that will iterate for 3 months in the first phase. Then you can define in the next phase what will happen, for example change the Prices in some way, like adding or removing Prices. An example may look like this:
\Stripe\SubscriptionSchedule::create([
'customer' => 'cus_xxx',
'start_date' => 'now',
'end_behavior' => 'release',
'phases' => [
[
'items' => [
[
'price' => 'price_print', # Monthly Price
'quantity' => 1,
],
],
'iterations' => 3, # Iterates for 3 months
],
[
'items' => [
[
'price' => 'price_print',
'quantity' => 1,
],
[
'price' => 'price_digital', # An extra price
'quantity' => 1,
],
],
'iterations' => 1, # Iterate for 1 month
],
],
]);
[1] https://stripe.com/docs/billing/subscriptions/subscription-schedules#managing
[2] https://stripe.com/docs/api/subscription_schedules/object#subscription_schedule_object-phases
I believe you need to create price instead of plan and the code would be like this (Not sure I use different platform but you can try)
use \Stripe\Plan;
$price = \Stripe\Price::create([
'product' => '{{PRODUCT_ID}}',
'unit_amount' => 1000,
'currency' => 'usd',
'recurring' => [
'interval' => 'month',
'interval_count' => 2 //Try tweaking this value for change
],
]);
Related
I'm creating a web app where I need to get the id's of the accounts that will act as benchmark accounts, at the moment what I thought is to have a separate file called bench-accounts where I'll be able to get the id and the color of each account. Then I use config('bench-accounts') to be able to get the accounts and filter them as I want. I did it this way because whoever manages this will be able to get the id of the account from the database then add it here and have it act as a benchmark account (since I think it will be the best way for people that doesn't have a lot of knowledge with programming)
The file would follow the next structure:
<?php
return [
1 => [
'id' => 1,
'color' => '#00cfff',
],
2 => [
'id' => 2,
'color' => '#ff0000',
],
130 => [
'id' => 130,
'color' => '#fff200',
],
149 => [
'id' => 149,
'color' => '#3F220F',
],
168 => [
'id' => 168,
'color' => '#00ff0f',
],
169 => [
'id' => 169,
'color' => '#5438DC',
],
170 => [
'id' => 170,
'color' => '#3E517A',
]
];
Is it correct? I'm planning on adding an active parameter so that in the future you'll be able to activate or disable it on which accounts you want to benchmark and which not.
Tried it adding the account to a config file where it will have different parameters and the ids of the accounts.
For some reason, when you create a "Subscription Schedules" stripe, it has very odd behavior where instead of trying to charge the customer, it keeps the invoice in draft for 1 hour and then closes the invoice and charges the customer.
I already have the card in the customer. I wonder if there is any way I can force the first phase of the subscription to be charged immediately.
My code:
$phases = [
[
'items' => [
[
'price_data' => [
'currency' => 'usd',
'product' => $product->stripe_product_id,
'recurring' => [
'interval' => $payment_plan['frequency'],
],
'unit_amount' => $payment_plan['stripe_amount']
],
'quantity' => 1,
],
],
'iterations' => (int) $payment_plan['total_payments']
],
];
$subscription = $stripe->subscriptionSchedules->create([
'customer' => $customer->stripe_customer_id,
'start_date' => 'now',
'end_behavior' => 'cancel',
'phases' => $phases,
]);
If you want to create a subscription immediately, you can do that without a Subscription Schedule and then set the schedule for that existing subscription:
https://stripe.com/docs/billing/subscriptions/subscription-schedules/use-cases#existing-subscription
Alternatively, if creating with the schedule like you're doing, once the subscription/invoice is created (as a draft) you can use the API to finalize it manually to proceed with the payment:
https://stripe.com/docs/api/invoices/finalize
Here is the solution:
$subscription = $stripe->subscriptionSchedules->create([
'customer' => $customer->stripe_customer_id,
'start_date' => 'now',
'end_behavior' => 'cancel',
'phases' => $phases,
]);
// now the invoice is a draft so we go get this invoice
$invoice = $stripe->invoices->all([
'limit' => 3,
'status' => 'draft',
'subscription' => $subscription->subscription,
'customer' => $customer->stripe_customer_id,
]);
//get the most recent draft
$invoice = $invoice->data[0];
//finalize the invoice (but this don't generate the payment for some reason)
$stripe->invoices->finalizeInvoice(
$invoice->id, [
'auto_advance' => true
]
);
//finaly pay the invoice
$invoice = $stripe->invoices->pay($invoice->id,[
'payment_method' => $token
]);
I'm trying to generate an invoice through stripe but the API requires a Client ID which I have no idea how to retrieve.
I'm using Session checkout:
method called by ajax
$checkout_session = Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'customer_email' => $email,
'mode' => 'payment',
'success_url' => $domain. '/success/{CHECKOUT_SESSION_ID}',
'cancel_url' => $domain. '/cancel/{CHECKOUT_SESSION_ID}',
]);
I receive
[
"id" => "cs_test_xxxxxxxxxxxxxxxx"
"object" => "checkout.session"
"allow_promotion_codes" => null
"amount_subtotal" => 2000
"amount_total" => 2000
"billing_address_collection" => null
"cancel_url" => "http://localhost/cancel/{CHECKOUT_SESSION_ID}"
"client_reference_id" => null
"currency" => "usd"
"customer" => null
"customer_details" => null
"customer_email" => "xxxxxxxxxxx#gmail.com"
"livemode" => false
"locale" => null
"metadata" => []
"mode" => "payment"
"payment_intent" => "pi_xxxxxxxxxxxxx"
"payment_method_options" => []
"payment_method_types" => array:1 [
0 => "card"
]
"payment_status" => "unpaid"
"setup_intent" => null
"shipping" => null
"shipping_address_collection" => null
"submit_type" => null
"subscription" => null
"success_url" => "http://localhost/success/{CHECKOUT_SESSION_ID}"
"total_details" => array:3 [
"amount_discount" => 0
"amount_shipping" => 0
"amount_tax" => 0
]
]
Create invoice requires a customer id which I don't have and have no idea where to get since session checkout creates a customer without giving me an id.
A Checkout Session in payment mode will generate a Payment Intent, not an Invoice. If you want to work solely with Invoices, you Checkout Session is not the right solution. If you don't care about Invoices and just want to be able to charge a customer for a one-off payment then you can continue using Checkout.
If you do want to work solely with Invoices, you need to do the following:
Create a Customer
Pass in the Customer ID when you create as many Invoice Items as you need
Pass in the Customer ID when you create an Invoice - this will automatically pull in any pending Invoice Items tied to the customer.
This is all summarized in this guide: https://stripe.com/docs/invoicing/integration
I am working on this code which is from the Stripe website
URL : https://stripe.com/docs/checkout/integration-builder
this is directly from the website and works fine for me
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
However when I change the code to the below to try to get the dollar and cents working ( using either unit_amount as in the example ... or unit_amount_decimal as suggested by this page https://stripe.com/docs/billing/subscriptions/decimal-amounts ) then there are issues which I dont really understand, particularly the error returned when using unit_amount_decimal because it appears that there's no way to do decimal places in Stripe ... which hopefully is not the case.
How would I do this if not as they suggest on that page ... which seems to not work?
same code with 2000.55 instead of 2000
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount_decimal' => 2000.55,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
network tab errors
using unit_amount Invalid integer: 2000.55
using unit_amount_decimal Checkout does not currently support usd prices with more than 2 decimal places in line_items[0]. You passed 2000.55 corresponding to $20.0055, which is not supported.
The second error seems to be suggesting that this is not possible, yet this makes no sense since products are not always even dollar amounts.
Stripe measures amounts in the smallest unit of a currency - for example cents for USD:
'unit_amount' => 2000, //2000 cents or $20
'unit_amount' => 2050, //2050 cents or $20.50
If you need to use a decimal amount of the smallest unit - e.g. 1.5 cents per action then use 'unit_amount_decimal'
'unit_amount_decimal' => 1.5 // 1.5ยข or $0.015 per action
I am creating App for Quickbook in PHP
I am using https://github.com/intuit/QuickBooks-V3-PHP-SDK/
I want to make payments to multiple invoice in single API call.
I see their example but those make single payment. I need multiple payments towards various invoice for different customers.
How that is possible?
Their example code is here:
https://github.com/IntuitDeveloper/SampleApp-CRUD-PHP/blob/master/CRUD_Examples/Payment/PaymentCreate.php
So here's a snippet:
$theResourceObj = Payment::create([
"CustomerRef" =>
[
"value" => "1"
],
"TotalAmt" => 100.00,
"Line" => [
[
"Amount" => 100.00,
"LinkedTxn" => [
[
"TxnId" => "210",
"TxnType" => "Invoice"
]]
]]
]);
$resultingObj = $dataService->Add($theResourceObj);
Then if you refer to their documentation:
https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/payment#the-payment-object
You can see that the Line element is composed of Zero or more transactions accounting for this payment. including The invoice to which payment is applied. i.e. you can repeat things within Line, to apply the payment to more than one invoice.
So applying a payment to more than one invoice lists each individual invoice, and the amount of the payment to apply, in the repeating Line node array:
$theResourceObj = Payment::create([
"CustomerRef" =>
[
"value" => "1"
],
"TotalAmt" => 100.00,
"Line" => [
[
"Amount" => 50.00,
"LinkedTxn" => [
[
"TxnId" => "210",
"TxnType" => "Invoice"
]]
],
[
"Amount" => 25.00,
"LinkedTxn" => [
[
"TxnId" => "211",
"TxnType" => "Invoice"
]]
],
[
"Amount" => 25.00,
"LinkedTxn" => [
[
"TxnId" => "212",
"TxnType" => "Invoice"
]]
],
]
]);
$resultingObj = $dataService->Add($theResourceObj);
Finally I found solution for me.
API have Batch Option to add multiple Objects in single Batch.
Thanks to everyone