stripe future subscription is not working

531 Views Asked by At

I am working on future subscription, when i run the code, It create 2 subscription 1 is active and another is trial, don't know why is it creating 2 subscription ? For that active subscription it creates payment that is wrong, i have set the trial_end as "2018-06-20" still it started subscription from today, can anyone please help me how can i resolve this issue ? here i have added my code

<?php
require_once('init.php');
if (isset($_POST['stripeToken'])) {
    \Stripe\Stripe::setApiKey("*****************");
    \Stripe\Stripe::setApiVersion("2018-05-21");
    $token = $_POST['stripeToken'];
    try {
        $plan_id = time();
    /************ check if plan exists ***************/
    $plan_created = \Stripe\Plan::create(array(
                "amount" => 1200,
                "interval" => "day",
                "product" => array(
                "name" => "test",
                ),  
                "currency" => "usd",
                "id" => $plan_id,
            )
    );

        //Create Customer:
        $customer = \Stripe\Customer::create(array(
                    'source' => $token,
            'description'=> 'Test Customer',
                    'email' => '[email protected]',
                    'plan' => $plan_id
        ));
        // Charge the order:
    $dateTime = new DateTime('2018-06-20'); 
    $date_timestamp = $dateTime->format('U');

        $charge = \Stripe\Subscription::create(array(
                    'customer' => $customer->id,
                    "items" => array(
                        array(
                            "plan" => $plan_id,
                        ),
                    ),
            "trial_end"=>$date_timestamp,
                )
        );

        echo "<pre>";
        print_r($charge);
    die;

    } catch (Stripe\Error\Card $e) {
        // The card has been declined
    }
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<form action="" method="post">
    <script src="http://checkout.stripe.com/v2/checkout.js" class="stripe-button"
            data-key="pk_test_4Ak5l6azsnSsVrpVJbIepoBu"
    data-amount="5000" data-description="One year's subscription"></script>
</form>

<script>
    $(document).ready(function () {
        $("button").trigger("click");
    });
</script>
1

There are 1 best solutions below

0
karllekko On

The reason you are seeing two subscriptions is that your creates two of them. The first is created when you call Customer::Create and pass the plan:

'plan' => $plan_id

This subscription has no trial period because you didn't specify one on the plan and that is all that is used when creating this particular subscription.

The other, separate subscription was created from your Subscription::create call. This is the correct subscription and it has the correct trial period set.

To fix this issue, I would just remove the plan from where you create the Customer and keep your existing code for creating the subscription, since that works fine.