Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ankur_g's avatar

Stripe payment integration in laravel for buying subscription

I have a requirement to buy subscription that is present.

I do no know how to implement this.

I am implementing in the following way, although this code I have got from a website :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe;
use Exception;
use Illuminate\Support\Facades\Validator;
use Stripe\Exception\CardException;
use Stripe\StripeClient;

class StripePaymentController extends Controller
{
    const COMPONENT_CODE = 'RS_PAYMENT';

    private $stripe;

    public function __construct()
    {
        Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
        $this->stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
    }

    public function payment(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'fullName' => 'required',
            'cardNumber' => 'required',
            'month' => 'required',
            'year' => 'required',
            'cvv' => 'required'
        ]);

        if ($validator->fails()) {
            return $this->returnErrorResponse(self::COMPONENT_CODE, 101, array_column( $validator->errors()->messages() , 0), 422);
        }

        $token = $this->createToken($request);
      
        if (!empty($token['error'])) {
            return $this->returnErrorResponse(self::COMPONENT_CODE, 102, 'Invalid token', 422);
        }
        if (empty($token['id'])) {
            return $this->returnErrorResponse(self::COMPONENT_CODE, 102, 'ID does not exist', 422);
        }

        $charge = $this->createCharge($token['id'], 2000);
        if (!empty($charge) && $charge['status'] == 'succeeded') {
            $returnArray['status'] = 'Success';
            $returnArray['message'] = 'Payment Successful';
            return response()->json($returnArray, 200);
        } else {
            return $this->returnErrorResponse(self::COMPONENT_CODE, 102, 'Payment failed', 422);
        }
    }

    private function createToken($cardData)
    {
        $token = null;
        try {
            $token = $this->stripe->tokens->create([
                'card' => [
                    'number' => $cardData['cardNumber'],
                    'exp_month' => $cardData['month'],
                    'exp_year' => $cardData['year'],
                    'cvc' => $cardData['cvv']
                ]
            ]);
        } catch (CardException $e) {
            $token['error'] = $e->getError()->message;
        } catch (Exception $e) {
            $token['error'] = $e->getMessage();
        }
        return $token;
    }

    private function createCharge($tokenId, $amount)
    {
        $charge = null;
        try {
            $charge = $this->stripe->charges->create([
                'amount' => $amount,
                'currency' => 'usd',
                'source' => $tokenId,
                'description' => 'My first payment'
            ]);
        } catch (Exception $e) {
            $charge['error'] = $e->getMessage();
        }
        return $charge;
    }
}

Now I am testing with the following request:

{
    "fullName":"TestName",
    "cardNumber":"4242424242424242",
    "month":12,
    "year":2030,
    "cvv":123
}

It shows the error: Invalid token as no token is being created.

Kindly help, where am I going completely wrong, or is there any other way that I can try.

0 likes
5 replies
ankur_g's avatar

@tykus No sir I do not know about laravel-cashier, I was told to integrate the stripe payment method only. That is why I had created an account on Stipe as I saw from some websites.

martinbean's avatar

@ankur_g Don’t use the env helper outside of configuration files, and absolute do not handle card details. That data should not be touching your server at all and defeats the point of using Stripe.

ankur_g's avatar

@martinbean Sir could you kindly suggest what changes need to be done, or could you refer any source from where I will be able to get any help, I am not been able to understand this process of integration.

martinbean's avatar

@ankur_g You really need to read the Stripe docs. You should not be handling card details on your site at all. That’s the point of Stripe. It handles any transmission of card details. Otherwise you become liable for PCI compliance, and can be liable for very large fines if you don’t handle payment information properly and securely.

Please or to participate in this conversation.