Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

Stripe & Laravel how to upgrade or downgrade session subscription?

I have some issues using the Laravel Cashier for creating subscriptions.

First, from my backend I am creating a Package, which calls the following two Strip functions:

public function createStripeProduct(array $data)
{
    $product = $this->stripe->products->create([
        'name' => $data['title']." ".appName(),
    ]);
    
    return $product->id;
}

public function createStripePrice(array $data)
{
    $price = $this->stripe->prices->create([
        'unit_amount' => $data['price'] * $this->multiple,
        'currency' => $this->currency,
        'recurring' => ['interval' => 'month'],
        'product' => $data['stripe_prod_id'],
    ]);
    
    return $price->id;
}

Then in my Controller, I am creating the session:

public function create(Request $request)
{
    $key = config('services.stripe.secret');
    $stripe = new StripeStripeClient($key);
    $stripeCustomer = $user->createOrGetStripeCustomer();

    $checkout_session = $stripe->checkout->sessions->create([
        'customer' => $stripeCustomer['id'],
        'success_url' =>  route('frontend.user.account'),
        'cancel_url' => route('frontend.user.account'),
        'payment_method_types' => ['card'],
        'line_items' => [
            [
                'price' => $request->stripe_price_id,
                'quantity' => 1,
            ],
        ],
        'mode' => 'subscription',
        'allow_promotion_codes' => true,
    ]);

    return $checkout_session['id'];
}

Everything is working so far, but with the implementation, I can subscribe one use multiple times to the same or to a different Package.

How can I prevent this from happening and also how to implement a future upgrade/downgrade of the Package?

question from:https://stackoverflow.com/questions/65905403/stripe-laravel-how-to-upgrade-or-downgrade-session-subscription

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To answer your two questions:

1) I can subscribe one use multiple times to the same or to a different Package. How can I prevent this from happening

Your code is fetching a Stripe Customer object in createOrGetStripeCustomer(). You can list all Subscriptions on the Customer with https://stripe.com/docs/api/subscriptions/list#list_subscriptions-customer and then check if you want to create an additional CheckoutSession Subscription on that Customer.

2) how to implement a future upgrade/downgrade of the Package?

You would use the code snippets here: https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#changing where you update the Subscription's SubscriptionItem with a new Price ID.

$sub = StripeSubscription::update('sub_123', [
  'cancel_at_period_end' => false,
  'proration_behavior' => 'create_prorations',
  'items' => [
    [
      'id' => $subscription->items->data[0]->id,
      'price' => 'price_456', // the new Price to update to
    ],
  ],
]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...