src/Helper/StripeHelper.php line 269

Open in your IDE?
  1. <?php
  2. namespace App\Helper;
  3. use App\Entity\Customer;
  4. use GuzzleHttp\Client;
  5. use Stripe\Stripe;
  6. use Stripe\Subscription;
  7. use Stripe\Customer as StripeCustomer// Alias the Stripe Customer class
  8. class StripeHelper 
  9. {
  10.     private function create(Customer $customer,$productId,$secret)
  11.     {
  12.         $id $customer->getId(); 
  13.         if ($id === null) {
  14.             return ['url' => ''];
  15.         }
  16.         $DOMAIN $_ENV['DOMAIN'];
  17.         $PATH'/settings/membership';
  18.         $url $DOMAIN $PATH;
  19.         Stripe::setApiKey($secret);
  20.         $session = \Stripe\Checkout\Session::create([
  21.             'payment_method_types' => ['card'],
  22.             'line_items' => [[
  23.                 'price' => $productId,
  24.                 'quantity' => 1,
  25.             ]],
  26.             'mode' => 'subscription',
  27.             'success_url' => $url '?session_id={CHECKOUT_SESSION_ID}',
  28.             'cancel_url' => $url,
  29.             'metadata' => [
  30.                 'user_id' => $id,
  31.             ],
  32.         ]);
  33.         return ['url' => $session->url];
  34.     }
  35.     private function createSubscriptionIntent($customer,$priceId,$secret)
  36.     {
  37.         // Set your Stripe secret key
  38.         Stripe::setApiKey($secret);
  39.         $id $customer->getId();
  40.         if ($id === null) {
  41.             return ['clientSecret' => null'message'=> 'not found user_id'];
  42.         }
  43.         $customerId $this->getCustomer($secret,$customer);
  44.         if ($customerId === null) {
  45.             return ['clientSecret' => null'message'=> 'cannot create customer id'];
  46.         }
  47.         
  48.         $stripe = new \Stripe\StripeClient($secret);
  49.         // Create the subscription with the customer ID, price ID, and necessary options.
  50.         $subscription $stripe->subscriptions->create([
  51.             'customer' => $customerId,
  52.             'items' => [[
  53.                 'price' => $priceId,
  54.             ]],
  55.             'metadata' => [
  56.                 'user_id' => $id,
  57.             ],
  58.             'payment_behavior' => 'default_incomplete',
  59.             'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
  60.             'expand' => ['latest_invoice.payment_intent'],
  61.         ]);
  62.         // Retrieve the invoice
  63.         // $invoice = \Stripe\Invoice::retrieve($subscription->latest_invoice);
  64.         // Retrieve the associated payment intent
  65.         // $paymentIntent = \Stripe\PaymentIntent::retrieve($subscription->latest_invoice->payment_intent);
  66.         // Return the client secret to your React app
  67.         return ['clientSecret' => $subscription->latest_invoice->payment_intent->client_secret];
  68.     }
  69.     private function createSubscription($paymentMethodId$customer$priceId,$secret)
  70.     {
  71.         Stripe::setApiKey($secret);
  72.         try {
  73.             $id $customer->getId();
  74.             if ($id === null) {
  75.                 return ['subscription' => null'message'=> 'not found user_id'];
  76.             }
  77.             $customerId $this->getCustomer($secret,$customer);
  78.             // Create the payment method
  79.             $stripe = new \Stripe\StripeClient($secret);
  80.             $stripe->paymentMethods->attach(
  81.                $paymentMethodId,
  82.             ['customer' => $customerId]
  83.             );
  84.             $subscription Subscription::create([
  85.                 'customer' => $customerId,
  86.                 'items' => [['price' => $priceId]],
  87.                 'default_payment_method' => $paymentMethodId,
  88.                 'metadata' => [
  89.                     'user_id' => $id,
  90.                 ],
  91.             ]);
  92.             return ['subscription' => $subscription];
  93.         } catch (\Stripe\Exception\ApiErrorException $e) {
  94.             return ['subscription' => null'message'=> 'coud not create subscription','err'=>$e];
  95.         }
  96.     }
  97.     private function createStripeCustomer($secret$customer)
  98.     {
  99.         Stripe::setApiKey($secret);
  100.         $email $customer->getEmail(); 
  101.         try {
  102.             $stripeCustomer StripeCustomer::create([
  103.                 'email' => $email,
  104.                 // You can also add other details like name, address, etc.
  105.             ]);
  106.             return $stripeCustomer->id;
  107.         } catch (\Stripe\Exception\ApiErrorException $e) {
  108.             return null
  109.         }
  110.     }
  111.     public function getCustomer($secret,$customer)
  112.     {
  113.         // Check if customer with given email exists
  114.         Stripe::setApiKey($secret);
  115.         $email $customer->getEmail();
  116.         $customerList StripeCustomer::all(['email' => $email]);
  117.         $customerId null;
  118.         if (count($customerList->data) > 0) {
  119.             $customerId $customerList->data[0]->id;
  120.         } else {
  121.             // Customer doesn't exist, create a new customer
  122.             $customerId $this->createStripeCustomer($secret,$email);
  123.             if (!$customerId) {
  124.                 return null;
  125.             }
  126.         }
  127.         return $customerId;
  128.     }
  129.     public function getBySubscriptionKey(string $subscriptionId)
  130.     {
  131.         $stripeSecretKey $_ENV['STRIPE_SECRET_KEY_TEST'];
  132.         Stripe::setApiKey($stripeSecretKey);
  133.         try {
  134.             $subscription Subscription::retrieve($subscriptionId);
  135.             return ['info'=>$subscription];
  136.         } catch (\Stripe\Exception\ApiErrorException $e) {
  137.             return ['error' => 'Error fetching subscription information'];
  138.         }
  139.     }
  140.     public function info(string $subscriptionId)
  141.     {
  142.         $stripeSecretKey $_ENV['STRIPE_SECRET_KEY_TEST'];
  143.         Stripe::setApiKey($stripeSecretKey);
  144.         try {
  145.             $subscription Subscription::retrieve($subscriptionId);
  146.             $price $subscription->items->data[0]->price;
  147.             $subscriptionInfo = [
  148.                 'active'=> $subscription->status === 'active',
  149.                 'price_name' => $price->nickname,
  150.                 'product_id'=> $price->interval,
  151.                 'auto_renewable' => $price->recurring->interval !== '0',
  152.             ];
  153.             return $subscriptionInfo;
  154.         } catch (\Stripe\Exception\ApiErrorException $e) {
  155.             return ['error' => 'Error fetching subscription information'];
  156.         }
  157.     }
  158.     public function cancel(string $subscriptionId)
  159.     {
  160.         if (strpos($subscriptionId'sub_') === 0) {
  161.             $stripeSecretKey $_ENV['STRIPE_SECRET_KEY_TEST'];
  162.             Stripe::setApiKey($stripeSecretKey);
  163.     
  164.             try {
  165.                 $subscription Subscription::retrieve($subscriptionId);
  166.                 $subscription->cancel();
  167.     
  168.                 return ['message' => 'Subscription canceled successfully','success' => true];
  169.             } catch (\Stripe\Exception\ApiErrorException $e) {
  170.                 return ['error' => 'Error canceling subscription''success' => false];
  171.             }
  172.         }
  173.         
  174.         return ['message'=> 'Not valid Subscription: ' $subscriptionId];
  175.     }
  176.     public function validateSubscription(string $subscriptionId): bool
  177.     {
  178.         return $this->getSubscriptionById($subscriptionId)->status === 'active';
  179.     }
  180.     public function getSubscriptionById(string $subscriptionId)
  181.     {
  182.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  183.         Stripe::setApiKey($secret);
  184.         try {
  185.             return Subscription::retrieve($subscriptionId);
  186.         } catch (\Stripe\Exception\ApiErrorException $e) {
  187.             return 'Error fetching status';
  188.         }
  189.     }
  190.     public function createSeasonYearly($customer)
  191.     {
  192.         $productId $_ENV['STRIPE_PRICE_ID_YEARLY'];
  193.         $secret $_ENV['STRIPE_SECRET_KEY'];
  194.         return $this->create($customer,$productId,$secret);
  195.     }
  196.     public function createSessionMonthly($customer)
  197.     {
  198.         $productId $_ENV['STRIPE_PRICE_ID_YEARLY'];
  199.         $secret $_ENV['STRIPE_SECRET_KEY'];
  200.         return $this->create($customer,$productId,$secret);
  201.     }
  202.     public function createSeasonYearlyTest($customer)
  203.     {
  204.         $productId $_ENV['STRIPE_PRICE_ID_YEARLY_TEST'];
  205.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  206.         return $this->create($customer,$productId,$secret);
  207.     }
  208.     public function createSessionMonthlyTest($customer)
  209.     {
  210.         $productId $_ENV['STRIPE_PRICE_ID_MONTH_TEST'];
  211.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  212.         return $this->create($customer,$productId,$secret);
  213.     }
  214.     public function createMonthlySubscriptionTest($customer,$paymentId)
  215.     {
  216.         $priceId $_ENV['STRIPE_PRICE_ID_MONTH_TEST'];
  217.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  218.         return $this->createSubscription($paymentId,$customer,$priceId,$secret);
  219.     }
  220.     
  221.     public function createMonthlySubscription($customer,$paymentId)
  222.     {
  223.         $priceId $_ENV['STRIPE_PRICE_ID_MONTH'];
  224.         $secret $_ENV['STRIPE_SECRET_KEY'];
  225.         return $this->createSubscription($paymentId,$customer,$priceId,$secret);
  226.     }
  227.     public function createMonthlySubscriptionIntent($customer)
  228.     {
  229.         $priceId $_ENV['STRIPE_PRICE_ID_MONTH'];
  230.         $secret $_ENV['STRIPE_SECRET_KEY'];
  231.         return $this->createSubscriptionIntent($customer,$priceId,$secret);
  232.     }
  233.     public function createMonthlySubscriptionIntentTest($customer)
  234.     {
  235.         $priceId $_ENV['STRIPE_PRICE_ID_MONTH_TEST'];
  236.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  237.         return $this->createSubscriptionIntent($customer,$priceId,$secret);
  238.     }
  239.     public function createYearlySubscriptionTest($customer,$paymentId)
  240.     {
  241.         $priceId $_ENV['STRIPE_PRICE_ID_YEARLY_TEST'];
  242.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  243.         return $this->createSubscription($paymentId,$customer,$priceId,$secret);
  244.     }
  245.     
  246.     public function createYearlySubscription($customer,$paymentId)
  247.     {
  248.         $priceId $_ENV['STRIPE_PRICE_ID_YEARLY'];
  249.         $secret $_ENV['STRIPE_SECRET_KEY'];
  250.         return $this->createSubscription($paymentId,$customer,$priceId,$secret);
  251.     }
  252.     public function createYearlySubscriptionIntent($customer)
  253.     {
  254.         $priceId $_ENV['STRIPE_PRICE_ID_YEARLY'];
  255.         $secret $_ENV['STRIPE_SECRET_KEY'];
  256.         return $this->createSubscriptionIntent($customer,$priceId,$secret);
  257.     }
  258.     public function createYearlySubscriptionIntentTest($customer)
  259.     {
  260.         $priceId $_ENV['STRIPE_PRICE_ID_YEARLY_TEST'];
  261.         $secret $_ENV['STRIPE_SECRET_KEY_TEST'];
  262.         return $this->createSubscriptionIntent($customer,$priceId,$secret);
  263.     }
  264. }