mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-31 05:16:49 -04:00
211 lines
5.5 KiB
PHP
211 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Subscription\Services;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
class StripePaymentService
|
|
{
|
|
protected $stripePayment;
|
|
protected $currecyCode;
|
|
|
|
public function __construct()
|
|
{
|
|
$credential = getPaymentInfoViaSellerId(1, 4);
|
|
$this->stripePayment = new \Stripe\StripeClient([
|
|
"api_key" => @$credential->perameter_3,
|
|
"stripe_version" => "2022-11-15"
|
|
]);
|
|
$this->currecyCode = getCurrencyCode();
|
|
}
|
|
|
|
public function createProduct($data)
|
|
{
|
|
return $this->stripePayment->products->create([
|
|
'name' => $data['title'],
|
|
'images' => [
|
|
url($data['image'])
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function updateProduct($plan,$data)
|
|
{
|
|
return $this->stripePayment->products->update(
|
|
$plan->stripe_product_id,
|
|
['name' => $data['title']]
|
|
);
|
|
}
|
|
|
|
public function archiveProduct($productID)
|
|
{
|
|
return $this->stripePayment->products->update(
|
|
$productID,
|
|
['active' => false]
|
|
);
|
|
}
|
|
|
|
public function createProductPrice($stripeProductID, $data)
|
|
{
|
|
$intervals = [
|
|
'Daily' => 'day',
|
|
'Weekly' => 'week',
|
|
'Monthly' => 'month',
|
|
'Yearly' => 'year'
|
|
];
|
|
|
|
if($data['billing_type'] == 'Recurring'){
|
|
|
|
if($data['interval'] == 'Custom'){
|
|
|
|
$custom_intervals = [
|
|
'Days' => 'day',
|
|
'Weeks' => 'week',
|
|
'Months' => 'month'
|
|
];
|
|
$recurring = [
|
|
'interval' => $custom_intervals[$data['custom_interval_type']],
|
|
'interval_count' => $data['custom_interval']
|
|
];
|
|
} else {
|
|
$recurring = [
|
|
'interval' => $intervals[$data['interval']]
|
|
];
|
|
}
|
|
|
|
$price_data = [
|
|
'currency' => $this->currecyCode,
|
|
'recurring' => $recurring,
|
|
'unit_amount_decimal' => $data['price'] * 100,
|
|
'product' => $stripeProductID
|
|
];
|
|
|
|
} else {
|
|
|
|
$price_data = [
|
|
'currency' => $this->currecyCode,
|
|
'unit_amount_decimal' => $data['price'] * 100,
|
|
'product' => $stripeProductID
|
|
];
|
|
}
|
|
|
|
return $this->stripePayment->prices->create($price_data);
|
|
}
|
|
|
|
|
|
public function updateProductPrice($price,$data)
|
|
{
|
|
//lets archive first the price then create new one
|
|
$this->archivePrice($price->stripe_price_id);
|
|
return $this->createProductPrice($price->plan->stripe_product_id,$data);
|
|
}
|
|
|
|
|
|
public function archivePrice($priceID)
|
|
{
|
|
return $this->stripePayment->prices->update(
|
|
$priceID,
|
|
['active' => false]
|
|
);
|
|
}
|
|
|
|
public function paymentDetails($subscription)
|
|
{
|
|
if($subscription->billing_type == 'Recurring'){
|
|
return $this->stripePayment->subscriptions->retrieve(
|
|
$subscription->txn_id,
|
|
[]
|
|
);
|
|
} else {
|
|
return $this->stripePayment->charges->retrieve(
|
|
$subscription->txn_id,
|
|
[]
|
|
);
|
|
}
|
|
}
|
|
|
|
public function createCoupon($data)
|
|
{
|
|
if($data['type'] == 'Amount'){
|
|
|
|
$coupon_data = [
|
|
'id' => $data['code'],
|
|
'name' => $data['name'],
|
|
'amount_off' => $data['discount'] * 100,
|
|
'currency' => $this->currecyCode,
|
|
'duration' => strtolower($data['duration'])
|
|
];
|
|
|
|
} else {
|
|
|
|
$coupon_data = [
|
|
'id' => $data['code'],
|
|
'name' => $data['name'],
|
|
'percent_off' => $data['discount'],
|
|
'duration' => strtolower($data['duration'])
|
|
];
|
|
}
|
|
|
|
|
|
if($data['redemptions'] == true){
|
|
$coupon_data['max_redemptions'] = $data['max_redemptions'];
|
|
}
|
|
|
|
if($data['expiration'] == true){
|
|
$coupon_data['redeem_by'] = Carbon::parse($data['expires_at'])->timestamp;
|
|
}
|
|
|
|
if($data['applied_to'] == 'alacarte'){
|
|
|
|
$productIDs = [];
|
|
|
|
foreach ($data['alacarte_id'] as $key => $id) {
|
|
$alacarte = \Modules\WTIALaCarte\Entities\Alacarte::find($id);
|
|
if($alacarte){
|
|
array_push($productIDs,$alacarte->stripe_product_id);
|
|
}
|
|
}
|
|
|
|
$coupon_data['applies_to']['products'] = $productIDs;
|
|
}
|
|
|
|
$coupon = $this->stripePayment->coupons->create($coupon_data);
|
|
|
|
if($data['applied_to'] == 'alacarte'){
|
|
$this->createPromoCode($coupon['id'],$data);
|
|
}
|
|
|
|
return $coupon;
|
|
|
|
}
|
|
|
|
|
|
public function createPromoCode($couponID,$data)
|
|
{
|
|
$promo_data = [
|
|
'coupon' => $couponID,
|
|
'code' => $data['code']
|
|
];
|
|
|
|
if($data['redemptions'] == true){
|
|
$promo_data['max_redemptions'] = $data['max_redemptions'];
|
|
}
|
|
|
|
if($data['expiration'] == true){
|
|
$promo_data['expires_at'] = Carbon::parse($data['expires_at'])->timestamp;
|
|
}
|
|
|
|
return $this->stripePayment->promotionCodes->create($promo_data);
|
|
}
|
|
|
|
public function retrieveCoupon($couponID)
|
|
{
|
|
return $this->stripePayment->coupons->retrieve($couponID, []);
|
|
}
|
|
|
|
public function deleteCoupon($couponID)
|
|
{
|
|
return $this->stripePayment->coupons->delete($couponID, []);
|
|
}
|
|
}
|