I want to add Add On to my subscription. Thus i follow this MultiPlan in Laravel doc. For every new subscription, new stripe product are create (means every plan have different product. including the addon). User need to subscribe to any subscription before subscript to the addon. There are no error, but the Subscription DB from stripe for the current subscription will return null quantity and null stripe_plan. Then create error from that database as i cant call the current subscription. Why does the stripe does that? or am I surpose to create new plans under the same product id in Stripe?
My code to create stripe product and plan
$stripe = new StripeClient(env('STRIPE_SECRET'));
$product_stripe = $stripe->products->create([
'name' => $request->service_name,
]);
$plan_stripe = $stripe->plans->create([
// 'id' => $stripe_plan,
'nickname' => $request->service_name,
'product' => $product_stripe['id'],
'amount' => $request->price*100,
'currency' => 'usd',
'interval' => $request->period,
'interval_count' => $request->period_num,
]);
This is my code to subscribe to addon
$user = auth()->user();
$user->subscription('default')->addPlanAndInvoice('plan_stripe_id', 'quantity');
Note that default is the user current subscription.
Related
I am trying to use Stripe Checkout to allow users to set up a monthly recurring donation for an amount of their choosing. When I set up the Session, it provides the correct inputs to the Stripe form, however when I look in Subscriptions in the back end of Stripe, nothing is created, and it seems to just take a single payment. Here is my code:
$checkout_values['success_url'] = $success_url;
$checkout_values['cancel_url'] = $cancel_url;
$checkout_values['payment_method_types'] = ['card','sepa_debit'];
$checkout_values['mode'] = 'subscription';
$checkout_values['metadata']['order_id'] = $order_id;
//Single line item for the dynamically created subscription and price info
$line_item = [
'price_data' => [
'recurring' => [
'interval' => 'month',
'interval_count' => 1
],
'currency' => $order->get_currency(),
'product_data' => [
'name' => $item->get_name()
],
'unit_amount' => $item_total
],
'quantity' => 1,
];
$checkout_values['line_items'][] = $line_item;
Maybe I need to create a subscription in Stripe and tie that in? In which case why doesn't it give me an error?
you don't need to explicitly create a subscription object. If you use Stripe Checkout, a subscription will be automatically created when your customer completed the payment flow in your checkout page.
You might want to firstly check if you are viewing the right mode (either Live or Test), and then take a look at the Dashboard Events to confirm subscription related events. You application can also listen to webhook events to get notified.
Following code I'm using to upgrade a user's plan:
$subscription = \Stripe\Subscription::all(array('customer'=>$customerId,'limit'=>10));
$subscription_id = $subscription->data[0]->id;
$subscription = \Stripe\Subscription::retrieve($subscription_id);
$updatePlan = [
'cancel_at_period_end' => false,
'items' => [
[
'id' => $subscription->items->data[0]->id,
'plan' => planPrefix.$packageId,
],
],
'tax_percent' => $package_tax,
];
\Stripe\Subscription::update($subscription_id, $updatePlan);
//Create invoice now
$invoice = \Stripe\Invoice::create([
'customer' => $customerId,
]);
$invoiceId = $invoice->id;
//Pay invoice now
$invoice = \Stripe\Invoice::retrieve($invoiceId);
$invoice->pay();
And when stripe get paid to that newly created invoice, The event "invoice.payment_succeeded" is triggered to my webhook where I update my database accordingly.
The problem is that stripe sends me previous plan id with the accurate invoice.
E.g., If User A subscribes to plan id 1 then stripe sends me object for the newly subscribed plan details to my webhook with accurate data, but when User A
upgrades to plan whose id is 2, stripe sends me data with the event of "invoice.payment_succeeded" where I can find all the data related to subscription update but the issue is with the plan id. The plan id stripe sends me is old one i.e., 1 instead of 2, and when User A upgrade to plan id 3 then it sends me plan id 2 in the webhook notification.
Any help in this matter would be much appreciated.
For compliance reasons I am generating tokens on the client side and sending those details to stripe. I want to display the last four digits and the type of card on my confirmation page
I am creating a customer
// Create a Customer:
$customer = \Stripe\Customer::create([
'source' => $token,
'email' => $current_user->user_email,
]);
than adding them to a subscription
//create the subscription for the customer
$subscription = \Stripe\Subscription::create(array(
'customer' => $customer->id,
"items" => array(
array(
"plan" => "dpc-standard",
),
)
));
The subscription returns https://stripe.com/docs/api#subscription_object
a ton of data including the invoice_id that is generated for the subscription but doesn't return any CC details
When you create a Customer and pass the source parameter set to a token id, it will save that card on the new customer. The value returned by this call is a Customer object with the sources property which will contain the new card you just saved.
You can access the last 4 digits easily using:
$last4 = $customer->sources->data[0]->last4;
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.
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