<?php
namespace App\Helper;
use App\Entity\Customer;
use GuzzleHttp\Client;
use Stripe\Stripe;
use Stripe\Subscription;
use Stripe\Customer as StripeCustomer; // Alias the Stripe Customer class
class StripeHelper
{
private function create(Customer $customer,$productId,$secret)
{
$id = $customer->getId();
if ($id === null) {
return ['url' => ''];
}
$DOMAIN = $_ENV['DOMAIN'];
$PATH= '/settings/membership';
$url = $DOMAIN . $PATH;
Stripe::setApiKey($secret);
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price' => $productId,
'quantity' => 1,
]],
'mode' => 'subscription',
'success_url' => $url . '?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $url,
'metadata' => [
'user_id' => $id,
],
]);
return ['url' => $session->url];
}
private function createSubscriptionIntent($customer,$priceId,$secret)
{
// Set your Stripe secret key
Stripe::setApiKey($secret);
$id = $customer->getId();
if ($id === null) {
return ['clientSecret' => null, 'message'=> 'not found user_id'];
}
$customerId = $this->getCustomer($secret,$customer);
if ($customerId === null) {
return ['clientSecret' => null, 'message'=> 'cannot create customer id'];
}
$stripe = new \Stripe\StripeClient($secret);
// Create the subscription with the customer ID, price ID, and necessary options.
$subscription = $stripe->subscriptions->create([
'customer' => $customerId,
'items' => [[
'price' => $priceId,
]],
'metadata' => [
'user_id' => $id,
],
'payment_behavior' => 'default_incomplete',
'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
'expand' => ['latest_invoice.payment_intent'],
]);
// Retrieve the invoice
// $invoice = \Stripe\Invoice::retrieve($subscription->latest_invoice);
// Retrieve the associated payment intent
// $paymentIntent = \Stripe\PaymentIntent::retrieve($subscription->latest_invoice->payment_intent);
// Return the client secret to your React app
return ['clientSecret' => $subscription->latest_invoice->payment_intent->client_secret];
}
private function createSubscription($paymentMethodId, $customer, $priceId,$secret)
{
Stripe::setApiKey($secret);
try {
$id = $customer->getId();
if ($id === null) {
return ['subscription' => null, 'message'=> 'not found user_id'];
}
$customerId = $this->getCustomer($secret,$customer);
// Create the payment method
$stripe = new \Stripe\StripeClient($secret);
$stripe->paymentMethods->attach(
$paymentMethodId,
['customer' => $customerId]
);
$subscription = Subscription::create([
'customer' => $customerId,
'items' => [['price' => $priceId]],
'default_payment_method' => $paymentMethodId,
'metadata' => [
'user_id' => $id,
],
]);
return ['subscription' => $subscription];
} catch (\Stripe\Exception\ApiErrorException $e) {
return ['subscription' => null, 'message'=> 'coud not create subscription','err'=>$e];
}
}
private function createStripeCustomer($secret, $customer)
{
Stripe::setApiKey($secret);
$email = $customer->getEmail();
try {
$stripeCustomer = StripeCustomer::create([
'email' => $email,
// You can also add other details like name, address, etc.
]);
return $stripeCustomer->id;
} catch (\Stripe\Exception\ApiErrorException $e) {
return null;
}
}
public function getCustomer($secret,$customer)
{
// Check if customer with given email exists
Stripe::setApiKey($secret);
$email = $customer->getEmail();
$customerList = StripeCustomer::all(['email' => $email]);
$customerId = null;
if (count($customerList->data) > 0) {
$customerId = $customerList->data[0]->id;
} else {
// Customer doesn't exist, create a new customer
$customerId = $this->createStripeCustomer($secret,$email);
if (!$customerId) {
return null;
}
}
return $customerId;
}
public function getBySubscriptionKey(string $subscriptionId)
{
$stripeSecretKey = $_ENV['STRIPE_SECRET_KEY_TEST'];
Stripe::setApiKey($stripeSecretKey);
try {
$subscription = Subscription::retrieve($subscriptionId);
return ['info'=>$subscription];
} catch (\Stripe\Exception\ApiErrorException $e) {
return ['error' => 'Error fetching subscription information'];
}
}
public function info(string $subscriptionId)
{
$stripeSecretKey = $_ENV['STRIPE_SECRET_KEY_TEST'];
Stripe::setApiKey($stripeSecretKey);
try {
$subscription = Subscription::retrieve($subscriptionId);
$price = $subscription->items->data[0]->price;
$subscriptionInfo = [
'active'=> $subscription->status === 'active',
'price_name' => $price->nickname,
'product_id'=> $price->interval,
'auto_renewable' => $price->recurring->interval !== '0',
];
return $subscriptionInfo;
} catch (\Stripe\Exception\ApiErrorException $e) {
return ['error' => 'Error fetching subscription information'];
}
}
public function cancel(string $subscriptionId)
{
if (strpos($subscriptionId, 'sub_') === 0) {
$stripeSecretKey = $_ENV['STRIPE_SECRET_KEY_TEST'];
Stripe::setApiKey($stripeSecretKey);
try {
$subscription = Subscription::retrieve($subscriptionId);
$subscription->cancel();
return ['message' => 'Subscription canceled successfully','success' => true];
} catch (\Stripe\Exception\ApiErrorException $e) {
return ['error' => 'Error canceling subscription', 'success' => false];
}
}
return ['message'=> 'Not valid Subscription: ' . $subscriptionId];
}
public function validateSubscription(string $subscriptionId): bool
{
return $this->getSubscriptionById($subscriptionId)->status === 'active';
}
public function getSubscriptionById(string $subscriptionId)
{
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
Stripe::setApiKey($secret);
try {
return Subscription::retrieve($subscriptionId);
} catch (\Stripe\Exception\ApiErrorException $e) {
return 'Error fetching status';
}
}
public function createSeasonYearly($customer)
{
$productId = $_ENV['STRIPE_PRICE_ID_YEARLY'];
$secret = $_ENV['STRIPE_SECRET_KEY'];
return $this->create($customer,$productId,$secret);
}
public function createSessionMonthly($customer)
{
$productId = $_ENV['STRIPE_PRICE_ID_YEARLY'];
$secret = $_ENV['STRIPE_SECRET_KEY'];
return $this->create($customer,$productId,$secret);
}
public function createSeasonYearlyTest($customer)
{
$productId = $_ENV['STRIPE_PRICE_ID_YEARLY_TEST'];
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
return $this->create($customer,$productId,$secret);
}
public function createSessionMonthlyTest($customer)
{
$productId = $_ENV['STRIPE_PRICE_ID_MONTH_TEST'];
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
return $this->create($customer,$productId,$secret);
}
public function createMonthlySubscriptionTest($customer,$paymentId)
{
$priceId = $_ENV['STRIPE_PRICE_ID_MONTH_TEST'];
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
return $this->createSubscription($paymentId,$customer,$priceId,$secret);
}
public function createMonthlySubscription($customer,$paymentId)
{
$priceId = $_ENV['STRIPE_PRICE_ID_MONTH'];
$secret = $_ENV['STRIPE_SECRET_KEY'];
return $this->createSubscription($paymentId,$customer,$priceId,$secret);
}
public function createMonthlySubscriptionIntent($customer)
{
$priceId = $_ENV['STRIPE_PRICE_ID_MONTH'];
$secret = $_ENV['STRIPE_SECRET_KEY'];
return $this->createSubscriptionIntent($customer,$priceId,$secret);
}
public function createMonthlySubscriptionIntentTest($customer)
{
$priceId = $_ENV['STRIPE_PRICE_ID_MONTH_TEST'];
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
return $this->createSubscriptionIntent($customer,$priceId,$secret);
}
public function createYearlySubscriptionTest($customer,$paymentId)
{
$priceId = $_ENV['STRIPE_PRICE_ID_YEARLY_TEST'];
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
return $this->createSubscription($paymentId,$customer,$priceId,$secret);
}
public function createYearlySubscription($customer,$paymentId)
{
$priceId = $_ENV['STRIPE_PRICE_ID_YEARLY'];
$secret = $_ENV['STRIPE_SECRET_KEY'];
return $this->createSubscription($paymentId,$customer,$priceId,$secret);
}
public function createYearlySubscriptionIntent($customer)
{
$priceId = $_ENV['STRIPE_PRICE_ID_YEARLY'];
$secret = $_ENV['STRIPE_SECRET_KEY'];
return $this->createSubscriptionIntent($customer,$priceId,$secret);
}
public function createYearlySubscriptionIntentTest($customer)
{
$priceId = $_ENV['STRIPE_PRICE_ID_YEARLY_TEST'];
$secret = $_ENV['STRIPE_SECRET_KEY_TEST'];
return $this->createSubscriptionIntent($customer,$priceId,$secret);
}
}