Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying subscription id on Stripe after creating a subscription in php

I can't seem to figure this out. So what I am doing is having a auto pay page. The customer hits autopay, and the php script will create the subscription with stripe. What I can't figure out is how to get that subscription id to save it on my db. Here is what I have:

$cu = Stripe_Customer::retrieve("$stripename"); 
$cu->subscriptions->create(array("plan" => "cocacola"));

So this creates the subscription in stripe fine. I now want to save that new subscription id in my database. I can get a list of data by using this line:

echo "$cu->subscriptions";

The subscription id is displayed there along with a lot of other data, but I cant seem to isolate it and save it into a string.

The reply from the echo:

{
    "object": "list",
    "count": 1,
    "url": "/v1/customers/cus_3bgSLHOiTUHFjq/subscriptions",
    "data": [
        {
            "id": "sub_3c3YirUU5jAftr",
            "plan": {
                "id": "cocacola",
                "interval": "month",
                "name": "cocacola",
                "created": 1392273785,
                "amount": 80000,
                "currency": "usd",
                "object": "plan",
                "livemode": false,
                "interval_count": 1,
                "trial_period_days": null,
                "metadata": []
            },
            "object": "subscription",
            "start": 1394065982,
            "status": "active",
            "customer": "cus_3bgSLHOiTUHFjq",
            "cancel_at_period_end": false,
            "current_period_start": 1394065982,
            "current_period_end": 1396744382,
            "ended_at": null,
            "trial_start": null,
            "trial_end": null,
            "canceled_at": null,
            "quantity": 1,
            "application_fee_percent": null,
            "discount": null
        }
    ]
}

I am trying to save "sub_3c3YirUU5jAftr" into a string.

like image 760
user3386076 Avatar asked Nov 01 '22 03:11

user3386076


1 Answers

    Stripe::setApiKey("Use your api key"); // use your api key
            $subscription = Stripe_Customer::create(array(
                'source' => 'Use your token', // Pass Token
                'plan' => '1', // Pass plan id
                'customer' => 'Use Customer Id' // Pass customer id
                )
              ); 
echo $subscription['subscription']['id'];

Here you can get the data in side the variable $subscription

like image 111
Ankit Patel Avatar answered Nov 13 '22 16:11

Ankit Patel