Files
wticreatorstudio/Modules/WTIALaCarte/Services/StripeService.php
T

168 lines
5.1 KiB
PHP

<?php
namespace Modules\WTIALaCarte\Services;
use Carbon\Carbon;
class StripeService
{
protected $stripe;
protected $currecyCode;
public function __construct()
{
$credential = getPaymentInfoViaSellerId(1, 4);
$this->stripe = new \Stripe\StripeClient([
"api_key" => @$credential->perameter_3,
"stripe_version" => "2022-11-15"
]);
$this->currecyCode = getCurrencyCode();
}
public function createSession($items)
{
$currentSubscription = auth()->user()->currentSubscription();
$line_items = [];
foreach ($items as $key => $item) {
if($currentSubscription && $currentSubscription->status == 'active'){
$data = [
'price_data' => [
'currency' => $this->currecyCode,
'product' => $item['stripe_product_id'],
'unit_amount_decimal' => $item['final_price'] * 100
],
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 1,
'maximum' => 100,
],
'quantity' => $item['qty'],
];
} else if($currentSubscription && $currentSubscription->status == 'cancelled'){
$subscription_expired = $currentSubscription->expired_at != null ? \Carbon\Carbon::now()->gt(\Carbon\Carbon::parse($currentSubscription->expired_at)) : true;
if($subscription_expired) {
$data = [
'price' => $item['stripe_price_id'],
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 1,
'maximum' => 100,
],
'quantity' => $item['qty'],
];
} else {
$data = [
'price_data' => [
'currency' => $this->currecyCode,
'product' => $item['stripe_product_id'],
'unit_amount_decimal' => $item['final_price'] * 100
],
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 1,
'maximum' => 100,
],
'quantity' => $item['qty'],
];
}
} else {
$data = [
'price' => $item['stripe_price_id'],
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 1,
'maximum' => 100,
],
'quantity' => $item['qty'],
];
}
array_push($line_items, $data);
}
return $this->stripe->checkout->sessions->create([
'line_items' => $line_items,
'consent_collection' => [
'terms_of_service' => 'required'
],
'allow_promotion_codes' => true,
'customer_email' => auth()->user()->email,
'client_reference_id' => auth()->user()->id,
'mode' => 'payment',
'success_url' => url('/job-board'),
'cancel_url' => url('/a-la-carte'),
]);
}
public function createProduct($data)
{
return $this->stripe->products->create([
'name' => $data['name'],
'description' => $data['description'],
'images' => [
url('/public/storage/'.$data['image'])
],
'default_price_data' => [
'currency' => $this->currecyCode,
'unit_amount_decimal' => $data['price'] * 100
]
]);
}
public function updateProduct($product,$data)
{
$product_data = [
'name' => $data['name'],
'description' => $data['description'],
];
if(isset($data['image'])){
$product_data['images'] = [ url('/public/storage/'.$data['image']) ];
}
if($product->price != (float)$data['price']){
$product_data['default_price'] = $this->createProductPrice($product->stripe_product_id,$data['price']);
}
return $this->stripe->products->update(
$product->stripe_product_id,
$product_data
);
}
public function archiveProduct($productID)
{
return $this->stripe->products->update(
$productID,
['active' => false]
);
}
public function createProductPrice($stripeProductID, $price)
{
$price_data = [
'currency' => $this->currecyCode,
'unit_amount_decimal' => $price * 100,
'product' => $stripeProductID
];
return $this->stripe->prices->create($price_data);
}
public function archivePrice($priceID)
{
return $this->stripe->prices->update(
$priceID,
['active' => false]
);
}
}